网上找了视频,LeetCode 30 天挑战,用c语言写,记录一下,一共30个leetcode 算法题 对应30天,大概需要写10篇,每篇3道题,手打下代码,外加记录一下。
统计数组中某数+1还在该数组,由于该题在leetcode里面需要付费订阅,写下思路
1.暴力法就是循环2个for
2.由于限制了数据的大小为0-1000,空间换时间,创建一个1002(1001个数据外加1个大于一的)的矩阵里面存储
true 和 false, 先走一遍循环,把true和false 填上,然后再走循环+1,判断在改矩阵是不是true
另外的一些感受/统计次数和排序是一个意思 用函数qsort
找到中间的数,但是是链表的中间数,链表的解释在后面,链表包括数据和下个数据的指针。
//截至条件就是curr==NULL
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* middleNode(struct ListNode* head){
struct ListNode* curr=head;
struct ListNode* result=head;
int count=0;
while(curr!=NULL)
{
curr=curr->next;
count++;
}
printf("xxxx%d\n",count);
int i=0;
while(i<count/2)
{
printf("----%d\n",result->val);
result=result->next;
i++;
}
return result;
}
字符串s,t里面#号代表删除,比较s,t是否相等?
// 暴力法,从左到右写,两个参数i,j定位相应的位置
char *process(char * s)
{
int length=strlen(s);
int j=0;
for(int i=0;i<length;i++)
{
if(s[i]=='#')
{
if(j>0){
j--;
}
}
else{
s[j]=s[i];
j++;
}
}
s[j]='\0';
return s;
}
bool backspaceCompare(char * s, char * t){
process(s);
process(t);
printf("%s\n%s",s,t);
return strcmp(s, t)==0;
}
注意 字符串和字符,单引号,双引号, %s,%c的区别
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。