Leetcode--easy系列2
Posted claireyuancy
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Leetcode--easy系列2相关的知识,希望对你有一定的参考价值。
#14 Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
这个题求多个字符串的公共前缀,除了要考虑串空外,假设存在公共前缀,那么必然也是第一个串的前缀。
所以能够以第一个串为基准,比較其它串的前缀是否与第一个串同样。
char* longestCommonPrefix(char** strs, int strsSize) { char *s0,*si; int index;//指示比較位置 if (strsSize <= 0 || strs == NULL) return strs; if (strsSize == 1) return strs[0]; s0 = strs[0];//以第一个字符串为基准 for (int i = 1; i < strsSize; ++i) { si = strs[i]; index = 0;//每一个字符串从0位置開始比較 while (true) { if (s0[index] != si[index] || s0[index] == NULL || si[index] == NULL) { s0[index] = ‘\0‘;//对于不相等的位置,必然不是公共前缀 break; } index++; } } return strs[0]; }
#19 Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.
For example,
Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the linked list becomes 1->2->3->5.
Note:
Given n will always be valid.
Try to do this in one pass.
//0ms /** * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { int len=0,j=0; struct ListNode *p,*q; p = head;//指向第一个元素 没有头结点 //求链表长度 while(p!=NULL) { p = p->next; len++; } //边界条件 if(len-n<0) return NULL; if(len==1 &&n==1) return NULL; p = head;//指向第一个元素 q=p; //删除的元素是第一个位置,不存在q全部先讨论 if(n==len) { p =head->next; return p; } //一般条件---待删除元素位于第len-n+1个位置,倒数第n个位置 for(j=1;j<=len-n;j++) { q = p;//q指向待删除元素前一个元素第len-n个元素 p = p->next;//p指向待删除元素---第len-n+1个元素 } q->next = p->next; return head; }在leetcode discuss里面又看到第二种写法,一次遍历得到待删除节点位置
struct ListNode* removeNthFromEnd(struct ListNode* head, int n) { struct ListNode* front = head; struct ListNode* behind = head; //front后移len长度,behind后移len-n长度。behind->next即为待删除节点 while (front != NULL) { front = front->next; if (n-- < 0) behind = behind->next; } if (n == 0)//len==n head = head->next; else behind->next = behind->next->next; return head; }
#20 Valid Parentheses
Given a string containing just the characters ‘(‘
, ‘)‘
, ‘{‘
, ‘}‘
, ‘[‘
and ‘]‘
,
determine if the input string is valid.
The brackets must close in the correct order, "()"
and "()[]{}"
are
all valid but "(]"
and "([)]"
are
not.
bool isValid(char* s) { int i,top,len; char *a; char ch; len = strlen(s); a = (char *)malloc(sizeof(char)*len); top =-1; for(i=0;i<len;i++) { ch = s[i]; if(ch==‘{‘||ch==‘(‘||ch==‘[‘) a[++top] = ch; if(ch==‘]‘||ch==‘)‘||ch==‘}‘) { if(top==-1) return false; else if(ch==‘]‘&& a[top]==‘[‘) top--; else if(ch==‘}‘&& a[top]==‘{‘) top--; else if(ch==‘)‘&& a[top]==‘(‘) top--; else return false; } } if(top==-1) return true; else return false; }
#21 Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
合并两个有序链表---首先想到了归并排序最后的合并两个有序数组,写法全然类似。查了下面discuss 发现一种递归的写法。
/**4ms * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode *p,*head; head = p; if(l1==NULL) return l2; if(l2==NULL) return l1; while(l1&&l2) { if(l1->val <= l2->val) { p->next = l1; l1 = l1->next; } else { p->next = l2; l2 = l2->next; } p = p->next; } if(l1) p->next = l1; if(l2) p->next = l2; return head->next; }
/**4ms * Definition for singly-linked list. * struct ListNode { * int val; * struct ListNode *next; * }; */ struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { struct ListNode* l3; if(l1==NULL) return l2; if(l2==NULL) return l1; if(l1->val < l2->val) { l3 = l1; l3->next = mergeTwoLists(l1->next,l2); } else { l3 = l2; l3->next = mergeTwoLists(l1,l2->next); } return l3; }
以上是关于Leetcode--easy系列2的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode Easy Palindrome Number
LeetCode Easy: 33. Search in Rotated Sorted Array