leetcode一期整理(没好好整

题目代码区

剑指12

// https://leetcode-cn.com/problems/ju-zhen-zhong-de-lu-jing-lcof/
// 剑指12,同主站79
// https://leetcode-cn.com/problems/word-search/
// 2021-01-12 22:48
bool exist(char** board, int boardSize, int* boardColSize, char * word){
    int wordlength = 0;
    while(word[wordlength] != '\0'){
        wordlength++;
    }
    // 设好了wordlength,接下来设一个坐标表
    bool found[wordlength];
    int co1[wordlength];
    int co2[wordlength];
    int flag = 0;
    for(int i = 0; i <wordlength; i++){
        co1[i] = -1;
        co2[i] = -1;
        found[i] = false;
    }
    // 接下来设一个与board同大小的布尔表,用来表示board同位置的字母是否被用过
    bool* boolboard[boardSize];
    for(int x = 0; x <boardSize; x++){
        boolboard[x] = (bool *)malloc(sizeof(bool) * boardColSize[x]);
    }//为每个小表申请空间
    for(int x = 0; x <boardSize; x++){
        for(int y =0; y <boardColSize[x];y++){
            boolboard[x][y] = false;
        }
    }//将所有表都设为false
    void filltrue(int x, int y){
        boolboard[x][y] = true;
    }
    void fillfalse(int x, int y){
        boolboard[x][y] = false;
    }
    bool checkbool(int x, int y){
        return boolboard[x][y];
    }
    void fillco(int x, int y){
        if (flag==wordlength)
            return;
        co1[flag] = x;
        co2[flag] = y;
        found[flag] = true;
        flag++;
        filltrue(x,y);
    }
    void deleteco(){
        flag--;
        found[flag] = false;
        fillfalse(co1[flag],co2[flag]);
        co1[flag] = -1;
        co2[flag] = -1;
    }
    bool check(int now, int x,int y){
        return (board[x][y] == word[now]);
    }
    void initboard(){
        while(flag!=0){
            deleteco();
        }
    }
// 套娃函数
// 为的是在x,y周围找到index为now的字母
    void taowa(int now, int x, int y){
// x+1阶段
        if (flag == wordlength)
            return;
        while (flag>now){
            deleteco();
        }
        if (x+1 < boardSize){
            if( (check(now,x+1,y))&& !checkbool(x+1,y) ){
                fillco(x+1,y);
                taowa(now+1,x+1,y);
            }
        }
// x-1阶段
        if (flag == wordlength)
            return;
        while (flag>now){
            deleteco();
        }
        if (x-1 >=0){
            if( (check(now,x-1,y))&& !checkbool(x-1,y) ){
                fillco(x-1,y);
                taowa(now+1,x-1,y);
            }
        }
// y+1阶段
        if (flag == wordlength)
            return;
        while (flag>now){
            deleteco();
        }
        if(y+1 <boardColSize[x]){
            if( (check(now,x,y+1)) && !checkbool(x,y+1)){
                fillco(x,y+1);
                taowa(now+1,x,y+1);
            }
        }
// y-1阶段
        if (flag == wordlength)
            return;
        while (flag>now){
            deleteco();
        }
        if(y-1 >=0 ){
            if( (check(now,x,y-1)) && !checkbool(x,y-1)){
                fillco(x,y-1);
                taowa(now+1,x,y-1);
            }
        }
    }
// 流程分为两步
// 1. 找到第一个字母
// 2. 开始套娃找下一个字母
    for(int x = 0; x <boardSize; x++){
        for(int y =0; y <boardColSize[x];y++){
            initboard();
            if (check(0,x,y)){
                fillco(x,y);
                taowa(1,x,y);
            }
            if(flag == wordlength)
                return true;
        }
    }
    return false;
}

139

// 20210113
// https://leetcode-cn.com/problems/word-break/
bool wordBreak(char * s, char ** wordDict, int wordDictSize){
// 求长的函数
	int getlen(char *a){
		int m = 0;
		while(a[m] != '\0')
			m++;
		return m;
	}
// 定义总长
	int n = getlen(s);
// 定义一个布尔数组并初始化
	bool pd[n+1];
	for(int i=1; i<n+1; i++){
		pd[i] = false;
	}
	pd[0] = true;
// 搞个判定函数
	bool check(char *a, char *b, int len,int origin){
		int i =0;
		while(i< len){
			if(a[i] != b[i+origin]){
				return false;
            }else{
			i++;
		    }
		}
		return true;
	}
// 走流程开始
	for(int i=0; i<n; i++){
		for(int j= i+1; j< n+1; j++){
			if( !pd[i]) {
				break;
			}	// i不行的话直接out
			if(pd[j]){
				continue;
			}	// j行的话直接out
			bool ok = false;
			// 接下来轮一圈比较
			for (int k= 0; k<wordDictSize; k++){
				if(ok)
					break;
				char* checking = wordDict[k];
				int wordlen = getlen(checking);
				if(wordlen != j-i)
					continue;
				ok = check(checking, s, wordlen,i);
			}
			if(ok)
				pd[j] = true;
		}
	}
	return pd[n];
}

98

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


bool isValidBST(struct TreeNode* root){
// 得到最左边的数
    int getsmallest(struct TreeNode* root){
        while(root->left != NULL)
            root = root->left;
        return root->val;
    }
    int pre = getsmallest(root);
    bool first = false;
    bool answer = true;
// 写一个中序遍历的函数
    void mid(struct TreeNode* root){
        if (answer == false)
            return;
        if(root == NULL)
            return;
        mid(root->left);
        if (first){
            if (pre < root->val){
                pre = root->val;
            }else{
                answer = false;
            }
        }else{
            first =true;
        }
        mid(root->right);
    }
    mid(root);
    return answer;
}

20

// https://leetcode-cn.com/problems/valid-parentheses/
bool isValid(char * s){
    bool answer =true;
    // 求长的函数
    int getlen(char *a){
        int m = 0;
        while(a[m] != '\0')
            m++;
        return m;
    }
    int len= getlen(s);
    int stacklen= len/2;
    if(stacklen==0)
        return false;
    int zhan[stacklen];
    for (int i = 0; i <stacklen; i++)
        zhan[i] =0; // 初试全设为0
    int flag = 0;
    // 入栈
    void push(int a){
        if(flag == stacklen){
            answer = false;
            return;
        }
        zhan[flag] = a;
        flag++;
    }
    int pop(void){
        if(flag == 0){
            answer =false;
            return;
        }
        flag--;
        int m = zhan[flag];
        zhan[flag] = 0;
        return m;
    }
    // 整个过程可以看做一次循环,单次循环里进行入栈或出栈
    for (int i = 0; i <len; i++){
        if(!answer)
            return answer;
        if(s[i] == '(')
            push(1);
        if(s[i] == '[')
            push(2);
        if(s[i] == '{')
            push(3);
        if(s[i] == ')')
            if(pop() != 1)
                answer =false;
        if(s[i] == ']')
            if(pop() != 2)
                answer =false;
        if(s[i] == '}')
            if(pop() != 3)
                answer =false;
    }
    if(flag == 0)
        return answer;
    else
        return false;
}

62

// https://leetcode-cn.com/problems/unique-paths/
int uniquePaths(int m, int n){
    m = m -1;
    n = n-1;
    if(m == 0 )
        return 1;
    if(n == 0 )
        return 1;
    int big, small;
    if (m >n ){
        big = m, small = n;
    }else{
        big = n, small = m;
    }
    long answer=1;
    for (int i = big+1; i<=big +small; i++){
        answer *=i;
    }
    for(int i =1; i <= small; i++){
        answer /= i;
    }
    return answer;
}

1

// https://leetcode-cn.com/problems/two-sum/
/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    for(int i = 0; i< numsSize-1; i++){
        for( int j= i+1; j<numsSize;j++){
            if( nums[i] + nums[j] ==target){
                returnSize[0]=2;
                int *m = (int *)malloc(sizeof(int) * 2);
                m[0] = i;
                m[1] = j;
                return m;
            }
        }
    }
    return nums;
}

