LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters

题记

刷LeetCode第二天,今天做了两道题,速度比我想的要慢,看样子两个月的目标有点难,尽力吧。今天主要做了Add Two Numbers和Longest Substring Without Repeating Characters 两道题,下面就来看下这两道题吧。

题一:Add Two Numbers

题目内容

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

解题思路

  • LeetCode给的函数。
 /**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) { }
}
  • 题目的意思大致是:给出两个链表,这两个链表其实表示的一个倒置的非负整数,每一个节点表示非负整数的某一位上的数。比如链表2 -> 4 -> 3,其实表示的整数342。那么现在要做的是就是有这样的两个整数相加,最后返回同样规则的链表.比如 2->4->3 + 5 -> 6 ->4 = 7 -> 0 -> 8 即 342+465 = 807.
  • 这道题难度不是很大,我能想到有两种思路,第一种将每一个链表转换成十进制整数,然后整数相加,再将相加后的整数转换成链表输出,计算复杂度在2*O(n),而另一种就是直接链表相加,处理好进位就OK了,计算复杂度在O(n)。说实话,本来我以为第二种的运算时间是要小于第一种的,但是没想到结果差不多。其实当时我还想了另外一种方法,就是在进行链表转换成十进制整数以及十进制整数转换成链表时候,用16进制代替,这样就可以用移位代替除法操作,但是奈何16进制的加法一时没辙。

解题结果

方法一:

  public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
long num1 = 0;
long num2 = 0;
ListNode pl1 = l1;
ListNode pl2 = l2;
long index1 = 1;
long index2 = 1;
if ( l1 == null || l2 == null ){
return null;
} while(true){ if ( pl1 != null ){
num1 += pl1.val*index1;
pl1 = pl1.next;
index1*=10;
} if ( pl2 != null ){
num2 += pl2.val*index2;
pl2 = pl2.next;
index2*=10;
} if( pl2 == null && pl1 == null ){
break;
} } long sum = num1 + num2; ListNode head = new ListNode((int) (sum % 10));
head.next = null;
sum = sum / 10;
ListNode lsPoint = head; while( sum > 0 ){
ListNode lnSum = new ListNode((int) (sum % 10));
lsPoint.next = lnSum;
sum = sum / 10;
lsPoint = lnSum;
} return head;
}

方法二:

 public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode pl1 = l1;
ListNode pl2 = l2;
ListNode pHead = new ListNode(0);
ListNode pTravel = pHead;
int sum;
int carry = 0;
if ( l1 == null || l2 == null ){
return null;
} while( pl1 != null || pl2 != null ){
int val1 = 0;
int val2 = 0;
if ( pl1 != null){
val1 = pl1.val;
pl1 = pl1.next;
} if ( pl2 != null){
val2 = pl2.val;
pl2 = pl2.next;
} sum = val1 + val2 + carry;
ListNode newNode = new ListNode(sum % 10);
pTravel.next = newNode;
pTravel = newNode;
carry = sum / 10; } if( carry == 1 ){
ListNode newNode = new ListNode(carry);
pTravel.next = newNode;
pTravel = newNode;
}
return pHead.next;
}

两种方法的运行时间差不多,但是最短时间大概是在350ms,不知道是怎么弄出来的。

题二:Longest Substring Without Repeating Characters

题目内容

Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.

解题思路

  • LeetCode给出的函数
 public class Solution {
public int lengthOfLongestSubstring(String s) { }
}
  • 题目的意思大致是:找寻字符串S里面字符不重复的最长的子串长度。
  • 一般情况下,用两个for循环能获取最长的子串长度,计算复杂度是o(n2),想都不用想肯定通不过。因为O(n2)在遍历的时候会进行重复的遍历。

看一个例子:

S="abbdeca"。

t1="abbdeca",t1[1]==t1[2]。

t2="bbdeca",t2[0]==t2[1]。

t3="bdeca",一直扫描到最后。

t4="deca"、t5、t6、t7都同上。

我们在处理t1的时候已经扫描到了s[2],然后处理t3的时候扫描了s[2]到s[6],这两个子串已经扫描完了整个母串。

换言之,能使得子串停止扫描的位置只有两处:1.s[2];2.s[6](结尾)。

对于另一个例子S="aaab",能使子串停止扫描的位置分别是:s[1],s[2],s[3](结尾)。

  • 由此可见,之所以出现重复遍历,是因为在母串中扫描每一个字符都需要知道该字符是否在子串中出现过,如果使用遍历子串的方式那么肯定就费时了。那么有啥法子可以不遍历就知道重不重复呢,Hash表就可以实现,复杂度也变为了o(n)。同样我采用了两种方法,一种是使用Java自带的HashMap结构,另一种是自己定义数组来实现Hash。
  • 但是实际做的时候发现单单使用Hash还是不行的,需要有个指针来表示子串的起始的位置。比如字符串s="akicklac",当我扫描到第二个k,也就是s[4]是,Hash表里面存放的是a,k,i,c,s[4]已经替换了s[1]的那个k了,这是子串起始位置需要从s[0]变为s[2],否则s[6]会因为s[0]的存在而少算。

解题结果

方法一

     public int lengthOfLongestSubstring(String s) {
Map<Character,Integer> hash = new HashMap<Character,Integer>();
int num = 0;
int max = 0;
int start = 0;
for(int i = 0 ; i < s.length() ; i++ ){
Character cha = s.charAt(i);
if( hash.containsKey(cha)){
int val = hash.get(cha);
if ( val >= start) {
start = val + 1;
num = i - val - 1;
}
}
hash.put(cha, i);
num++;
max = max < num ? num : max;
}
return max;
}

方法二

     public int lengthOfLongestSubstring(String s) {
int max = 0;
int[] local = new int[256];
int start = 0;
int num = 0;
for( int i = 0 ; i < s.length(); i++ ){
Character ch = s.charAt(i);
int localValue = local[ch] ;
if ( localValue-- > start ){
start = localValue + 1;
num = i - localValue - 1;
}
local[ch] = i + 1;
num++;
max = max < num ? num : max;
}
return max;
}

LeetCode(2) || Add Two Numbers && Longest Substring Without Repeating Characters的更多相关文章

  1. 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters

    [Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...

  2. (python)leetcode刷题笔记03 Longest Substring Without Repeating Characters

    3. Longest Substring Without Repeating Characters Given a string, find the length of the longest sub ...

  3. 【LeetCode每天一题】Longest Substring Without Repeating Characters(最长无重复的字串)

    Given a string, find the length of the longest substring without repeating characters. Example 1:    ...

  4. 【Leetcode】【Medium】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  5. 【leetcode刷题笔记】Longest Substring Without Repeating Characters

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  6. Leetcode第三题《Longest Substring Without Repeating Characters》

    题目: Given a string, find the length of the longest substring without repeating characters. For examp ...

  7. LeetCode 3 Longest Substring Without Repeating Characters 解题报告

    LeetCode 第3题3 Longest Substring Without Repeating Characters 首先我们看题目要求: Given a string, find the len ...

  8. [LeetCode] Longest Substring Without Repeating Characters 最长无重复子串

    Given a string, find the length of the longest substring without repeating characters. For example, ...

  9. [LeetCode] Longest Substring Without Repeating Characters 最长无重复字符的子串

    Given a string, find the length of the longest substring without repeating characters. Example 1: In ...

随机推荐

  1. WinForm TextBox自定义扩展方法数据验证

    本文转载:http://www.cnblogs.com/gis-crazy/archive/2013/03/17/2964132.html 查看公司项目代码时,存在这样一个问题:winform界面上有 ...

  2. 草稿-Hyper-V

    Hyper-V Over SMB3.0 为Hyper-v宿主机和故障转移群集做防病毒排除 微软SMB 3.0文件共享协议新特性介绍

  3. 使用Inputstream读取文件

    在java中,能够使用InputStream对文件进行读取,就是字节流的输入.当读取文件内容进程序时,须要使用一个byte数组来进行存储,如此会有例如以下两个问题: 1.怎样建立合适大小的byte数组 ...

  4. 在Windows下使用MinGW静态编译Assimp

    使用MinGW静态编译Assimp 到了5月份了,没有写一篇日志,于是自己从知识库里面拿出一篇文章充数吧.这次将要解说怎样在Windows下使用MinGW静态编译Assimp. Assimp是眼下比較 ...

  5. Android MediaPlayer Error/Info Code

    1. 常见错误 error(-38, 0) 我觉得-38表示在当前的MediaPlayer状态下,不能运行你的操作. 详细怎样做请參考:Android MediaPlayer 另外我在其它资料中.发现 ...

  6. JSP 内置对象的四种属性范围

    在jsp页面中的对象,包括用户创建的对象(例如,javaBean对象)和JSP的隐含对象,都有一个范围属性.范围定义了在什么时间内,在哪一个JSP页面中可以访问这些对象.例如,session对象在会话 ...

  7. mysql其他函数

    mysql,,); +---------------+ ,,) | +---------------+ | +---------------+ row in set (0.22 sec) mysql) ...

  8. ctkPlugin插件系统实现项目插件式开发

    插件式开发体会: 自开始写[大话QT]系列就开始接触渲染客户端的开发,说是开发不如更多的说是维护以及重构,在接手这块的东西之前自己还有点犹豫,因为之前我一直认为客户端嘛,没什么技术含量,总是想做比较有 ...

  9. Java Interface是存放常量的最好地方吗?(转)

    虽然Inteface中默认常量就是static final 的.因此很多人就认为其实存放常量的最佳地方,effective java中就提过,不建议使用常量接口.其有一个原因就是:代码编译问题 好,我 ...

  10. ubuntu16.04 : 4: [: y: unexpected operator

    Ubuntu16.04 执行行脚本出错 在使用sh 执行脚本 出错标志 : 4: [: y: unexpected operator 原因:sh是连接到dash的,又因为dash跟bash的不兼容所以 ...