码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode OJ Linked List: 138题、109题和191题

时间:2015-03-27 23:36:53      阅读:380      评论:0      收藏:0      [点我收藏+]

标签:

138题:Copy List with Random Pointer

技术分享

题目分析:

本题思路1:第一步,你需要遍历一下链表,对于每个结点,你都new出一个连接在其后面。第二步,调整random指针。第三步,把复制的链表与原链表断开。时间复杂度O(N),空间复杂度O(1)。

本题思路2:第一步,仍需要遍历一下链表,对于每个结点都new出一个节点,但不连接在其后面,把这种旧节点到新结点的映射关系,存储在map中。第二步,调整random指针。时间复杂度O(N),空间复杂度O(N)。

本题思路3:第一步与前两个思路相同,但不记录random信息。第二步,对于new出的每个结点,遍历原链表一次,找到合适位置。时间复杂度O(N^2),空间复杂度O(1)。

思路1解法:

技术分享
 1 class Solution {
 2 public:
 3      RandomListNode* copyRandomList(RandomListNode *head)
 4     {
 5         if (head == NULL)
 6         {
 7             return NULL;
 8         }
 9     
10         //copy Nodes and ignore random pointers
11         RandomListNode *cur = head, *cpycur = NULL;
12         while (cur != NULL)
13         {
14             cpycur = new RandomListNode(cur->label);
15             cpycur->next = cur->next;
16             cur->next = cpycur;
17             cur = cpycur->next;
18         }
19     
20         //Adjust the random pointers
21         for (cur = head, cpycur = cur->next; cur; cur = cpycur->next, cpycur = cur != NULL ? cur->next : NULL)
22         {
23             if (cur->random != NULL)
24             {
25                 cpycur->random = cur->random->next;
26             }
27         }
28     
29         //separate the list
30         cur = head;
31         RandomListNode *cpyhead = cur->next;
32         for (cpycur = cpyhead; cur; )
33         {
34             cur->next = cpycur->next;
35             cur = cur->next;
36             cpycur->next = cur != NULL ? cur->next : NULL;
37             cpycur = cpycur->next;
38         }
39     
40         return cpyhead;
41     }
42 };
View Code

思路2解法:

技术分享
 1 class Solution {
 2 public:
 3     RandomListNode* copyRandomList(RandomListNode *head)
 4     {
 5         if (head == NULL)
 6         {
 7             return NULL;
 8         }
 9     
10         //copy Nodes and ignore random pointers
11         RandomListNode *cur = head;
12         RandomListNode dummy(-1);
13         RandomListNode *cpypre = &dummy, *cpycur = dummy.next;
14     
15         unordered_map<RandomListNode*, RandomListNode*> cpymap;
16         while (cur != NULL)
17         {
18             cpycur = new RandomListNode(cur->label);
19             cpypre->next = cpycur;
20             cpymap[cur] = cpycur;
21             cur = cur->next;
22             cpypre = cpypre->next;
23         }
24     
25         //Adjust random pointers
26         for (cur = head; cur; cur = cur->next)
27         {
28             cpymap[cur]->random = cpymap[cur->random];
29         }
30     
31         return dummy.next;
32     }
33 };
View Code

思路3就不提供了,这个,没什么太大价值。

 

109题:Convert Sorted List to Binary Search Tree

技术分享

题目分析:递归将链表中点作为子树的根即可。下面解法时间复杂度O(N^2),空间复杂度O(logN)。(好吧,其实我也不知道空间复杂度为啥是logN)

技术分享
 1 class Solution {
 2 public:
 3     ListNode* FindnthPos(ListNode *head, int n)
 4     {
 5         if (n <= 0)
 6         {
 7             return NULL;
 8         }
 9     
10         ListNode *p = head;
11         while (--n)
12         {
13             p = p->next;
14         }
15         return p;
16     }
17 
18     TreeNode* PutsortedListToBST(ListNode *head, int len)
19     {
20         if (len == 0)
21         {
22             return NULL;
23         }
24         if (len == 1)
25         {
26             return new TreeNode(head->val);
27         }
28     
29         TreeNode *root = new TreeNode(FindnthPos(head, len / 2 + 1)->val);
30         root->left = PutsortedListToBST(head, len / 2);
31         root->right = PutsortedListToBST(FindnthPos(head, len / 2 + 2), (len - 1) / 2);
32     
33         return root;
34     }
35 
36     TreeNode* sortedListToBST(ListNode *head)
37     {
38         int len = 0;
39         ListNode *p = head;
40         while (p != NULL)
41         {
42             len = len + 1;
43             p = p->next;
44         }
45     
46         return PutsortedListToBST(head, len);
47     }
48 };
View Code

另外一种自底向上的解法,时间复杂度能够达到O(N),空间复杂度O(logN)。

技术分享
 1 class Solution {
 2 public:
 3     TreeNode *sortedListToBST(ListNode *head) 
 4     {
 5         int len = 0;
 6         ListNode *p = head;
 7         while (p) {
 8             len++;
 9             p = p->next;
10         }
11         return sortedListToBST(head, 0, len - 1);
12     }
13 private:
14     TreeNode* sortedListToBST(ListNode*& list, int start, int end) 
15     {
16         if (start > end) return nullptr;
17         int mid = start + (end - start) / 2;
18         TreeNode *leftChild = sortedListToBST(list, start, mid - 1);
19         TreeNode *parent = new TreeNode(list->val);
20         parent->left = leftChild;
21         list = list->next;
22         parent->right = sortedListToBST(list, mid + 1, end);
23         return parent;
24     }
25 };
View Code

 

191题:Number of 1 Bits

技术分享

题目分析:数1的个数。这个我貌似在程序员面试宝典里看过,给出一个程序,问干啥的,答:数1的个数。哈哈…

技术分享
 1 class Solution {
 2 public:
 3     int hammingWeight(uint32_t n) 
 4     {
 5         int count = 0;
 6         while (n)
 7         {
 8             count = count + 1;
 9             n = n & (n - 1);
10         }
11         return count;  
12     }
13 };
View Code

 

________________________________________________我是分割线___________________________________________

LeetCode OJ 其它题目:

LeetCode OJ Linked List: 24题、148题和61题

LeetCode OJ Linked List: 92题、143题、147题和160题

LeetCode OJ Linked List: 19题、83题、82题和86题

LeetCode OJ Linked List: 21题、2题、141题和142题

LeetCode OJ Linked List: 138题、109题和191题

标签:

原文地址:http://www.cnblogs.com/mengwang024/p/4369838.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!