# leetcode刷题

## Leetcode 14.最长公共前缀

### C语言题解与思路

char* longestCommonPrefix(char** strs, int strsSize) {
int i,j,f,c=0;
for(i=0;i<strlen(strs[0]);i++)
{
f=1;
{
if(strs[j][i]!=strs[j-1][i])
{
f=0;
break;
}
}
if(f==1)
{
c++;
}
else{
break;
}
}
if(c==0)    return "";
char *a=(char*)malloc((c+1)*sizeof(char));
for(i=0;i<c;i++)
{
a[i]=strs[0][i];
}
a[c]='\0';
return a;
}


## Leetcode 15.三数之和

### 题目描述

nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0 。
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0 。
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0 。

• 3 <= nums.length <= 3000
• -105 <= nums[i] <= 105

### C语言题解与思路

/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
void swap(int *a, int *b)
{
int tem = *a;
*a = *b;
*b = tem;
}
int partSort(int *a, int left, int right)
{
int key = left;
while(left < right)
{
while(left < right && a[key] <= a[right])
{
right--;
}
while(left < right && a[key] >= a[left])
{
left++;
}
swap(&a[left], &a[right]);
}
swap(&a[left], &a[key]);
return left;
}
void QuickSort(int *a, int left, int right)
{
if(left >= right)
{
return;
}
int key = partSort(a, left, right);
QuickSort(a, left, key - 1);
QuickSort(a, key + 1, right);
}
int** threeSum(int* nums, int numsSize, int* returnSize, int** returnColumnSizes) {
int i, j, k, n = 20000;
int **str = (int **)malloc(sizeof(int *) * n);
*returnColumnSizes = (int *)malloc(sizeof(int) * n);
*returnSize = 0;
QuickSort(nums, 0, numsSize - 1);
for(i = 0; i < numsSize - 2; i++)
{
if(i > 0 && nums[i] == nums[i-1])
{
continue;
}
j = i + 1;
k = numsSize - 1;
while(j < k)
{
int sum = nums[i] + nums[j] + nums[k];
if(sum == 0)
{
str[*returnSize] = (int *)malloc(sizeof(int) * 3);
(*returnColumnSizes)[*returnSize] = 3;
str[*returnSize] = malloc(sizeof(int) * 3);
str[*returnSize][0] = nums[i];
str[*returnSize][1] = nums[j];
str[*returnSize][2] = nums[k];
(*returnSize)++;
int num1 = nums[j], num2 = nums[k];
while(nums[j] == num1 && j < k)
{
j++;
}
while(nums[k] == num2 && j < k)
{
k--;
}
}
else if(sum < 0){
j++;
}
else{
k--;
}
}
}
return str;
}


## Leetcode 20.有效的括号

### 题目描述

• 1 <= s.length <= 104
• s 仅由括号 ‘()[]{}’ 组成

### C语言的题解与思路

char pairs(char t)
{
if(t == ')')
{
return '(';
}
if(t == '}')
{
return '{';
}
if(t == ']')
{
return '[';
}
return 0;
}
bool isValid(char* s) {
int n = strlen(s);
if(n % 2 == 1)
{
return false;
}
int top = 0, str[n + 1];
for(int i = 0; i < n; i++)
{
char flag = pairs(s[i]);
if(flag != 0)
{
if(top == 0 || str[top - 1] != flag)
{
return false;
}
top--;
}
else
{
str[top++] = s[i];
}
}
}


## Leetcode 21.合并两个有序链表

### 题目描述

• 两个链表的节点数目范围是 [0, 50]
• -100 <= Node.val <= 100
• l1 和 l2 均按 非递减顺序 排列

### C语言题解与思路

/**
* struct ListNode {
*     int val;
*     struct ListNode *next;
* };
*/
struct ListNode* mergeTwoLists(struct ListNode* list1, struct ListNode* list2) {
while(list1&&list2)
{
if(list1->val<list2->val)
{
p->next=list1;
p=p->next;
list1=list1->next;
}
else{
p->next=list2;
p=p->next;
list2=list2->next;
}
}
if(list1)
{
p->next=list1;
}
if(list2)
{
p->next=list2;
}
}


	if (!l1)
return l2;
if (!l2)
return l1;
if (l1->val < l2->val){
l1->next = mergeTwoLists(l1->next, l2);
return l1;
}
else{
l2->next = mergeTwoLists(l1, l2->next);
return l2;
}


## Leetcode 217.存在重复元素

### 题目描述

• 1 <= nums.length <= 105
• -109 <= nums[i] <= 109

### C语言题解和思路

int cmp(const void *a, const void *b)
{
int x = *(int *)a, y = *(int *)b;
return x - y;
}
bool containsDuplicate(int* nums, int numsSize) {
qsort(nums, numsSize, sizeof(int), cmp);
for(int i = 1; i < numsSize; i++)
{
if(nums[i] == nums[i - 1])
{
return true;
}
}
return false;
}


## LCR 086. 分割回文串

### 题目描述

• 1 <= s.length <= 16
• s 仅由小写英文字母组成

### C语言题解和思路

/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
*/
void dfs(char *s, int n, int i, int **f, char ***ret, int *retSize, int *retColSize, char **ans, int *ansSize)
{
if(i == n)
{
char **tmp = malloc(sizeof(char *) * (*ansSize));
for(int j = 0; j < (*ansSize); j++)
{
int ansColSize = strlen(ans[j]);
tmp[j] = malloc(sizeof(char) * (ansColSize + 1));
strcpy(tmp[j], ans[j]);
}
ret[*retSize] = tmp;
retColSize[(*retSize)++] = *ansSize;
return;
}
for(int j = i; j < n; j++)
{
if(f[i][j] != 0)
{
char *sub = malloc(sizeof(char) * (j - i + 2));
for(int k = i; k <= j; k++)
{
sub[k - i] = s[k];
}
sub[j - i + 1] = '\0';
ans[(*ansSize)++] = sub;
dfs(s, n, j + 1, f, ret, retSize, retColSize, ans, ansSize);
(*ansSize)--;
}
}
}
char*** partition(char* s, int* returnSize, int** returnColumnSizes){
int n = strlen(s);
int maxlen = n * 2^(n);
char ***ret = malloc(sizeof(char **) * maxlen);
*returnSize = 0;
*returnColumnSizes = malloc(sizeof(int) * maxlen);
int *f[n];
for(int i = 0; i < n; i++)
{
f[i] = malloc(sizeof(int) * n);
for(int j = 0; j < n; j++)
{
f[i][j] = 1;
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = i + 1; j < n; ++j) {
f[i][j] = (s[i] == s[j]) && f[i + 1][j - 1];
}
}
char *ans[n];
int ansSize = 0;
dfs(s, n, 0, f, ret, returnSize, *returnColumnSizes, ans, &ansSize);
return ret;
}


dfs函数开始时判断当前递归处理的起始索引是否等于当前字符串长度，当二者相等时，表示已经处理完整个字符串，即得到了一个可能的回文子串分割方案。然后将这个结果保存给 ret ，分割方案长度保存在 retColSize ，更新 retSize 并返回上一层递归。

• 9
点赞
• 25
收藏
觉得还不错? 一键收藏
• 1
评论
10-01 8万+
12-15 403
07-01 5225
05-28 616
02-13 1万+
02-20
01-17
08-04
07-05

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。