携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第3天,点击查看活动详情
题目描述
解题思路
本题就是在两个字符串中查找相同的字符,对相同的元素进行解释它所代表的意思。 注意: 1.星期要控制字符的范围为
A到G2.小时的范围为0到9,以及A到N3.要%02d输出小时和分钟
代码
#include <stdio.h>
int ret[4];
void find1(char* s1,char* s2)
{
int i =0,n=0;
while (s1[i]&&s2[i])
{
if (s1[i] == s2[i])
{
if (s1[i] >= 'A' && s1[i] <= 'G')
{
ret[n++] = i;
}
else if(((s1[i] >= 'A' && s1[i] <= 'N') || (s1[i] >= '0' && s1[i] <= '9')) && n == 1)
{
ret[n++] = i;
}
}
i++;
}
}
void find2(char* s1, char* s2)
{
int i = 0;
while (s1[i] && s2[i])
{
if (s1[i] == s2[i])
{
if ((s1[i] >= 'A' && s1[i] <= 'Z')|| (s1[i] >= 'a' && s1[i] <= 'z'))
ret[0] = i;
}
i++;
}
}
int main()
{
char a1[61], a2[61], b1[61], b2[61];
char* day[] = { "MON","TUE","WED","THU","FRI","SAT","SUN" };
scanf("%s%s%s%s", a1, a2, b1, b2);
find1(a1, a2);//前两个串
//下面这个是处理小时问题
if (a1[ret[1]] >= 'A')
ret[1] = a1[ret[1]] - 'A' + 10;
else
ret[1] = a1[ret[1]] - '0';
printf("%s %02d", day[a1[ret[0]]-'A'], ret[1]);
find2(b1, b2);//后两个串
printf(":%02d", ret[0]);
return 0;
}
剑指Offer06.从尾到头打印链表
解题思路
1.先遍历1遍,得出链表的长度L。 2.动态开辟L长度的内存,从头遍历链表,同时把值给数组,数组存值时,从后往前给值。 ==时间复杂度O(N)==
代码
int* reversePrint(struct ListNode* head, int* returnSize){
struct ListNode *cur=head;
int n=0;
while(cur)
{
cur=cur->next;
n++;
}
*returnSize=n;
int* arr=(int*)malloc(sizeof(int)*n);
cur=head;
while(cur)
{
arr[n-1]=cur->val;
n--;
cur=cur->next;
}
return arr;
}
剑指Offer18.删除链表的节点
解题思路
可以用一个带哨兵位的头节点作为链表的头。把不等于该值的节点尾插到新链表的后面。最后释放该哨兵节点。
代码
struct ListNode* deleteNode(struct ListNode* head, int val){
struct ListNode *phead=(struct ListNode*)malloc(sizeof(struct ListNode));
struct ListNode *tail=phead;
phead->next=NULL;
struct ListNode* cur=head;
while(cur)
{
if(cur->val==val)
{
struct ListNode* temp=cur->next;
free(cur);
cur=temp;
}
else
{
tail->next=cur;
cur=cur->next;
tail=tail->next;
}
}
//最后这里要制成空
tail->next=NULL;
struct ListNode* newhead=phead->next;
free(phead);
return newhead;
}