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 ...
随机推荐
- nyoj 62 笨小熊
点击打开链接 笨小熊 时间限制:2000 ms | 内存限制:65535 KB 难度:2 描述 笨小熊的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到了一种方法,经试验证明,用这种方法 ...
- 针对android方法数64k的限制,square做出的努力。精简protobuf
1.早期的Dalvik VM内部使用short类型变量来标识方法的id,dex限制了程序的最大方法数是65535,如果超过最大限制,无法编译,把dex.force.jumbo=true添加到proje ...
- 在SoCEDS环境下编译和更新preloader和uboot程序的方法
在SoCEDS环境下编译和更新preloader和uboot程序的方法 前面有介绍preloader在HPS boot过程中的的作用,接下来讲述下用户在SoCEDS环境下改如何编译preloade ...
- java中的 json 处理包
Jackson 以前很火 Fastjson 阿里巴巴出品
- QWizard中运行时默认按钮显示英文问题
QWizard中运行时默认按钮在编译前设计界面的时候是显示中文的,运行的时候就变成英文了.. 后来是发现国际化的时候有问题,解决办法如下: 在main.cpp里加: QTranslator* tran ...
- Hive基础之Hive开启查询列名及行转列显示
Hive默认情况下查询结果里面是只显示值: hive> select * from click_log; OK ad_101 :: ad_102 :: ad_103 :: ad_104 :: a ...
- 解决Git报错:The current branch is not configured for pull No value for key branch.master.merge found in configuration
1.在本地工程目录找到config文件(我的是在D:\git\demo\.git):2.修改config文件内容为: [core] repositoryformatversion = 0 filemo ...
- JavaScript instanceof和typeof的区别
引用自: http://www.cnblogs.com/eoiioe/archive/2008/12/31/1366081.html instanceof和typeof都能用来判断一个变量是否为空 ...
- Android开发-API指南-<compatible-screens>
<compatible-screens> 英文原文:http://developer.android.com/guide/topics/manifest/compatible-screen ...
- cygwin中运行命令提示command not found的解决方法
在cygwin下运行ls等linux常见命令时出现“command not found”的提示,原因是环境变量没有配置好,因此只要将环境变量配置正确,即可正常使用.举例说明,cygwin安装在C盘根目 ...