LeetCode 26 Remove Duplicates from Sorted Array

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
For example, Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the new length.


  • 用count来记录有多少个不重复的元素
  • 遇到重复的元素跳过

LeetCode 21 Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.

LeetCode 3 Longest Substring Without Repeating Characters

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


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.


比如 "abcabcbb" => "abc" =>长度为3
比如 "bbbbb" => "b" => 长度为1
比如 "pwwkew" => "wke" =>长度为3

LeetCode 7 Reverse Integer

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

Have you thought about this?

Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer's last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

The input is assumed to be a 32-bit signed integer. Your function should return 0 when the reversed integer overflows.


首先想到需要3种情况: 大于0和小于0的情况,可以用sign变量保存符号,然后在reverse。注意的是,overflow是在reverse完之后判断,因为input默认是没有overflow的.

缺点是因为用了很多javascript built-in 函数,不适用别的语言

LeetCode 1 Two Sum

Given 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.



刚开始感觉遍历所有两个元素的组合来判断是否等于目标值,需要n∗(n−1)/2 步。(O(n^2))





题目要求每个元素只能用一次,那就意味着大O是O(n). 什么样的的算法可以遍历一遍所有元素判断出来呢?虽然,我想到了应该用map(hash table) 来写,但我对map不熟,看到别人的算法才明白该怎么做。


这道题的解题关键是对hash table有很好的理解,切入点是每个元素只能用一次 ===> O(n). O(n) 意味着只能用一次for循环和大O是常数的算法,也只剩下hash table 能做到O(1)的search了。