leetCode刷题记录
(1)Linked List Cycle
Total Accepted: 13297 Total Submissions: 38411Given 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/)
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?
测试用例:单元素数组
本题的解法:
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刷题记录的更多相关文章
- leetcode刷题记录--js
leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...
- Leetcode刷题记录(python3)
Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...
- LeetCode 刷题记录(二)
写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...
- LeetCode 刷题记录
写在前面:因为要准备面试,开始了在[LeetCode]上刷题的历程.LeetCode上一共有大约150道题目,本文记录我在<http://oj.leetcode.com>上AC的所有题目, ...
- LeetCode刷题记录(python3)
由于之前对算法题接触不多,因此暂时只做easy和medium难度的题. 看完了<算法(第四版)>后重新开始刷LeetCode了,这次决定按topic来刷题,有一个大致的方向.有些题不止包含 ...
- leetcode 刷题记录(java)-持续更新
最新更新时间 11:22:29 8. String to Integer (atoi) public static int myAtoi(String str) { // 1字符串非空判断 " ...
- leetcode刷题记录——树
递归 104.二叉树的最大深度 /** * Definition for a binary tree node. * public class TreeNode { * int val; * Tree ...
- 算法进阶之Leetcode刷题记录
目录 引言 题目 1.两数之和 题目 解题笔记 7.反转整数 题目 解题笔记 9.回文数 题目 解题笔记 13.罗马数字转整数 题目 解题笔记 14.最长公共前缀 题目 解题笔记 20.有效的括号 题 ...
- leetcode刷题记录——链表
使用java实现链表 单向链表 双向链表 单向循环链表 双向循环链表 题目记录 160.相交链表 例如以下示例中 A 和 B 两个链表相交于 c1: A: a1 → a2 c1 → c2 → c3 B ...
随机推荐
- Codeforces 626C Block Towers「贪心」「二分」「数学规律」
题意: 一堆人用方块盖塔,有n个人每次只能加两块方块,有m个人每次只能加三块方块.要求每个人盖的塔的高度都不一样,保证所用方块数最少,求最高的塔的高度. 0<=n+m 0<=n,m< ...
- [HackerCup Round1 2] Autocomplete (Trie)
题目链接:https://www.facebook.com/hackercup/problems.php?pid=313229895540583&round=344496159068801 题 ...
- jstl的mavin依赖
pom.xml中加入 <!-- jstl支持 --><dependency> <groupId>javax.servlet</groupId> ...
- 【转】三种不同类型的ssh隧道
转自:http://blog.creke.net/722.html 大家都知道SSH是一种安全的传输协议,用在连接服务器上比较多.不过其实除了这个功能,它的隧道转发功能更是吸引人.下面是个人根据自己的 ...
- Spring定时任务配置
可参照http://gong1208.iteye.com/blog/1773177 的详细配置 <!-- 系统定时任务工厂类 --> <bean id="systemDes ...
- Spark1.0源码编译
编译方式一:mavenexport MAVEN_OPTS="-Xmx2g -XX:MaxPermSize=512M -XX:ReservedCodeCacheSize=512m"m ...
- 中南大学第一届长沙地区程序设计邀请赛 New Sorting Algorithm
1352: New Sorting Algorithm Time Limit: 1 Sec Memory Limit: 128 MB Description We are trying to use ...
- Java注解(Annotation)自定义注解入门
要深入学习注解,我们就必须能定义自己的注解,并使用注解,在定义自己的注解之前,我们就必须要了解Java为我们提供的元注解和相关定义注解的语法. 元注解: 元注解的作用就是负责注解其他注解.Java5. ...
- 如何激活phpstorm | phpstorm的下载
2016年7月14日 phpsotrm 推送2016.2 更新 phpstorm的下载地址 https://www.jetbrains.com/phpstorm/download/#section=w ...
- Gestures_Article_4_0
Technical Article Windows Phone 7™ Gestures Compared Lab version: 1.0.0 Last updated: August 25, 201 ...