Skip to content

Commit

Permalink
Merge pull request #23 from apachecn/master
Browse files Browse the repository at this point in the history
1
  • Loading branch information
xshahq committed Oct 31, 2018
2 parents 2c7d764 + 81e1964 commit 027ec4d
Show file tree
Hide file tree
Showing 14 changed files with 967 additions and 130 deletions.
94 changes: 94 additions & 0 deletions docs/Leetcode_Solutions/JavaScript/008._String_to_Integer.md
@@ -0,0 +1,94 @@
# 008. String to Integer (atoi)

**<font color=red>难度: Medium</font>**

## 刷题内容

> 原题连接
* https://leetcode.com/problems/string-to-integer-atoi/description/

> 内容描述
```
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
The string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
If the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
If no valid conversion could be performed, a zero value is returned.
Note:
Only the space character ' ' is considered as whitespace character.
Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. If the numerical value is out of the range of representable values, INT_MAX (231 − 1) or INT_MIN (−231) is returned.
```
#### Example 1:
```
Input: "42"
Output: 42
```

#### Example 2:
```
Input: " -42"
Output: -42
Explanation: The first non-whitespace character is '-', which is the minus sign.
Then take as many numerical digits as possible, which gets 42.
```
#### Example 3:
```
Input: "4193 with words"
Output: 4193
Explanation: Conversion stops at digit '3' as the next character is not a numerical digit.
```

#### Example 4:
```
Input: "words and 987"
Output: 0
Explanation: The first non-whitespace character is 'w', which is not a numerical
digit or a +/- sign. Therefore no valid conversion could be performed.
```

#### Example 5:
```
Input: "-91283472332"
Output: -2147483648
Explanation: The number "-91283472332" is out of the range of a 32-bit signed integer.
Thefore INT_MIN (−231) is returned.
```
## 解题方案

> 思路 1
******- 时间复杂度: O(1)******- 空间复杂度: O(N)******

代码:

```javascript
/**
* @param {string} str
* @return {number}
*/
var myAtoi = function(str) {
const INT_MAX = 2 ** 31 - 1;
const INT_MIN = -(2 ** 31);
str = str.match(/^\s*([-+]?\d+)/);
let strNum = str ? Number(str[0]) : 0;
if(strNum < INT_MIN ){
return INT_MIN
}else if(strNum > INT_MAX){
return INT_MAX
}else{
return strNum
}
};
```

73 changes: 67 additions & 6 deletions docs/Leetcode_Solutions/Python/027._Remove_Element.md
@@ -1,15 +1,46 @@
### 27. Remove Element
# 27. Remove Element

**<font color=red>难度: Easy</font>**

## 刷题内容

题目:
<https://leetcode.com/problems/remove-element/>
> 原题连接
* https://leetcode.com/problems/remove-element/

难度:
Easy
> 内容描述
瞬秒
```
Given an array nums and a value val, 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 1:
Given nums = [3,2,2,3], val = 3,
Your function should return length = 2, with the first two elements of nums being 2.
It doesn't matter what you leave beyond the returned length.
Example 2:
Given nums = [0,1,2,2,3,0,4,2], val = 2,
Your function should return length = 5, with the first five elements of nums containing 0, 1, 3, 0, and 4.
Note that the order of those five elements can be arbitrary.
It doesn't matter what values are set beyond the returned length.
```

## 解题方案

> 思路 1
******- 时间复杂度: O(N^2)******- 空间复杂度: O(1)******

极其暴力。。。

```python
class Solution(object):
Expand All @@ -23,3 +54,33 @@ class Solution(object):
nums.remove(val)
return len(nums)
```



> 思路 2
******- 时间复杂度: O(N)******- 空间复杂度: O(1)******

如果当前数字等于val,就把当前数字换成数组的最后一个数字,然后删除掉数组最后一个数字,这样可以实现时间O(N)了

beats 100%

```python
class Solution(object):
def removeElement(self, nums, val):
"""
:type nums: List[int]
:type val: int
:rtype: int
"""
idx = 0
while idx < len(nums):
if nums[idx] == val:
nums[idx] = nums[-1]
del nums[-1]
else:
idx += 1
return len(nums)
```


