Leetcode题库
本博客用于记录在LeetCode网站上一些题的解答方法。具体实现方法纯属个人的一些解答,这些解答可能不是好的解答方法,记录在此,督促自己多学习多练习。
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
Example 1:
nums1 = [1, 3] nums2 = [2] The median is 2.0
Example 2:
nums1 = [1, 2] nums2 = [3, 4] The median is (2 + 3)/2 = 2.5
1 double findMedianSortedArrays(int* nums1, int nums1Size, int* nums2, int nums2Size) { 2 int *nums = NULL; 3 int totle_num = 0; 4 int mid_num = 0; 5 double mid = 0; 6 int i = 0, j = 0, k = 0; 7 8 totle_num = nums1Size + nums2Size; 9 mid_num = totle_num >> 1; 10 11 nums = (int *)malloc(sizeof(int) * (totle_num)); 12 if (nums == NULL) { 13 return -1; 14 } 15 16 for (k = 0; k < (mid_num + 1); k++) { 17 if (nums1Size == 0 || i == nums1Size) { 18 *(nums + k) = *(nums2 + j); 19 j++; 20 } else if (nums2Size == 0 || j == nums2Size) { 21 *(nums + k) = *(nums1 + i); 22 i++; 23 } else if (*(nums1 + i) <= *(nums2 + j)) { 24 *(nums + k) = *(nums1 + i); 25 i++; 26 } else if (*(nums1 + i) > *(nums2 + j)){ 27 *(nums + k) = *(nums2 + j); 28 j++; 29 } 30 } 31 32 if (totle_num % 2 == 0) { 33 mid = (double)((*(nums + (mid_num - 1)) + *(nums + mid_num))) / (double)2; 34 } else { 35 mid = *(nums + mid_num); 36 } 37 free(nums); 38 39 return mid; 40 }
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) { 9 struct ListNode *tail1 = NULL; 10 struct ListNode *tail2 = NULL; 11 struct ListNode *new = NULL;; 12 int tmp = 0; 13 14 if (NULL == l1 || NULL == l2) { 15 perror("list NULL!n"); 16 return NULL; 17 } 18 19 for (tail1 = l1, tail2 = l2; (tail1->next != NULL) || (tail2->next != NULL);) { 20 if ((tail1->next != NULL) && (tail2->next != NULL)) { 21 tail1->val += tail2->val; 22 } else if ((tail1->next != NULL) && (tail2->next == NULL)) { 23 if (tail2 != NULL) { 24 tail1->val += tail2->val; 25 } 26 } else if ((tail1->next == NULL) && (tail2->next != NULL)) { 27 if (tail1 != NULL) { 28 tail1->val += tail2->val; 29 new = (struct ListNode *)malloc(sizeof(struct ListNode)); 30 if (NULL == new) { 31 perror("malloc failed!n"); 32 return NULL; 33 } 34 tail1->next = new; 35 new->val = 0; 36 new->next = NULL; 37 } 38 } 39 40 tail1->val += tmp; 41 if (tail1->val < 10) { 42 tmp = 0; 43 } else { 44 tmp = tail1->val / 10; 45 tail1->val %= 10; 46 } 47 48 if (tail1->next != NULL) { 49 tail1 = tail1->next; 50 } 51 if (tail2->next != NULL) { 52 tail2 = tail2->next; 53 } else { 54 tail2->val = 0; 55 } 56 } 57 58 if (tail1 != NULL) { 59 if (tail2 != NULL) { 60 tail1->val += tail2->val; 61 } 62 tail1->val += tmp; 63 if (tail1->val >= 10) { 64 tmp = tail1->val / 10; 65 tail1->val %= 10; 66 new = (struct ListNode *)malloc(sizeof(struct ListNode)); 67 if (NULL == new) { 68 perror("malloc failed!n"); 69 return NULL; 70 } 71 tail1->next = new; 72 new->val = tmp; 73 new->next = NULL; 74 } 75 } 76 77 return l1; 78 }
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6]
, 5 → 2[1,3,5,6]
, 2 → 1[1,3,5,6]
, 7 → 4
[1,3,5,6]
, 0 → 01 int searchInsert(int* nums, int numsSize, int target) { 2 int *pNum = NULL; 3 int i = 0; 4 5 if (NULL == nums) { 6 return -1; 7 } 8 9 if (numsSize <= 0) { 10 return -1; 11 } 12 13 pNum = nums; 14 15 for (i = 0; i < numsSize; i++) { 16 if (*(pNum + i) >= target) { 17 return i; 18 } else { 19 if (i == numsSize - 1) { 20 return numsSize; 21 } 22 } 23 } 24 25 return -1; 26 }
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.
1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * struct ListNode *next; 6 * }; 7 */ 8 struct ListNode* mergeTwoLists(struct ListNode* l1, struct ListNode* l2) { 9 struct ListNode *tail1 = NULL; 10 struct ListNode *tail2 = NULL; 11 struct ListNode *bak_node1 = NULL; 12 struct ListNode *bak_node2 = NULL; 13 struct ListNode *bak_node = NULL; 14 15 if (l1 == NULL) { 16 return l2; 17 } 18 19 if (l2 == NULL) { 20 return l1; 21 } 22 23 // 找到第一个数据最小的链表,作为返回链表 24 if (l1->val <= l2->val) { 25 tail1 = l1; 26 tail2 = l2; 27 } else { 28 tail1 = l2; 29 tail2 = l1; 30 } 31 bak_node1 = tail1; 32 bak_node2 = tail2; 33 34 bak_node = tail1; 35 36 while (tail2 != NULL) { 37 while (tail1->val <= tail2->val) { 38 if (tail1->next == NULL) { 39 tail1->next = tail2; 40 return bak_node; 41 } 42 bak_node1 = tail1; 43 tail1 = tail1->next; 44 } 45 46 bak_node2 = tail2->next; 47 tail2->next = tail1; 48 bak_node1->next = tail2; 49 bak_node1 = bak_node1->next; 50 tail2 = bak_node2; 51 } 52 53 return bak_node; 54 }
1 bool isValid(char* s) { 2 char *pString = NULL; 3 int flag1 = 0, flag2 = 0, flag3 = 0; 4 int flag = 0; 5 6 if (s == NULL) { 7 perror("String NULL!n"); 8 return false; 9 } 10 11 pString = s; 12 13 while (*pString != '