头像

kelly1895


访客:1065

离线:21小时前


活动打卡代码 LeetCode 90. Subsets II

kelly1895
4个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~


/*
leetcode78 数组的子集(位运算法)
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description: 项目 leetcode78 的源文件
 * Author: y00559460
 * Create: 2020-05-25
 */
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description: 项目 testnew 的源文件
 * Author: y00559460
 * Create: 2020-05-18
 */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

 #define N 10010

int oneCnt(int n)
{
    int cnt=0;
    while(n){
        if((n&1) == 1){
            cnt++;
        }
        n >>= 1;
    }
    return cnt;
}

void getnum(int* num,int* res,int n){
    int k=0,idx=0;
    while(n){
        if((n&1) == 1){
            res[idx++]=num[k];
        }
        n>>=1;
        k++;
    }
}

int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    /*res数组的行数*/
    int resrow = (int)pow(2.0,numsSize);
    int** res = (int**)calloc(resrow, sizeof(int*));
    *returnSize = resrow;
    *returnColumnSizes = (int*)calloc(resrow,sizeof(int));
    for(int i = 0; i<resrow;i++){
        /*子集的大小 也就是有几个1*/
        (*returnColumnSizes)[i] = oneCnt(i);
        res[i] = (int*)calloc((*returnColumnSizes)[i],sizeof(int));
        getnum(nums,res[i],i);
    }
    return res;
}

int main()
{
    int nums[3] = {1,2,3};
    int numsSize = 3;
    int n = (int)pow(2.0,numsSize);
    int** res = calloc(n,sizeof(int*));
    int returnSize;
    int* returnColumnSize = calloc(n,sizeof(int));
    res = subsets(nums,numsSize,&returnSize,&returnColumnSize);

    system("pause");
}









活动打卡代码 LeetCode 78. Subsets

kelly1895
4个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~


/*
leetcode78 数组的子集(位运算法)
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description: 项目 leetcode78 的源文件
 * Author: y00559460
 * Create: 2020-05-25
 */
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description: 项目 testnew 的源文件
 * Author: y00559460
 * Create: 2020-05-18
 */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

 #define N 10010

int oneCnt(int n)
{
    int cnt=0;
    while(n){
        if((n&1) == 1){
            cnt++;
        }
        n >>= 1;
    }
    return cnt;
}

void getnum(int* num,int* res,int n){
    int k=0,idx=0;
    while(n){
        if((n&1) == 1){
            res[idx++]=num[k];
        }
        n>>=1;
        k++;
    }
}

int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    /*res数组的行数*/
    int resrow = (int)pow(2.0,numsSize);
    int** res = (int**)calloc(resrow, sizeof(int*));
    *returnSize = resrow;
    *returnColumnSizes = (int*)calloc(resrow,sizeof(int));
    for(int i = 0; i<resrow;i++){
        /*子集的大小 也就是有几个1*/
        (*returnColumnSizes)[i] = oneCnt(i);
        res[i] = (int*)calloc((*returnColumnSizes)[i],sizeof(int));
        getnum(nums,res[i],i);
    }
    return res;
}

int main()
{
    int nums[3] = {1,2,3};
    int numsSize = 3;
    int n = (int)pow(2.0,numsSize);
    int** res = calloc(n,sizeof(int*));
    int returnSize;
    int* returnColumnSize = calloc(n,sizeof(int));
    res = subsets(nums,numsSize,&returnSize,&returnColumnSize);

    system("pause");
}









活动打卡代码 LeetCode 78. Subsets

kelly1895
4个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

/*
leetcode78 数组的子集(位运算法)
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description: 项目 leetcode78 的源文件
 * Author: y00559460
 * Create: 2020-05-25
 */
/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2012-2018. All rights reserved.
 * Description: 项目 testnew 的源文件
 * Author: y00559460
 * Create: 2020-05-18
 */
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

 #define N 10010

int oneCnt(int n)
{
    int cnt=0;
    while(n){
        if((n&1) == 1){
            cnt++;
        }
        n >>= 1;
    }
    return cnt;
}

void getnum(int* num,int* res,int n){
    int k=0,idx=0;
    while(n){
        if((n&1) == 1){
            res[idx++]=num[k];
        }
        n>>=1;
        k++;
    }
}

