# [学习报告]《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) {
++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 收藏 回复

5.9w+

2021-11-25 23:53