五一中间断了几天,开始继续。。。

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相关推荐

  1. LeetCode 138. Copy List with Random Pointer

    LeetCode 138. Copy List with Random Pointer 参考链接:http://www.cnblogs.com/grandyang/p/4261431.html Sol ...

  2. LeetCode OJ - Copy List with Random Pointer

    题目: A linked list is given such that each node contains an additional random pointer which could poi ...

  3. 【LeetCode】Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  4. Leetcode 138 Copy List With Random Pointer

    题目描述 A linked list is given such that each node contains an additional random pointer which could po ...

  5. leetcode 138. Copy List with Random Pointer | 138. 复制带随机指针的链表(复杂链表的复制)

    题目 https://leetcode.com/problems/copy-list-with-random-pointer/ 题解 复杂链表的复制,经典问题,考察与 HashMap 的结合.注意如果 ...

  6. 138. Copy List with Random Pointer

    /** 138. Copy List with Random Pointer * 2016-5-22 by Mingyang* 要遍历两次,第一次用来找到所有的next节点,并且把新旧节点全部存在ha ...

  7. LeetCode Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

  8. [Leetcode] Copy list with random pointer 对带有任意指针的链表深度拷贝

    A linked list is given such that each node contains an additional random pointer which could point t ...

  9. LeetCode || Copy List with Random Pointer

    A linked list is given such that each node contains an additional random pointer which could point t ...

最新文章

  1. java线程池拒绝策略_Java核心知识 多线程并发 线程池原理(二十三)
  2. 从内部自用到对外服务,配置管理的演进和设计优化实践
  3. Java swing实现Visio中对直线、曲线、折线的画及拖动删除
  4. DEBIAN vim的语法高亮和自动縮进
  5. Mina2.0框架源码剖析(八)
  6. Julia程序设计2 数值类型
  7. CSS之Responsive设计和CSS3 Media Queries的结合
  8. Mybatis(13)动态sql语句if/where/foreach
  9. 共享共建会让中国的5G加速吗?
  10. day17【前台】支付案例
  11. mysql自建云盘_使用nextcloud自建私人云盘
  12. Die notwendige Evolution menschlichen Verhalten
  13. mysql备份与恢复的一些方法
  14. 拉格朗日插值多项式及其余项
  15. 土地利用转移矩阵图怎么做_土地利用转移矩阵怎么分析excel?
  16. USSD设置呼叫转移功能
  17. SM2258XT固态硬盘不认盘量产修复开卡工具,SM2258XT开卡教程
  18. php长微博,用Word一键发布长微博
  19. 接口测试工具优缺点总结
  20. c语言逻辑运算符组合,C语言的逻辑运算符

热门文章

  1. numpy.sum详解
  2. zookeeper curator 服务注册
  3. Dapper,大规模分布式系统的跟踪系统--转
  4. Java 动态代理机制分析及扩展--转
  5. 海象金服与沐金农签署战略合作协议,共谋消费金融发展 正文 财经网微评论(0人评论) 本文来源于东方网 2017-08-17 10:03:12 我要评论(0) share 快成为第一
  6. 【风控建模】风控分类模型种类(决策、排序)比较与模型评估体系(ROC/gini/KS/lift)
  7. NUXT: 视图和模板
  8. Uber创始人:一个优秀创业者应具八种特质
  9. 学习笔记:The Log(我所读过的最好的一篇分布式技术文章
  10. 李彦宏透露百度真正的护城河