C语言实现初级数组算法。
1.删除排序数组中的重复项
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
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.旋转数组
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.存在重复元素
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.只出现一次的数字
int singleNumber(int* nums, int numsSize){
int *end=nums+numsSize,n=0;
while(nums!=end){
n^=*nums++;
}
return n;
}
6.两个数组的交集 II
/**
* 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.加一
/**
* 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.移动零
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.两数之和
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.有效的数独
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.旋转图像
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;
}
}
}