(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的项(可优化?)

leetCode刷题记录的更多相关文章

  1. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  2. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  3. LeetCode 刷题记录(二)

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

  4. LeetCode 刷题记录

    写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...

  5. LeetCode刷题记录(python3)

    由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...

  6. leetcode 刷题记录(java)-持续更新

    最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...

  7. leetcode刷题记录——树

    递归 104.二叉树的最大深度 /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...

  8. 算法进阶之Leetcode刷题记录

    目录 引言 题目 1.两数之和 题目 解题笔记 7.反转整数 题目 解题笔记 9.回文数 题目 解题笔记 13.罗马数字转整数 题目 解题笔记 14.最长公共前缀 题目 解题笔记 20.有效的括号 题 ...

  9. leetcode刷题记录——链表

    使用java实现链表 单向链表 双向链表 单向循环链表 双向循环链表 题目记录 160.相交链表 例如以下示例中 A 和 B 两个链表相交于 c1: A: a1 → a2 c1 → c2 → c3 B ...

随机推荐

  1. 页面设计--RadioButton

    RadioButton单选控件支持多分组模式 属性如下图 设计: web显示效果图:

  2. MapReduce从HBase读写数据简单示例

    就用单词计数这个例子,需要统计的单词存在HBase中的word表,MapReduce执行的时候从word表读取数据,统计结束后将结果写入到HBase的stat表中. 1.在eclipse中建立一个ha ...

  3. (转)如何检查系统是否支持Zend Optimizer

    原文地址:http://blog.chinaunix.net/uid-25266990-id-2978539.html Zend Optimizer 主要有两个功能: 1.可以加速 PHP 脚本的执行 ...

  4. 通过weka.jar包来进行数据预处理

    前言:注意首先要将weka.jar包加载到相应的路径中去.程序中的数据也是用的weka自带的数据. 扩展:eclipse添加jar包的操作方法: 打开eclipse ,在对应的工程下右击,选择Buil ...

  5. awk 解析maps文件中的地址

    maps文件一般是这个样子: pi@raspberrypi:~ $ sudo cat /proc//maps 54b88000-54c8d000 r-xp b3: /lib/systemd/syste ...

  6. Flex 使用列表和表格

    Flex 设计了不同的控件来实现列表和表格,不仅能够将数据显示在表格和列表中,还可以实现对数据进行操纵,修改等更加强大的功能. 与列表和表格相关的控件如下所示: 列表控件(List Control): ...

  7. Decks

    Now that we have Card objects, the next step is to define a class to represent decks. Since a deck i ...

  8. oracle限制用户连接数

    查看是否启用限制配置 SQL> show parameter resource_limit; 或者 select * from v$parameterwhere name = 'resource ...

  9. 图的最短路算法 Dijkstra及其优化

    单源最短路径算法 时间复杂度O(N2) 优化后时间复杂度为O(MlogN)(M为图中的边数 所以对于稀疏图来说优化后更快) 不支持有负权的图 #include<iostream> usin ...

  10. WebService之基于REST机制的实现实例(Java版)

    REST是REpresentational State Transfer的缩写(一般中文翻译为表述性状态转移).2000年Roy Fielding博士在他的博士论文“Architectural Sty ...