设为首页 加入收藏

TOP

Leetcode刷题(一)
2018-10-21 20:08:32 】 浏览:213
Tags:Leetcode 刷题
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;
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言中数组与指针的关系 下一篇转移Blog

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目