96

// https://leetcode-cn.com/problems/unique-binary-search-trees/
int numTrees(int n){
    if(n==1)
        return 1;
    if(n==2)
        return 2;
    int p[n+1];
    p[0] = 1;
    p[1] = 1;
    p[2] = 2;
    for(int i = 3; i < n +1; i++){
        p[i] =0;
        for(int j = 0; j<i; j++){
            p[i] += (p[j]*p[i-1-j]);
        }
    }
    return p[n];
}

42

// https://leetcode-cn.com/problems/trapping-rain-water/submissions/

// 递归算法,每次求出中间一部分
int trap(int* height, int heightSize){
    if (heightSize <= 2)
        return 0;
// 开头跟结尾必0
    int water[heightSize];
// 把所有水记录为0
    for(int i = 0; i <heightSize; i++){
        water[i] = 0;
    }
    int big1,big2;
// 找到最大两个, j必须大于i,否则不好弄
    void find2big(int i, int j){
        big1 = i;
        for(int m = i; m<=j; m++){
            if(height[m]> height[big1]){
                big1 = m;
            }
        }// 确定了big1
        if(big1 == i )
            big2 = i+1;
        else
            big2 = i;
        for(int m = i; m<=j; m++){
            if(m==big1)
                continue;
            if(height[m]> height[big2]){
                big2 = m;
            }
        }// 确定了big2
        if(big1>big2){
            int temp = big1;
            big1 = big2;
            big2 = temp;
        }
    }
//  用于每次求中间一部分的函数,先找到区间内最高的两个柱子
    void DOmid(int i, int j){
        if((i==j)||((i+1)==j))
            return;
        find2big(i,j);
        int small;
        if(height[big1]>height[big2])
            small = height[big2];
        else
            small = height[big1];
        for(int k = big1+1; k< big2;k++)
            water[k] = small-height[k];
        DOmid(i,big1);
        DOmid(big2,j);
    }
    DOmid(0,heightSize-1);
//求和
    int sum = 0;
    for(int i = 1; i <heightSize-1; i++)
        sum += water[i];
    return sum;
}

