Leetcode--easy系列2,leetcode--easy系列
#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==nhead = head->next;elsebehind->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;}