概述
C语言实现初级数组算法。
1.删除排序数组中的重复项
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2gy9m/实现代码:
int removeDuplicates(int* nums, int numsSize){
int *i = nums, *end = nums + numsSize;
while (++nums < end)
if (*i != *nums)
*(++i) = *nums;
return i - nums + numsSize + 1;
}
2.买卖股票的最佳时机 II
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2zsx1/实现代码:
int maxProfit(int *prices, int pricesSize){
if (pricesSize < 2)
return 0;
int *end = prices + pricesSize - 1, min = *prices;
int a=*prices,b=*(++prices),c;
pricesSize = 0;
while(prices<end){
c=*(prices+1);
if(b<=a){
if(b<c)
min=b;
}else{
if(b>=c)
pricesSize+=b-min;
}
a=b;
b=c;
prices++;
}
if (b > a)
return pricesSize + b - min;
return pricesSize;
}
3.旋转数组
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2skh7/实现代码:
void reverse(int *nums, int l, int r)
{
int n, len = (l + r) / 2, i, j = --r;
for (i = l; i < len; i++, j--){
n = *(nums + i);
*(nums + i) = *(nums + j);
*(nums + j) = n;
}
}
void rotate(int *nums, int numsSize, int k)
{
k = k % numsSize;
reverse(nums, 0, numsSize);
reverse(nums, 0, k);
reverse(nums, k, numsSize);
}
4.存在重复元素
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x248f5/实现代码:
int *cmp(const void *a,const void *b){return *((int*)a)>*((int*)b)?0:1;}
bool containsDuplicate(int* nums, int numsSize){
qsort(nums,numsSize,sizeof(int),cmp);
int i;
for(i=1;i<numsSize;++i){
if(*(nums+i)==*(nums+i-1))
return true;
}
return false;
}
5.只出现一次的数字
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x21ib6/实现代码:
int singleNumber(int* nums, int numsSize){
int *end=nums+numsSize,n=0;
while(nums!=end){
n^=*nums++;
}
return n;
}
6.两个数组的交集 II
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2y0c2/实现代码:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int cmp(const void *a, const void *b) { return *((int *)a) < *((int *)b) ? 0 : 1; }
int *intersect(int *nums1, int nums1Size, int *nums2, int nums2Size, int *returnSize)
{
qsort(nums1, nums1Size, sizeof(int), cmp);
qsort(nums2, nums2Size, sizeof(int), cmp);
int *retns = (int *)malloc((nums1Size < nums2Size ? nums1Size : nums2Size) * (sizeof(int)));
int i = 0, j = 0, r = 0;
for (; i < nums1Size; ++i){
for (; j < nums2Size; ++j){
if (nums1[i] == nums2[j]){
retns[r++] = nums2[j++];
break;
}
if (nums1[i] < nums2[j]){
break;
}
}
}
*returnSize = r;
return retns;
}
7.加一
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2cv1c/实现代码:
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int *plusOne(int *digits, int digitsSize, int *returnSize)
{
//如果能在原数组的基础上修改,就不申请内存
int i = digitsSize - 1;
*returnSize = digitsSize;
digits[i] += 1;
for (; i > 0; --i){
if (digits[i] > 9){
digits[i] = 0;
digits[i - 1] += 1;
}else{
break;
}
}
int *ret;
if (digits[0] > 9){
digits[0] = 0;
(*returnSize)++;
ret = (int *)malloc((*returnSize) * sizeof(int));
memcpy(ret + 1, digits, digitsSize * sizeof(int));
ret[0] = 1;
return ret;
}
return digits;
}
8.移动零
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2ba4i/实现代码:
void moveZeroes(int *nums, int numsSize)
{
int i = 0, k = 0, t;
for (; i < numsSize; ++i){
if (0 != nums[i]){
t = nums[i];
nums[i] = 0;
nums[k++] = t;
}
}
}
9.两数之和
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2jrse/实现代码:
int comp(const void *a, const void *b)
{
return *(int *)a - *(int *)b;
}
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int *twoSum(int *nums, int numsSize, int target, int *returnSize)
{
int len = numsSize - 1, j = len, i = 0, n;
int *ret = (int *)malloc(sizeof(int) * (2 + numsSize)), *num1;
if (ret == NULL){
*returnSize = 0;
return NULL;
}
num1 = ret + 2;
*returnSize = 2;
memcpy(num1, nums, numsSize * sizeof(int));
qsort(num1, numsSize, sizeof(int), comp);
while (i < len && j > 0){
n = target - num1[i];
if (n < num1[j]) j--;
else if (n > num1[j]) i++;
else{
ret[0] = num1[i];
ret[1] = num1[j];
break;
}
}
for (i = 0, j = 0; i < numsSize && j < 2; ++i){
if (nums[i] == ret[0]){
ret[0] = i;
j++;
}
else if (nums[i] == ret[1]){
ret[1] = i;
j++;
}
}
return ret;
}
10.有效的数独
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x2f9gg/实现代码:
bool isValidSudoku(char** board, int boardSize, int* boardColSize){
int x, y[9] = {0}, b, bit, bi, i, j;
for (i = 0; i < boardSize; ++i)
{
x = 0;
if (i % 3 == 0) b = 0;
for (j = 0; j < *boardColSize; ++j)
{
if (board[i][j] == '.') continue;
bit = 1 << (board[i][j] - '0');
bi = bit << (j / 3) * 9;
if ((bit & x) || (bit & y[j]) || (bi & b))
return false;
x |= bit;
y[j] |= bit;
b |= bi;
}
}
return true;
}
11.旋转图像
初级算法 - LeetBook - 力扣(LeetCode)全球极客挚爱的技术成长平台 (leetcode-cn.com)https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/xnhhkv/实现代码:
void rotate(int **matrix, int matrixSize, int *matrixColSize)
{
int i, j, e_j, e_i, tmp, ends = matrixSize - 1, len = ends;
for (i = 0; i < matrixSize / 2; ++i, --len){
for (j = ends - len; j < len; ++j){
tmp = matrix[i][j];
e_i = ends - i;
e_j = ends - j;
matrix[i][j] = matrix[e_j][i];
matrix[e_j][i] = matrix[e_i][e_j];
matrix[e_i][e_j] = matrix[j][e_i];
matrix[j][e_i] = tmp;
}
}
}
最后
以上就是现实手链为你收集整理的leetcode学习(初级算法-数组)的全部内容,希望文章能够帮你解决leetcode学习(初级算法-数组)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复