3,剑指48

// language c
// 剑指48
// https://leetcode-cn.com/problems/zui-chang-bu-han-zhong-fu-zi-fu-de-zi-zi-fu-chuan-lcof/
// 同主站3
// https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/
// 滑动窗口
int lengthOfLongestSubstring(char * s){
    int count =0;
    int max = 0;
    int ima = 0;
    int start = 0;
// 判断函数
    int check(int p, int q){// p 是开始点,q是待判定点
        while(p<q){
            if(s[p] == s[q])
                return p;   // 重复
            p++;
        }
        return -1;  // 不重复
    }
    int flag;
    while(s[count] != '\0'){
        flag = check(start, count);
        if(flag==-1){
            count++;
        }else{  // 结算
            ima = count-start;
            if( ima >max)
                max = ima;
            start = flag+1;
            count++;
        }
    }
    // 最终结算
    ima = count-start;
    if( ima >max)
        max = ima;
    return max;
}

4

// https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size){
    int index1,index2;
    if( (nums1Size+nums2Size)%2 ==1){
        index1 = (nums1Size+nums2Size+1)/2;
        index2 = index1;
    }else{
        index1 = (nums1Size+nums2Size)/2;
        index2 = index1+1;
    }
    double an1,an2;
    int for1=0,for2=0,count=0,num;

    while(count != index1){
        if((for1<nums1Size) && (for2<nums2Size)){   // 情况1
            if(nums1[for1]<nums2[for2]){
                count++;
                num = nums1[for1++];
            }else{
                count++;
                num = nums2[for2++];
            }
        }else if(for1==nums1Size){  // 情况2
            count++;
            num = nums2[for2++];
        }else if(for2==nums2Size){  // 情况3
            count++;
            num = nums1[for1++];
        }
    }
    an1 = num;
    if(index1 == index2)
        return an1;         // 奇数总数
    if((for1<nums1Size) && (for2<nums2Size)){   // 情况1
        if(nums1[for1]<nums2[for2]){
            count++;
            an2 = nums1[for1++];
        }else{
            count++;
            an2 = nums2[for2++];
        }
    }else if(for1==nums1Size){
        count++;
        an2 = nums2[for2++];
    }else if(for2==nums2Size){
        count++;
        an2 = nums1[for1++];
    }
    
    return (an1+an2)/2;
}

238

// https://leetcode-cn.com/problems/product-of-array-except-self/
// 第一种解法,比较好,因为题目不让用除法,而第二种写之前没看清题目,所以用了除法
int* productExceptSelf(int* nums, int numsSize, int* returnSize){
    returnSize[0] = numsSize;
    int* answer = (int *)malloc(sizeof(int) * numsSize);
    answer[0] = 1;
    for(int i = 1;i <numsSize;i++){
        answer[i] = answer[i-1] * nums[i-1];
    }
    int temp1=nums[numsSize-1],temp2;
    nums[numsSize-1] = 1;
    for(int i = numsSize-2; i>=0; i--){
        temp2 = nums[i];
        nums[i] = nums[i+1] * temp1;
        temp1 = temp2;
    }
    for(int i = 0;i <numsSize;i++){
        answer[i] = answer[i]*nums[i];
    }
    return answer;
}
// https://leetcode-cn.com/problems/product-of-array-except-self/
// 第二种解法,有问题,因为题目不让用除法
int* productExceptSelf(int* nums, int numsSize, int* returnSize){
    returnSize[0] = numsSize;
    int all = 1;
    for(int i = 0;i <numsSize;i++){
        all*= nums[i];
    }
    int* answer = (int *)malloc(sizeof(int) * numsSize);
    for(int i = 0;i <numsSize;i++){
        if(nums[i] != 0 )
            answer[i] = all/nums[i];
        else{
            answer[i] =1;
            for(int j =0; j<numsSize;j++){
                if(j ==i)
                    continue;
                answer[i] *= nums[j];
            }
        }
    }
    return answer;
}

