# LeetCode

## 1.Two Sum

Q: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.

Example:

Code

Q: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.

Example:

Code

## 3.Longest Substring Without Repeating Characters

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

Example:

Code

## 5. Longest Palindromic Substring

Q:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.

Example:

①for遍历选中心元素，找齐所有中心元素（第一个while 向后 找出所有与所选中心元素重复的元素）

② l r 指示回文边界，向两遍扩展

①l r 始终指向有效边界（闭区间，考虑边界值仍然是回文区域）

②注意边界情况判断，如第一个while循环判定条件有两个，应该把边界判断放在前面，要不会出现index越界

Code

## 7. Reverse Integer

Q:Given a 32-bit signed integer, reverse digits of an integer.

Example:

①flag表示符号判断正数1负数-1

②将int转为字符串形式，[::-1]表示字符串反转

③bit_length表示位数的判断

Code

## 8. String to Integer (atoi)

Q:Implement atoi to convert a string to an integer.

Example:

1. str.strip（）用于移除首位指定字符串

2. str.isdigit()判断字符是否是数值

3. ord()将字符转换为ASCII码 chr()将ASCII码转为字符如ord(‘a’)

-> 97 chr(97) -> ‘a’4. 字符串跟数值问题注意overflow问题，这里是有符号函数所以max=231-1 min=-231

Code

## 9. Palindrome Number

Q:Determine whether an integer is a palindrome. Do this without extra space.

Example:

1.负数不是回文数、非零且末位为零不是回文数

2.用数字一半部分来比较避免了翻转数字overflow问题（赞）

Code

## 11.Container With Most Water

Q:Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Example:

Code

## 14.Longest Common Prefix

Q:Write a function to find the longest common prefix string amongst an array of strings.

Example:

Code

## 15.3 Sum

Q:Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note: The solution set must not contain duplicate triplets.

Example:

Code

## 16.3 Sum Closest

Q:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

Code

## 17.Letter Combinations of a Phone Numbert

Q:Given a digit string, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

Example:

Code

## 18.4 Sum

Q:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.

Note: The solution set must not contain duplicate quadruplets.

Example:

Code

## 19.Remove Nth Node From End of List

Q:Given a linked list, remove the nth node from the end of list and return its head.

Note:
Given n will always be valid.
Try to do this in one pass.

Example:

Code

## 20. Valid Parentheses

Q:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.

The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not..

Example:

Code

## 21. Merge Two Sorted Lists

Q: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.

Example:

1.正常思路，用and终止while，l1 或 l2 的尾巴可以一并加上

2.递归思想找准结束条件，链表一个一个拼接

Code

## 22.Generate Parentheses

Q:Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

For example, given n = 3, a solution set is:

Example:

Left right 分别为左右括号的计数器

Init_str为目前生成的字符串

Code

## 24.wap Nodes in Pairs

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

Example:

Code

## 24.wap Nodes in Pairs

For example,
Given 1->2->3->4, you should return the list as 2->1->4->3.

Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed.

Example:

Code

## 26.Remove Duplicates from Sorted Array

Q: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 by modifying the input array in-place with O(1) extra memory.

Example:

Code

## 27.Remove Element

Q:Given an array and a value, remove all instances of that value in-place and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

The order of elements can be changed. It doesn’t matter what you leave beyond the new length.

Example:

Code

## 29.Divide Two Integers

Q:Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

Example:

Code

## 31.Next Permutation

Q:Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.

If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).

The replacement must be in-place, do not allocate extra memory.

Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.

Example:

list.sort() 和 sorted(list)的区别

①找与p交换的数字时从后往前找可以避免index越界问题

②考虑有重复数字问题

③while遍历时候考虑左右边界问题（如while p and 条件 ->此为不超过左边界）

Code

## 33.Search in Rotated Sorted Array

Q:Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

You are given a target value to search. If found in the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

Example:

Code

## 34.Search for a Range

Q:Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.

Your algorithm’s runtime complexity must be in the order of O(log n).

If the target is not found in the array, return [-1, -1].

Example:

①类似while pre>0 and nums[pre] == target: 中判断while结束后的pre是否指向target(有可能因为边界条件跳出循环，并且边界仍满足target)来判断pre是否 -1

②第一个while l<=r: 中‘=’存在不构成死循环的原因如果l=r且指向target接下来会return，如果l=r不指向target之后对于mid与target判定仍会跳出循环

Code

## 35.Search Insert Position

Q:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.

You may assume no duplicates in the array.

Example:

Code

**

0%