寻找重复数

根据题意,数组中的数字都在1~n之间,所以数字的范围是小于数组的范围的,数组的元素可以和数组的索引相联系。

例如:nums[0] = 1 即可以将nums[0]作为索引 通过nums[0] 可以访问到nums[1],以此类推。

如左图所示,环的入口就是重复元素。

那么问题就转化为了如何找到入环的第一个节点的问题。时间复杂度为O(n)

慢指针可以定义为:nums[slow] 快指针可以定义为:nums[nums[fast]]

 class Solution {
public int findDuplicate(int[] nums) {
int slow = 0;
int fast = 0;
slow = nums[slow];
fast = nums[nums[fast]];
while(slow != fast){
slow = nums[slow];
fast = nums[nums[fast]];
}
fast = 0;
while(slow != fast){
fast = nums[fast];
slow = nums[slow];
}
return slow;
}
}

【下面顺便复习一下关于链表的题】

环形链表

141. 环形链表

1. 判断是否有环的思路:使用快慢指针。快指针走两步,慢指针走一步,最终快慢指针在环上相遇,且绕圈小于两圈。
 public class Solution {
public boolean hasCycle(ListNode head) {
if(head == null || head.next == null || head.next.next == null){
return false;
}
ListNode slow = head.next;
ListNode fast = head.next.next;
while(slow != fast){
if(fast.next == null || fast.next.next == null){
return false;
}
slow = slow.next;
fast = fast.next.next;
}
return true;
}
}

142. 环形链表 II

2. 寻找入环第一个节点:使用快慢指针。快指针走两步,慢指针走一步,一起走到相遇节点后,慢指针不动,快指针回到原点,然后快慢指针再一起一步一步地走,相遇点即入环的第一个节点。

 public class Solution {
public ListNode detectCycle(ListNode head) {
if(head == null || head.next == null|| head.next.next == null){
return null;
}
ListNode slow = head.next;
ListNode fast = head.next.next;
while(slow!=fast){
if(fast.next == null || fast.next.next == null){
return null;
}
slow = slow.next;
fast = fast.next.next;
}
fast = head;
while(slow != fast){
slow = slow.next;
fast = fast.next;
}
return slow;
}
}

相交链表

3. 找出两个链表相交的第一个节点
两个链表的三种可能情况:
  1. 一个有环,一个无环:不可能相交
  2. 两个都无环:需要判断是否相交。相交,则是第一个相交节点。不相交,返回null。
  3. 两个都有环
    1. 两个各自一个环:不相交,返回null
    2. 两个共用一个环
      1. 先找入环的第一个节点 loop1和loop2
      2. 入环节点一样:loop1==loop2 可以转化为无环的相交问题,终止节点变成了loop节点。
      3. 入环节点不一样:loop1继续向下走直到遇到loop2,如果没有遇到则是两个各自一个环。返回loop1或loop2。
 
两个都无环相交的情况:
思路:如果两个链表相交,那么末尾的节点一定是同一个,因为一个节点只有一个出度。假设长链表长len1,短链表长len2,长链表先走len1-len2步,短链表从回到起点跟着一起走,相遇点即交点。
 public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode curA = headA;
ListNode curB = headB;
int lenA = 0, lenB = 0;
while(curA != null){
curA = curA.next;
lenA ++;
}
while(curB != null){
curB = curB.next;
lenB ++;
}
//A始终是长链表
curA = lenA > lenB ? headA:headB;
curB = curA == headA? headB:headA;
int del = Math.abs(lenA - lenB);
while(del > 0){
curA = curA.next;
del--;
}
while(curA!=curB){
if(curA.next == null || curB.next == null){
return null;
}
curA = curA.next;
curB = curB.next;
}
return curA;
}
}

两个都有环相交的情况:

【待续】

最新文章

  1. 【linux】linux下动态库so文件的一些认识
  2. 2016 - 1- 24 大文件下载 关于NSOutStream 的使用补充
  3. OOP多态和继承要点
  4. C++静态成员函数小结 [转]
  5. RHCA442学习笔记-Unit11内存缓存
  6. 常见makefile写法
  7. 【响应式】foundation栅格布局的“尝鲜”与“填坑”
  8. 2016-2017 ACM-ICPC, Egyptian Collegiate Programming Contest (ECPC 16) 题解
  9. APIO 2018游记
  10. Threading.Timer用法
  11. CodeForces - 896A Nephren gives a riddle
  12. 全栈性能测试修炼宝典--Jmeter实战(三)
  13. c++Builder debug DataSet Visualizer
  14. 【python】鼠标操作
  15. Integer.parseInt() 和 valueOf()
  16. 解决Sqoop报错Could not load db driver class: com.intersys.jdbc.CacheDriver
  17. c++犯过的错
  18. 初学Ionic
  19. 最新JAVA编程题全集(50题及答案)
  20. 微软Azure虚拟机备份服务在中国发布

热门文章

  1. Ansible安装部署
  2. Extmail邮件过滤和杀毒
  3. double运算的坑
  4. Apache2.4 根目录修改
  5. Floyd —Warshall(最短路及其他用法详解)
  6. MySQL 增删改查(单表)
  7. Matlab矩阵总结
  8. CC2530定时器
  9. OpenWrt R2020.3.11 去广告 抗污染 加速 PSW 无缝集成 UnPnP NAS
  10. hive元数据报错?试了很多方法都没辙?也许你漏了这一步