《leedcode习题》由会员分享,可在线阅读,更多相关《leedcode习题(25页珍藏版)》请在金锄头文库上搜索。
1、1. Two SumTotal Accepted: 242282 Total Submissions: 1007002 Difficulty: EasyGiven an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution.Example:Given nums = 2, 7, 11, 15, target = 9,Because nu
2、ms0 + nums1 = 2 + 7 = 9,return 0, 1./* Note: The returned array must be malloced, assume caller calls free().*/int* twoSum(int* nums, int numsSize, int target) int i,j;int *index = (int *)malloc(sizeof(int)*2);for(i=0;i 4 - 3) + (5 - 6 - 4)Output: 7 - 0 - 8/* Definition for singly-linked list.* stru
3、ct ListNode * int val;* struct ListNode *next;* ;*/struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) int flag=0;struct ListNode *p1,*p2,*head;p1 = (struct ListNode *)malloc(sizeof(struct ListNode);head = p1;if(l1 = NULL | l2 = NULL)return NULL;while(l1 != NULL)if(l2 != NULL)p1
4、-val = l1-val + l2-val + flag;flag = (p1-val = 10)?1:0;p1-val = (p1-val = 10)?(p1-val-10):(p1-val);l1 = l1-next;l2 = l2-next;else p1-val = l1-val + flag;flag = (p1-val = 10)?1:0;p1-val = (p1-val = 10)?(p1-val-10):(p1-val);l1 = l1-next;if(l1 !=NULL | l2 !=NULL)p2 = (struct ListNode*)malloc(sizeof(str
5、uct ListNode);p1-next = p2;p1=p1-next;while(l2 != NULL)p1-val = l2-val + flag;flag = (p1-val = 10)?1:0;p1-val = (p1-val = 10)?(p1-val-10):(p1-val);l2 = l2-next;if(l2 !=NULL)p2 = (struct ListNode*)malloc(sizeof(struct ListNode);p1-next = p2;p1=p1-next;if(flag = 1)p2 = (struct ListNode*)malloc(sizeof(
6、struct ListNode);p2-val = flag;/flag = 0;/p2-next = NULL;p1-next = p2;p2-next=NULL;return head;7. Reverse IntegerTotal Accepted: 144356 Total Submissions: 609009 Difficulty: EasyReverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321click to show spoilers.Have you t
7、hought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!If the integers last digit is 0, what should the output be? ie, cases such as 10, 100.Did you notice that the reversed integer might overflow? Assume the input is a 32-b
8、it integer, then the reverse of 1000000003 overflows. How should you handle such cases?For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.int reverse(int x) int i,j;int flag=0;char data10=0;long x_t=0;if(x=0 | x 2147483647)return 0;if(flag = 1)re
9、turn -x_t;elsereturn x_t;9. Palindrome NumberTotal Accepted: 127881 Total Submissions: 398360 Difficulty: EasyDetermine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting
10、the integer to string, note the restriction of using extra space.You could also try reversing an integer. However, if you have solved the problem Reverse Integer, you know that the reversed integer might overflow. How would you handle such case?There is a more generic way of solving this problem.boo
11、l isPalindrome(int x) int i;char s12=0;int len=0;if(x 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./* Definition for singly-linked list.* struct ListNode * int val;* struct ListNode *next
12、;* ;*/struct ListNode* removeNthFromEnd(struct ListNode* head, int n) int i=1;int len=0;struct ListNode*p1,*p2;p1=head;while(p1)len+;p1=p1-next;p1=head;if(len = 1)return NULL;if(len = n)head=head-next;return head;while(p1)if(i = (len - n)p2=p1-next; p1-next=p2-next;break;i+;p1=p1-next;return head;21
13、. Merge Two Sorted ListsTotal Accepted: 132371 Total Submissions: 369263 Difficulty: EasyMerge 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./* Definition for singly-linked list.* struct ListNode * int val;* str
14、uct ListNode *next;* ;*/struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) struct ListNode *p1,*p2,*p3,*p4,*head;int flag=1;head = l1;if(!l2)return l1;if(!l1)return l2; p1=l1;p2=p1-next;p3=l2;p4=p3-next; while(p3)while(p1)if(p3-val val & flag) /when the first value of l2 is sma
15、ller than the first value of l1 p3-next=p1;head=p3; /refresh the head pointerp1=p3;p2=p1-next;p3=p4;if(p3)p4=p3-next;flag=0; / only oncebreak;else if(p2 & p3-val = p1-val & p3-val val) / find the right position to insert l2 valuep1-next=p3;p3-next=p2;p1=p3;p2=p1-next;p3=p4;if(p3)p4=p3-next;break;elseif(p2) / find the right position to insert l2 valuep1=p2;p2=p1-next;else / insert the l2 values at the end of l1p1-next=p3;p3-next=NULL;p1=p3;p2=p1-next;p3=p4; if