leetcode -day8 Copy List with Random Pointer Single Number I II
五一中間斷了幾天,開始繼續。。。
1、
Copy List with Random Pointer
A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
分析:劍指offer上的一道題目,分三步進行,首先復制每個鏈表結點將其連接在每個結點之后,第二步拷貝指向隨機節點的指針,第三部拆分鏈表。
代碼如下:
/*** Definition for singly-linked list with a random pointer.* struct RandomListNode {* int label;* RandomListNode *next, *random;* RandomListNode(int x) : label(x), next(NULL), random(NULL) {}* };*/ class Solution { public:RandomListNode *copyRandomList(RandomListNode *head) {if(head == NULL){return NULL;}cloneNodes(head);connectRandomNodes(head);return reconnectNodes(head);}void cloneNodes(RandomListNode *head){RandomListNode* pNode = head;while(pNode!=NULL){RandomListNode* pClonedNode = new RandomListNode(pNode->label);pClonedNode->next = pNode->next;pNode->next = pClonedNode;pNode = pClonedNode->next;}}void connectRandomNodes(RandomListNode *head){RandomListNode* pNode = head;while(pNode!=NULL){RandomListNode* pClonedNode = pNode->next;RandomListNode* pRandomNode = pNode->random;if(pRandomNode != NULL){pClonedNode->random = pRandomNode->next;}pNode = pClonedNode->next;}}RandomListNode* reconnectNodes(RandomListNode *head){RandomListNode* pNode = head;RandomListNode* pNextNode = pNode->next;RandomListNode* pClonedHead = pNode->next;while(pNode!=NULL && pNextNode!=NULL){pNode->next = pNextNode->next;pNode = pNextNode;pNextNode = pNextNode->next;}return pClonedHead;} };2、Single Number
Given an array of integers, every element appears?twice?except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析:尋找數組中其他出現兩次只有一個數字出現一次是個很常見的題目,很簡單,所有數字亦或,最后的結果為只出現一次的數字。
代碼如下:
class Solution { public:int singleNumber(int A[], int n) {if(A==NULL || n<=0){return 0;}int result = A[0];for(int i=1;i<n;++i){result ^= A[i];}return result;} };3、Single Number II?
Given an array of integers, every element appears?three?times except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
分析:除了一個數字出現一次外其余數字出現三次,兩次上面已經求過很好算,三次怎么得到0呢,想到mod 3,肯定是通過位運算來處理。那么位運算如何得到mod 3呢,只能通過模擬了。
對于除出現一次之外的所有的整數,其二進制表示中每一位1出現的次數是3的整數倍,將所有這些1清零,剩下的就是最終的數。用ones記錄到當前計算的變量為止,二進制1出現“1次”(mod 3 之后的 1)的數位。用twos記錄到當前計算的變量為止,二進制1出現“2次”(mod 3 之后的 2)的數位。當ones和twos中的某一位同時為1時表示二進制1出現3次,此時需要清零。即 用二進制模擬三進制計算 。最終ones記錄的是最終結果。 class Solution { public:int singleNumber(int A[], int n) {int ones = 0, twos = 0, threes = 0;for(int i = 0; i < n; i++){threes = twos & A[i]; //已經出現兩次并且再次出現twos = twos | ones & A[i]; //曾經出現兩次的或者曾經出現一次但是再次出現的ones = ones | A[i]; //出現一次的twos = twos & ~threes; //當某一位出現三次后,我們就從出現兩次中消除該位ones = ones & ~threes; //當某一位出現三次后,我們就從出現一次中消除該位}return ones; //twos, threes最終都為0.ones是只出現一次的數} };
4、Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Input:?numbers={2, 7, 11, 15}, target=9
Output:?index1=1, index2=2
分析:在一個數組中找兩個數,使其和為目標值,如果循環遍歷復雜度為O(n^2),可以采用先排序再用雙指針法進行查找。考慮到vector的iterator是random iterator可以使用自帶的sort進行排序,但是排序后vector的元素位置改變,不符合題意;然后考慮到將數據保存到map中,鍵值為vector中的數值,數值為vector中的索引+1(輸出索引從1開始)。
注意使用map時因為vector中的元素可以重復,所以應該使用multimap,而且stl中 的紅黑樹的end()是個空值,所以雙指針時要注意尾指針的賦值。這樣的復雜度為O(nlgn)。
代碼如下:
class Solution { public:vector<int> twoSum(vector<int> &numbers, int target) {vector<int> resultVec;if(numbers.empty()){return resultVec;}multimap<int,int> numberMap;for(int i=0; i<numbers.size(); ++i){numberMap.insert(make_pair(numbers[i],i+1));}//sort(numbers.begin(),numbers.end());multimap<int,int>::iterator iter1 = numberMap.begin();multimap<int,int>::iterator iter2 = numberMap.end();--iter2;int sum = 0;while(iter1!=iter2){sum = iter1->first+iter2->first;if(sum == target){int index1 = iter1->second;int index2 = iter2->second;if(index1>index2){swap(index1,index2);}resultVec.push_back(index1);resultVec.push_back(index2);break;}else if(sum >target){--iter2;}else{++iter1;}}return resultVec;}};總結
以上是生活随笔為你收集整理的leetcode -day8 Copy List with Random Pointer Single Number I II的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: leetcode day5 -- Reo
- 下一篇: 面试题整理19 矩阵Z字形扫描