int** subsets(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    /*res数组的行数*/
    int resrow = (int)pow(2.0,numsSize);
    int** res = (int**)calloc(resrow, sizeof(int*));
    *returnSize = resrow;
    *returnColumnSizes = (int*)calloc(resrow,sizeof(int));
    for(int i = 0; i<resrow;i++){
        /*子集的大小 也就是有几个1*/
        (*returnColumnSizes)[i] = oneCnt(i);
        res[i] = (int*)calloc((*returnColumnSizes)[i],sizeof(int));
        getnum(nums,res[i],i);
    }
    return res;
}

int main()
{
    int nums[3] = {1,2,3};
    int numsSize = 3;
    int n = (int)pow(2.0,numsSize);
    int** res = calloc(n,sizeof(int*));
    int returnSize;
    int* returnColumnSize = calloc(n,sizeof(int));
    res = subsets(nums,numsSize,&returnSize,&returnColumnSize);

    system("pause");
}









活动打卡代码 LeetCode 47. Permutations II

kelly1895
5个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
class Solution {
private:
    vector<int> path;
    vector<bool> st;
    vector<vector<int>> res;
    void dfs(vector<int>& nums,int idx,int start){
        if(idx==nums.size()){
            res.push_back(path);
            return;
        }
        for(int i=start;i<nums.size();i++){    //枚举的每个数应该放在哪个位置
            if(!st[i]){
                path[i]=nums[idx];
                st[i] = true;
                if(idx+1 < nums.size() && nums[idx]==nums[idx+1])
                    dfs(nums,idx+1,i+1);
                else
                    dfs(nums,idx+1,0);
                st[i]=false;
            }
        }
    }
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        st = vector<bool>(nums.size(), false);
        path =vector<int>(nums.size());
        dfs(nums,0,0);
        return res;
    }
};



活动打卡代码 LeetCode 47. Permutations II

kelly1895
5个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

class Solution {
private:
    vector<int> path;
    vector<bool> st;
    vector<vector<int>> res;
    void dfs(vector<int>& nums,int idx,int start){
        if(idx==nums.size()){
            res.push_back(path);
            return;
        }
        for(int i=start;i<nums.size();i++){    //枚举的每个数应该放在哪个位置
            if(!st[i]){
                path[i]=nums[idx];
                st[i] = true;
                if(idx+1 < nums.size() && nums[idx]==nums[idx+1])
                    dfs(nums,idx+1,i+1);
                else
                    dfs(nums,idx+1,0);
                st[i]=false;
            }
        }
    }
public:
    vector<vector<int>> permuteUnique(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        st = vector<bool>(nums.size(), false);
        path =vector<int>(nums.size());
        dfs(nums,0,0);
        return res;
    }
};


活动打卡代码 AcWing 46. Permutations

kelly1895
5个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
//C语言
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */


 void dfs(int u,int* nums, int numsSize,int* path, int** res, int* st,int* resSize)
 {
     if(u==numsSize){
        memcpy(res[*resSize],path,sizeof(int)*numsSize);
        *resSize = *resSize+1;
        return;
     }
    for(int i = 0; i < numsSize;i++){
        if(st[i]==0){
            st[i] = 1;
            path[u] = nums [i];
            dfs(u+1,nums,numsSize,path,res,st,resSize);
            st[i] = 0;
        }
    }
 }


int** permute(int* nums, int numsSize, int* returnSize, int** returnColumnSizes){
    if(numsSize==0){
        *returnSize = 0;
        return NULL;
    }
    /*计算一共有几种方式,即res数组的行数*/
    int len =1;
    for(int i=1;i<=numsSize;i++){
        len *= i;
    }
    /*分配二维数组*/
    int** res = (int**)malloc(sizeof(int*)*len);
    for(int i=0;i<len;i++){
        res[i]=(int*)malloc(sizeof(int)*numsSize);
        /*每列的长度*/
        (*returnColumnSizes)[i]=numsSize;
    }

    /*分配一维数组*/
    int* path = (int*)malloc(sizeof(int)*numsSize);
    int* st = (int*)malloc(sizeof(int)*numsSize);
    *returnSize = len;
    memset(st,0,sizeof(int)*numsSize);
    int resSize = 0;
    dfs(0,nums,numsSize,path,res,st,&resSize);
    return res;
}





