(1)Linked List Cycle
Total Accepted: 13297 Total Submissions: 38411 
Given a linked list, determine if it has a cycle in it. 
 
Follow up:
Can you solve it without using extra space? 
(url:http://oj.leetcode.com/problems/linked-list-cycle/)
这一题是给定一个linkedlist判断其中是否存在环,其中关键的是如何标记某一个node曾经已经访问过。个人想到有以下几种方式:

1,如果原有数据list可以改变的话,能够在O(n)级别上实现,算法如下:

遍历该list,验证当前访问的node的next值是不是head,如果不是,则更改next引用为head,遍历的过程中一直执行该过程,只要发现有一个元素通过next引用可以访问到head,则证明该list中存在环

缺点:变量完之后链表的本身引用结构被破坏了。

下述代码accept

/**

 * Definition for singly-linked list.

 * class ListNode {

 *     int val;

 *     ListNode next;

 *     ListNode(int x) {

 *         val = x;

 *         next = null;

 *     }

 * }

 */

public class Solution {

        public boolean hasCycle(ListNode head){

        

        if(head==null){

            return false;

        }

        ListNode temp = head;

        ListNode hold = head;

        while(temp!=null){

            if(temp.next==null){

                return false;

            }

            else{

                if( temp.next==head){

                    return true;

                }

            }

            hold = temp;

            temp = temp.next;

            hold.next = head;

            

        }

        return false;

    }

}

2, 如果原有数据list中node有其他存储空间,例如有空余的属性值可以使用该属性值作为标志位来区别访问前的访问后的节点。

3,如果能够确定node里面的节点属于某一个范围。则可以利用数字的大小来做为标识位。在数的大小范围很小的时候(小于int/2),可以使用node.val+MaxInt/2来做为标识位的同时,保持原有的数据信息。在执行完后进行数据还原。(这个解决办法的复杂度为O(n),同时能保护原有数据信息)

4,暴力法,不做标记,依次遍历每一个node.next的元素是不是之前曾经访问过的,两个for循环,复杂度O(n2)。

下述代码时间在处理一个非常大的list时时间超时

/**

 * Definition for singly-linked list.

 * class ListNode {

 *     int val;

 *     ListNode next;

 *     ListNode(int x) {

 *         val = x;

 *         next = null;

 *     }

 * }

 */

public class Solution {

        public boolean hasCycle(ListNode head){

        

        if(head==null){

            return false;

        }

        ListNode temp;

        ListNode hold = head;

        while(hold!=null){

            temp = head;

            while(temp!=hold ){

                if(hold.next==null)

                {

                    return false;

                }else{

                    while(temp!=hold){

                        if(hold.next==temp){

                            return true;

                        }

                        temp = temp.next;

                    }

                    if(hold.next==hold){

                        return true;

                    }

                }

            }

            if(temp.next==hold){

                return true;

            }

            hold = hold.next;

        }

        

        return false;

    }

}

&&:本题的注意点:

测试用例,空list,单元素循环list,双元素循环list

循环list通过next访问是无限循环,所以需要设置好截至条件:返回值,断链等

(2)Linked List Cycle

(3)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?

url:http://oj.leetcode.com/problems/single-number/

测试用例:单元素数组

本题的解法:

1.利用set不存储重复数据的特性,找出所有单个数据集合,然后2*sum(set)-sum(all)

这种解法的复杂度为O(n),遍历数组,每个数组元素查询,插入HashSet,查询和插入的复杂度为O(1),所以,总体复杂度为O(n)

需要额外的存储内存,HashSet

public class Solution {

    public int singleNumber(int[] A){

        int allSum = 0;

        int numSum = 0;

        Object[] numArr;

        Set set = new HashSet();

        for(int i=0;i<A.length;i++){

            allSum += A[i];

            set.add(A[i]);

        }

        numArr = set.toArray();

        for(int i=0;i<set.size();i++){

            numSum += Integer.parseInt(numArr[i].toString());

        }

        

        return numSum*2-allSum;

    }

}

2.使用HashMap,key为值,value为次数建立HashMap的复杂度为O(n),转成list,然后根据list遍历查询次数为1的项(可优化?)

最新文章

  1. Ubuntu/Mint更换阿里云源
  2. 微信分享JS函数(原创)[已失效]
  3. CentOS搭建VPN
  4. windows 安装mysql 步骤
  5. 在Winform开发框架中实现对数据库的加密支持
  6. 动态调用webservice 接口
  7. (转)安装程序发布利器——InstallShield 2011 Limited Edition
  8. 初步认识 Web Service
  9. C# 扩展方法使用
  10. SuSE的命令安装软件 zypper
  11. Js删除数组函数
  12. Ubuntu下定时任务和自启动任务的部署
  13. python学习——读取染色体长度(五:从命令行输入染色体长度)
  14. 【论文阅读】Sequence to Sequence Learning with Neural Network
  15. 【转载】opencv实现人脸检测
  16. datepart in ssis
  17. JMeter(二十二)与其它工具对比(转载)
  18. R语言colorRampPalette函数-创建颜色梯度(渐变色)
  19. Python zipfile 编码问题
  20. Window History对象

热门文章

  1. 找不到方法&quot;Boolean System.Threading.WaitHandle.WaitOne(TimeSpan)&quot;的解决方案
  2. (转)如果知道dll文件是面向32位系统还是面向64位系统的?
  3. ee
  4. Bootstrap 3 How-To #1 下载与配置
  5. CCF真题Z型输出
  6. Android开发-API指南-&lt;meta-data&gt;
  7. 缓存之Redis
  8. SQL 实现,如果存在就更新,如果不存在就添加
  9. 正宗PC Unix实验环境
  10. Windows API学习---用户方式中的线程同步