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 ...
随机推荐
- POJ 3311 【状态压缩DP】
题意: 给n个点,给出矩阵代表i到j单向边的距离. 要求,不介意访问每个点的次数,要求访问完每个点,使得路程总和最小. 思路: 由于不介意访问每个点的次数,所以可以先进行FLOYD求出任意两个点之间的 ...
- poj 1016 Numbers That Count
点击打开链接 Numbers That Count Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 17922 Accep ...
- java的新窗体
1.JFrame窗体 jf.setSize(200, 150); jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); ...
- Js字符串与十六进制的相互转换
开发过程中,字符串与十六进.二进制之间的相互转换常常会用到,尤其是涉及到中文的加密时,就需要把中文转换为十六进制.下面说说具体的转换方法. 1.字符串转换为十六进制 主要使用 charCodeAt() ...
- Configuring HugePages for Oracle on Linux (x86-64)
Introduction Configuring HugePages Force Oracle to use HugePages (USE_LARGE_PAGES) Disabling Transpa ...
- MFC获取文本框字符串
//方法1:使用用GetDlgItem,得到控件对像, 再GetWindowText //GetDlgItem(IDC_EDIT1)->GetWindowText() //方法2:控件与对应类关 ...
- VR就是下一个浪潮_2016 (GMGC) 全球移动游戏大会观后感
"VR就是下一个浪潮" --2016 (GMGC) 全球移动游戏大会观后感 早在2014年参会Unity举办的一年一度的金立方盛典大会,就初次体验了VR头盔设备,于是印象深刻 ...
- 一个不简单的Procedure body例子
create or replace package body CountBankData_20150617 is type cursorCommon is ref cursor; --游标类型 str ...
- HTML5--》details
<details>是HTML5的新标签,用于描述文档或文档某个部分的细节. 目前只有 Chrome 和 Safari 6 支持 <details> 标签. 与 <summ ...
- SpringMVC序列化Long转成String
问题:由于JS中Number的精度为16位(最大位17位,第17位精度不准),我们的ID用的Number 18位,传到客户端会丢失最后两位: 解决方式:Long序列化成String,传到客户端: 注意 ...