活动打卡代码 LeetCode 79. Word Search

kelly1895
5个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~
class Solution {
private:
    bool dfs(int row,int col,int idx, vector<vector<char>>& board, string word){
        if(idx==word.size()) return true;
        //标志位可以置为*表示已使用
        int dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
        char t = board[row][col];
        board[row][col]='*';
        for(int i=0;i<4;i++){
            int a=row+dx[i],b=col+dy[i];
            if(a>=0&&a<board.size()&&b>=0&&b<board[0].size()&&board[a][b]==word[idx]){
                if(dfs(a,b,idx+1,board,word))
                    return true;
            }
        }
        board[row][col]=t;
        return false;
    }

public:
    bool exist(vector<vector<char>>& board, string word) {
        bool b=false;
        int m=board.size(),n=board[0].size();
        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                if(board[i][j]==word[0]){
                    b=dfs(i,j,1,board,word);
                    if(b==true){
                        return true;
                    }
                }
            }
        }
        return false;
    }
};




kelly1895
5个月前
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

class Solution {
private:
   unordered_map<int,string > num2char{
      {2,"abc"},{3,"def"},{4,"ghi"},{5,"jkl"},{6,"mno"},{7,"pqrs"},{8,"tuv"},{9,"wxyz"}
   };
    void dfs(string &digits,vector<string> &res,int idx,string &restr)
    {
        if(idx==digits.size()){
            if(restr.size()>0){res.push_back(restr);}    
            return;
        }

        for(int i=0;i<num2char[digits[idx]].size();i++){
            string tmp=num2char[digits[idx]];
            restr.push_back(tmp[i]);
            dfs(digits,res,idx+1,restr);
            restr.pop_back();
        }
    }
public:
    vector<string> letterCombinations(string digits) {
        vector<string> res;
        string restr="";
        dfs(digits,res,0,restr);
        return res;
    }
};



kelly1895
2019-09-18 03:06
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

class Solution {
public:
   bool isNumber(string s) {
        int i = 0;
        while (i < s.size() && s[i] == ' ') i ++ ;//删掉前面的空格
        int j = s.size() - 1;
        while (j >= 0 && s[j] == ' ') j -- ;//删掉后面的空格
        if (i > j) return false;
        s = s.substr(i, j - i + 1);

        if (s[0] == '-' || s[0] == '+') s = s.substr(1);//如果+- 直接跳过
        if (s.empty() || s[0] == '.' && s.size() == 1) return false;//排除 + — .

        int dot = 0, e = 0;
        for (int i = 0; i < s.size(); i ++ )
        {
            if (s[i] >= '0' && s[i] <= '9');
            else if (s[i] == '.')
            {
                dot ++ ;
                if (e || dot > 1) return false;  // 小数前面不能有e 小数点只能有一个
            }
            else if (s[i] == 'e' || s[i] == 'E')
            {
                e ++ ;
                if (i + 1 == s.size() || !i || e > 1 || i == 1 && s[0] == '.') return false;//e前后没有数 e多与一个数 e前面是小数点
                if (s[i + 1] == '+' || s[i + 1] == '-')
                {
                    if (i + 2 == s.size()) return false;  //2e+ 3e-
                    i ++ ;
                }
            }
            else return false;
        }
        return true;
    }
};



kelly1895
2019-09-18 02:54
//这里填你的代码^^
//注意代码要放在两组三个点之间,才可以正确显示代码高亮哦~

bool isMatch(string s, string p) {
        if(!p.size())  return !s.size();
        return match(s,p,0,0);
    }
    bool match(string s,string p,int a,int b){
        if(b == p.size())   return a == s.size();
        if(b + 1 == p.size())   return a + 1 == s.size() && (s[a] == p[b] || p[b] == '.');
        if(p[b+1] != '*'){
            if(s[a] == p[b] || p[b] == '.')    return match(s,p,a+1,b+1);
            return false;
        }
        if(s[a] != p[b] && p[b] != '.')    return match(s,p,a,b+2);
        return match(s,p,a,b+2) || match(s,p,a+1,b+2) || match(s,p,a+1,b);
    }