补充一下,开始我还用```nums = nums[:-1]```来代替```del nums[-1] ```,但是这道题的nums是pass by reference,所以我们切片的时候nums地址变化了,这样是不对的
47 changes: 9 additions & 38 deletions docs/Leetcode_Solutions/Python/039._combination_sum.md
Expand Up @@ -42,36 +42,7 @@ A solution set is:
## 解题方案

> 思路 1

开始打算先排序然后写个递归

beats 96.55%

```python
class Solution(object):
def combinationSum(self, candidates, target):
"""
:type candidates: List[int]
:type target: int
:rtype: List[List[int]]
"""
def helper(remain, cur_combi, candidates, idx):
if remain == 0:
res.append(cur_combi)
return
for i in range(idx, len(candidates)):
if candidates[i] > remain:
break
helper(remain-candidates[i], cur_combi+[candidates[i]], candidates, i)

candidates.sort()
res = []
helper(target, [], candidates, 0)
return res
```

> 思路 2
******- 时间复杂度: O(2^n)******- 空间复杂度: O(2^n)******

此题可以用递归拆分为子问题求解。
每一个子问题(步),有两种情况需要考虑:
Expand All @@ -91,18 +62,18 @@ class Solution(object):
:type target: int
:rtype: List[List[int]]
"""
def helper(remain, cur_combi, candidates, idx):
def helper(remain, combi, idx):
if remain < 0:
return
return
if remain == 0:
res.append(cur_combi)
res.append(combi)
return
if idx >= len(candidates):
return
helper(remain-candidates[idx], cur_combi+[candidates[idx]], candidates, idx)
helper(remain, cur_combi, candidates, idx+1)
return
helper(remain, combi, idx+1)
helper(remain-candidates[idx], combi+[candidates[idx]], idx)

res = []
helper(target, [], candidates, 0)
helper(target, [], 0)
return res
```
73 changes: 55 additions & 18 deletions docs/Leetcode_Solutions/Python/040._combination_sum_ii.md
@@ -1,18 +1,53 @@
### 40. Combination Sum II
# 40. Combination Sum II

题目:
**<font color=red>难度: Medium</font>**

<https://leetcode.com/problems/combination-sum-ii/>
## 刷题内容

> 原题连接
难度:
* https://leetcode.com/problems/combination-sum-ii/description/

Medium
> 内容描述
```
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates may only be used once in the combination.
Note:
All numbers (including target) will be positive integers.
The solution set must not contain duplicate combinations.
Example 1:
Input: candidates = [10,1,2,7,6,1,5], target = 8,
A solution set is:
[
[1, 7],
[1, 2, 5],
[2, 6],
[1, 1, 6]
]
Example 2:
Input: candidates = [2,5,2,1,2], target = 5,
A solution set is:
[
[1,2,2],
[5]
]
```

## 解题方案

> 思路 1
******- 时间复杂度: O(2^n)******- 空间复杂度: O(2^n)******


Combination Sum 已经AC,做了minor change.
- 现在不需要```set``````candidates```
- 但是递归的时候```index```要从```i+1```开始了

- 递归取了当前这个数的时候```idx```也要从```idx+1```开始了
- 要判断```combo not in res``````append``````res```中去

```python
Expand All @@ -23,17 +58,19 @@ class Solution(object):
:type target: int
:rtype: List[List[int]]
"""
def dfs(remain, combo, index):
if remain == 0 and combo not in res:
res.append(combo)
def helper(remain, combi, idx):
if remain < 0:
return
if remain == 0 and combi not in res:
res.append(combi)
return
for i in range(index, len(candidates)):
if candidates[i] > remain:
break
dfs(remain - candidates[i], combo + [candidates[i]], i+1)
candidates.sort()
res = []
dfs(target, [], 0)
return res
if idx >= len(candidates):
return
helper(remain, combi, idx+1)
helper(remain-candidates[idx], combi+[candidates[idx]], idx+1)

res = []
candidates.sort()
helper(target, [], 0)
return res
```

0 comments on commit 027ec4d

Please sign in to comment.