LeetCode 3. Longest Substring Without Repeating Characters [English ver]

[English ver]

3. Longest Substring Without Repeating Characters

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

Example:

1
2
3
4
5
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.

First time i get a very tedious algorithm, because of i did not thinking about the problem carefully in the beginning, it causes that i hava to fix bug again and again because of the situation i haven’t think about . this makes code become very redundant .

The solution for the first time

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
public class Solution {
public int lengthOfLongestSubstring(String s) {
HashMap<String,Integer> map = new HashMap<>();
int maxSubLength = 0;
int tempLength = 0;
boolean isFirstRepeat = true;
String beginSub = "";
int bigStartNum = 0;
for (int i=0;i<s.length();i++){
beginSub = String.valueOf(s.charAt(i));
if(map.containsKey(beginSub)){
if(map.get(beginSub)>bigStartNum){
tempLength = i - map.get(beginSub);
bigStartNum = map.get(beginSub);
}else{
tempLength = i - bigStartNum;
}
if(tempLength>maxSubLength){
maxSubLength = tempLength;
}
if(isFirstRepeat){
tempLength = i;
isFirstRepeat =false;
if(tempLength>maxSubLength){
maxSubLength = tempLength;
}
}
}else{
tempLength = i - bigStartNum;
if(tempLength>maxSubLength){
maxSubLength = tempLength;
}
}
map.put(beginSub,i);
}
if((s.length()-bigStartNum)>maxSubLength){
maxSubLength = (s.length()-bigStartNum-1);
}
if(isFirstRepeat){
return s.length();
}
return maxSubLength;
}
}

Read More

LeetCode 3.最长的没有重复字符的子字符串 [Chinese ver]

[Chinese ver]

3.最长的没有重复字符的子字符串

给你一个字符串,得出最长的一个没有重复字符的子字符串的长度。

例子:

给定“abcabcbb”,答案是“abc”,长度为3。

给定“bbbbb”,答案是“b”,长度为1。

Read More

LeetCode 2. Add Two Numbers [English ver]

[English ver]

2. Add Two Numbers

Question :You are given two non-empty linked lists representing two non-negative integers. 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.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.

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

Approach 1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}

Efficiency

Read More

LeetCode 2. Add Two Numbers [Chinese ver]

[Chinese ver]

2. Add Two Numbers

问题:你将获得两个非空 linked lists来表示两个非负整数。 数字以反向的顺序存储,并且它们的每个节点包含一位数字。 将两个数字相加并将其以 linked list的形式返回。

你可以假定这两个数字不包含任何前导零(即不存在首位出现0的情况),除了数字0本身。

输入 :(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出 : 7 -> 0 -> 8

方法一:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummyHead = new ListNode(0);
ListNode p = l1, q = l2, curr = dummyHead;
int carry = 0;
while (p != null || q != null) {
int x = (p != null) ? p.val : 0;
int y = (q != null) ? q.val : 0;
int sum = carry + x + y;
carry = sum / 10;
curr.next = new ListNode(sum % 10);
curr = curr.next;
if (p != null) p = p.next;
if (q != null) q = q.next;
}
if (carry > 0) {
curr.next = new ListNode(carry);
}
return dummyHead.next;
}

Read More

LeetCode 1 Two Sum [Chinese ver]

[Chinese ver]1 Two Sum

Question

两数求和 。给定一个整数的数组,返回两个数字的索引使得这两个数字加起来成为一个指定的目标值。
你可以假设每个输入都至少有一个解决方案,并且你不能使用相同的元素两次。

Read More

LeetCode 1 Two Sum [English ver]

[English ver]1 Two Sum

Question

Two SumGiven an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.

Read More
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×