974

// https://leetcode-cn.com/problems/subarray-sums-divisible-by-k/
int subarraysDivByK(int* A, int ASize, int K){
    int fuzhu[ASize];
    fuzhu[0] = A[0]%K;
    if(fuzhu[0]<0)
        fuzhu[0] += K;
    int answer = 0;
    int i;
    for(i = 1; i < ASize; i++){
        fuzhu[i] = (fuzhu[i-1]+A[i])%K;
        if(fuzhu[i]<0)
            fuzhu[i] += K;
    }
    int yu[K];
    for(i = 0; i<K; i++){
        yu[i] = 0;
    }
    for(i = 0; i < ASize; i++){
        yu[fuzhu[i]]++;
    }
    yu[0] +=1;
    for(i = 0; i<K; i++){
        if(yu[i] >1)
            answer+= yu[i]*(yu[i]-1)/2;
    }
    return answer;
}

628

// https://leetcode-cn.com/problems/maximum-product-of-three-numbers/
int maximumProduct(int* nums, int numsSize){
    int a= nums[0],b = nums[1], c = nums[2];
    void sort(void){    //小中大0
        if(a>b){
            int temp =a;
            a = b;
            b = temp;
        }
        if(b>c){
            int temp = b;
            b = c;
            c =temp;
        }
        if(a>b){
            int temp =a;
            a = b;
            b = temp;
        }
    }
    if(numsSize==3)
        return a*b*c;
// 获取最大数以及它的下标
    int max = nums[0], maxindex = 0;
    for(int i =0; i <numsSize; i++){
        if(nums[i]>max){
            max = nums[i];
            maxindex = i;
        }
    }
// 获取完毕,对max进行判断
    if(max == 0)
        return 0;
    if(max < 0 ){   //  全负情况
        sort();
        for(int i = 3;i <numsSize;i++){
            if(nums[i]>a){
                a = nums[i];
                sort();
            }
        }
        return (a*b*c);
    }
    // 最后一种情况,max>0
    int z1=0,z2=0,f1=0,f2=0;
    void zsort(void){   // 0小大
        if(z1>z2){
            int temp = z1;
            z1 = z2;
            z2 = temp;
        }
    }
    void fsort(void){   // 小大0
        if(f1>f2){
            int temp = f1;
            f1 = f2;
            f2 = temp;
        }
    }
    for(int i =0; i <numsSize; i++){
        if(i == maxindex)
            continue;
        if(nums[i]>z1){
            z1 = nums[i];
            zsort();
        }
        if(nums[i]<f2){
            f2 = nums[i];
            fsort();
        }
    }
    z1 = z1*z2;
    f1 = f1*f2;
    if(z1>f1)
        return z1*max;
    else
        return f1*max;
}

206,剑指24

// 剑指24
// https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/
// 同主站206
// https://leetcode-cn.com/problems/reverse-linked-list/
struct ListNode* reverseList(struct ListNode* head){
    if(head ==NULL)
        return head;
    struct ListNode * newhead = head, *p;
    head = head->next;
    newhead->next = NULL;
    while(head){
        p = head;
        head = head->next;
        p->next = newhead;
        newhead = p;
    }
    return newhead;
}

剑指03

// 剑指03
// https://leetcode-cn.com/problems/shu-zu-zhong-zhong-fu-de-shu-zi-lcof/
int findRepeatNumber(int* nums, int numsSize){
    int a;
    int findone(int m){
        int n = nums[m];
        nums[m] = -1;
        while((n!=-1)&&(nums[n] != n)){
            m = nums[n];
            nums[n] = n;
            n = m;
        }
        return n;
    }
    for(int i = 0; i< numsSize; i++){
        if(nums[i] != i){
            a = i;
            a = findone(a);
            if(a!=-1)
                return a;
        }
    }
    return -1;
}

posted @ 2021-03-11 10:24  RougeBW  阅读(51)  评论(0编辑  收藏  举报