[学习报告]《LeetCode零基础指南》(第四讲) 数组

Amy C语言圣王 C++菜鸟,opencv,单片机  2021-11-25 23:53:01
  1. 数组是数的排列,分为有序和无序,可以模拟很多数据组
  2. 数组可以用来循环处理,在C中需要知道数组的大小只能通过一个表示长度的变量进行传递
int search(int* nums, int numsSize, int target){
    int res;
    int left = 0;
    int right = numsSize - 1;
    int mid = (left + right) / 2;

    while (left <= right) {
        printf ("%d, %d, %d\n", left, mid, right);
        if (nums[mid] == target) {
            return mid;
        }

        if (nums[left] == target) {
            return left;
        }

        if (nums[right] == target) {
            return right;
        }

        // left is a sorted array
        if (nums[left] < nums[mid]) {
            if (nums[mid] < target) {
                left = mid + 1;
                right -=1;
            } else {
                left += 1;
                right -=1;
            }
        } else {
        // right is sorted
            if (nums[mid] > target) {
                left += 1;
                right = mid - 1;
            } else {
                left += 1;
                right -=1;
            }
        }
        mid = (left + right) / 2;
    }

    return -1;
}

旋转有序数组也可以二分法

bool search(int* nums, int numsSize, int target) {
    if (numsSize == 0) {
        return false;
    }
    if (numsSize == 1) {
        return nums[0] == target;
    }
    int l = 0, r = numsSize - 1;
    while (l <= r) {
        int mid = (l + r) / 2;
        if (nums[mid] == target) {
            return true;
        }
        if (nums[l] == nums[mid] && nums[mid] == nums[r]) {
            ++l;
            --r;
        } else if (nums[l] <= nums[mid]) {
            if (nums[l] <= target && target < nums[mid]) {
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        } else {
            if (nums[mid] < target && target <= nums[numsSize - 1]) {
                l = mid + 1;
            } else {
                r = mid - 1;
            }
        }
    }
    return false;
}
int findMin(int* nums, int numsSize){
    int left = 0;
    int right = numsSize - 1;
    int mid = (left + right) / 2;

    do {
        ///< when the graph is like ^V
        if (nums[left] > nums[right]) {
            ///< narrow the border, follow the trend
            left = nums[mid] > nums[left] ? mid : left;
            right = nums[mid] < nums[right] ? mid : right;
            mid = (left + right) / 2;
        } else { ///< is like A
            return nums[left];
        } 
    } while (1 != (right - left)) ;

    ///< is like V 
    return nums[right];
}
struct Matrix {
    long long mat[2][2];
};

struct Matrix multiply(struct Matrix a, struct Matrix b) {
    struct Matrix c;
    for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 2; j++) {
            c.mat[i][j] = a.mat[i][0] * b.mat[0][j] + a.mat[i][1] * b.mat[1][j];
        }
    }
    return c;
}

struct Matrix matrixPow(struct Matrix a, int n) {
    struct Matrix ret;
    ret.mat[0][0] = ret.mat[1][1] = 1;
    ret.mat[0][1] = ret.mat[1][0] = 0;
    while (n > 0) {
        if ((n & 1) == 1) {
            ret = multiply(ret, a);
        }
        n >>= 1;
        a = multiply(a, a);
    }
    return ret;
}

int climbStairs(int n) {
    struct Matrix ret;
    ret.mat[1][1] = 0;
    ret.mat[0][0] = ret.mat[0][1] = ret.mat[1][0] = 1;
    struct Matrix res = matrixPow(ret, n);
    return res.mat[0][0];
}

int f[1000];                      // (1)
int climbStairs(int n){
    f[0] = f[1] = 1;              // (2)
    for(int i = 2; i <= n; ++i) {
        f[i] = f[i-1] + f[i-2];   // (3)
    }
    return f[n];                  // (4)
}
int fib(int n){
    int f0 = 0;
    int f1 = 1;
    int fn = f0 + f1;

    while (n > 1) {
        fn = f0 + f1;
        f0 = f1;
        f1 = fn;
        n--;
    }

    if (n < 1) return f0;

    return fn;
}
int tribonacci(int n){
    int T[4] = {0, 1, 1, 2};
    enum {first = 0, second, curr, next};

    if (n < 4) {
        return T[n];
    }

    for (int i = 3; i <= n; ++i) {
        T[next] = T[first] + T[second] + T[curr];
        T[first] = T[second];
        T[second] = T[curr];
        T[curr] = T[next];
    }

    return T[curr];
}
int countKDifference(int* nums, int numsSize, int k){
    int res = 0;

    for (int i = 0; i < numsSize; i++) {
        for (int j = i + 1; j < numsSize; j++) {
            int judge = abs(nums[i] - nums[j]);

            if (judge == k) {
                res++;
            }
        }
    }
    
    return res;
}
int game(int* guess, int guessSize, int* answer, int answerSize){
    int res = 0;
    for (int i = 0; i < 3; ++i) {
        if (guess[i] == answer[i]) {
            ++res;
        }
    }
    return res;
}
int minCount(int* coins, int coinsSize){
    int res = 0;
    for (int i = 0; i < coinsSize;) {
        if (coins[i] > 0) {
            ++res;
            coins[i] -= 2;
        } else {
            ++i;
        }
    }

    return res;
}
...全文
24 8 收藏 回复
写回复
回复
切换为时间正序
请发表友善的回复…
发表回复
相关推荐
发帖
万人千题
创建于2021-10-14

5.9w+

社区成员

学习「 算法 」的捷径就是 「 题海战略 」,社区由「 夜深人静写算法 」作者创建,三年ACM经验,校集训队队长,亚洲区域赛金牌,世界总决赛选手。社区提供系统的训练,答疑解惑,面试经验,大厂内推等机会
帖子事件
创建了帖子
2021-11-25 23:53
社区公告

添加群主微信:18368041307 进行《算法零基础100讲》每日打卡专项训练。