Skip to content

Commit 00a1b28

Browse files
author
ninechapter-algorithm
committed
add solution file
1 parent b1ed4ab commit 00a1b28

File tree

693 files changed

+55933
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

693 files changed

+55933
-0
lines changed

题解/132-pattern.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# 132 模式
2+
> 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/132-pattern/?utm_source=sc-github-wzz)
3+
## 题目描述
4+
给你一个 n 个整数的序列 a1,a2,...,an,一个 `132` 模式是对于一个子串 ai,aj,ak,满足 `i` < `j` < `k``ai` < `ak` < `aj`。设计一个算法来检查输入的这 n 个整数的序列中是否存在132模式。
5+
`n` 会小于 `20,000`
6+
### 样例说明
7+
例1:
8+
```
9+
输入: nums = [1, 2, 3, 4]
10+
输出: False
11+
解释:
12+
没有132模式在这个序列中。
13+
```
14+
例2:
15+
```
16+
输入: nums = [3, 1, 4, 2]
17+
输出: True
18+
解释:
19+
存在132模式:[1,4,2]。
20+
```
21+
22+
### 参考代码
23+
s2代表当前次大值。
24+
从最后一个开始遍历,依次与S2做比较,若小于,直接返回true,若大于,跟新s2的值即可
25+
```java
26+
public class Solution {
27+
/**
28+
* @param nums a list of n integers
29+
* @return true if there is a 132 pattern or false
30+
*/
31+
public boolean find132pattern(int[] nums) {
32+
// Write your code here
33+
Stack<Integer> stack = new Stack<Integer>();
34+
for (int i = nums.length - 1, two = Integer.MIN_VALUE; i >= 0; i--) {
35+
if (nums[i] < two) {
36+
return true;
37+
} else {
38+
while (!stack.empty() && nums[i] > stack.peek())
39+
two = stack.pop();
40+
}
41+
stack.push(nums[i]);
42+
}
43+
return false;
44+
}
45+
}
46+
```
47+
更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz)

题解/132-patterns.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# 132-patterns
2+
> 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/132-patterns/?utm_source=sc-github-wzz)
3+
## 题目描述
4+
1776
5+
### 样例说明
6+
1776
7+
### 参考代码
8+
```java
9+
public class Solution {
10+
public boolean find132pattern(int[] nums) {
11+
if (nums == null || nums.length < 3) {
12+
return false;
13+
}
14+
15+
int[] min = new int[nums.length];
16+
min[0] = nums[0];
17+
18+
//assign the minimum value from num[0] to num [i - 1] to min[i]
19+
for (int i = 1; i < nums.length; i++) {
20+
min[i] = Math.min(nums[i - 1], min[i - 1]);
21+
}
22+
23+
Stack<Integer> stack = new Stack<>();
24+
int max = Integer.MIN_VALUE;
25+
for (int j = nums.length - 1; j >= 0; j--) {
26+
if (nums[j] > min[j]) {
27+
while (!stack.empty() && nums[j] > stack.peek()) {
28+
max = stack.pop();
29+
}
30+
if (max > min[j]) {
31+
return true;
32+
}
33+
}
34+
stack.push(nums[j]);
35+
}
36+
return false;
37+
}
38+
}
39+
```
40+
更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz)

题解/2-sum.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# 2 Sum
2+
> 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/2-sum/?utm_source=sc-github-wzz)
3+
## 题目描述
4+
1754
5+
### 样例说明
6+
1754
7+
### 参考代码
8+
```java
9+
public class Solution {
10+
public int[] twoSum(int[] nums, int target) {
11+
Map<Integer, Integer> hash = new HashMap<Integer, Integer> ();
12+
int[] result = new int[2];
13+
for (int i = 0; i < nums.length; ++i) {
14+
if (hash.containsKey(target - nums[i])) {
15+
result[0] = hash.get(target - nums[i]) + 1;
16+
result[1] = i + 1;
17+
break;
18+
}
19+
hash.put(nums[i], i);
20+
}
21+
return result;
22+
}
23+
}
24+
```
25+
更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz)

题解/3sum-closest.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# 最接近的三数之和
2+
> 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/3sum-closest/?utm_source=sc-github-wzz)
3+
## 题目描述
4+
给一个包含 *n* 个整数的数组 *S*, 找到和与给定整数 *target* 最接近的三元组,返回这三个数的和。
5+
### 样例说明
6+
例1:
7+
```
8+
输入:[2,7,11,15],3
9+
输出:20
10+
解释:
11+
2+7+11=20
12+
```
13+
14+
例2:
15+
```
16+
输入:[-1,2,1,-4],1
17+
输出:2
18+
解释:
19+
-1+2+1=2
20+
```
21+
22+
### 参考代码
23+
排序后。
24+
固定一个点,利用双指针的方式,扫描,记录答案即可。
25+
```python
26+
class Solution:
27+
"""
28+
@param numbers: Give an array numbers of n integer
29+
@param target: An integer
30+
@return: return the sum of the three integers, the sum closest target.
31+
"""
32+
def threeSumClosest(self, numbers, target):
33+
numbers.sort()
34+
ans = None
35+
for i in range(len(numbers)):
36+
left, right = i + 1, len(numbers) - 1
37+
while left < right:
38+
sum = numbers[left] + numbers[right] + numbers[i]
39+
if ans is None or abs(sum - target) < abs(ans - target):
40+
ans = sum
41+
42+
if sum <= target:
43+
left += 1
44+
else:
45+
right -= 1
46+
return ans
47+
```
48+
更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz)

题解/3sum.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# 三数之和
2+
> 题目来源: [LintCode 算法题库](https://www.lintcode.com/problem/3sum/?utm_source=sc-github-wzz)
3+
## 题目描述
4+
<p><span style="line-height: 30px;">给出一个有n个整数的数组S,在S中找到三个整数a, b, c,找到所有使得a + b + c = 0的三元组。</span><br></p>
5+
### 样例说明
6+
**例1:**
7+
```
8+
输入:[2,7,11,15]
9+
输出:[]
10+
```
11+
12+
13+
**例2:**
14+
```
15+
输入:[-1,0,1,2,-1,-4]
16+
输出:[[-1, 0, 1],[-1, -1, 2]]
17+
```
18+
19+
20+
### 参考代码
21+
暴力枚举三个数复杂度为O(N^3)
22+
先考虑2Sum的做法,假设升序数列a,对于一组解ai,aj, 另一组解ak,al
23+
必然满足 i<k j>l 或 i>k j<l, 因此我们可以用两个指针,初始时指向数列两端
24+
指向数之和大于目标值时,右指针向左移使得总和减小,反之左指针向右移
25+
由此可以用 O(N)O(N) 的复杂度解决2Sum问题,3Sum则枚举第一个数 O(N^2)O(N
26+
2
27+
)
28+
使用有序数列的好处是,在枚举和移动指针时值相等的数可以跳过,省去去重部分
29+
```java
30+
public class Solution {
31+
/**
32+
* @param nums : Give an array numbers of n integer
33+
* @return : Find all unique triplets in the array which gives the sum of zero.
34+
*/
35+
public List<List<Integer>> threeSum(int[] nums) {
36+
List<List<Integer>> results = new ArrayList<>();
37+
38+
if (nums == null || nums.length < 3) {
39+
return results;
40+
}
41+
42+
Arrays.sort(nums);
43+
44+
for (int i = 0; i < nums.length - 2; i++) {
45+
// skip duplicate triples with the same first numebr
46+
if (i > 0 && nums[i] == nums[i - 1]) {
47+
continue;
48+
}
49+
50+
int left = i + 1, right = nums.length - 1;
51+
int target = -nums[i];
52+
53+
twoSum(nums, left, right, target, results);
54+
}
55+
56+
return results;
57+
}
58+
59+
public void twoSum(int[] nums,
60+
int left,
61+
int right,
62+
int target,
63+
List<List<Integer>> results) {
64+
while (left < right) {
65+
if (nums[left] + nums[right] == target) {
66+
ArrayList<Integer> triple = new ArrayList<>();
67+
triple.add(-target);
68+
triple.add(nums[left]);
69+
triple.add(nums[right]);
70+
results.add(triple);
71+
72+
left++;
73+
right--;
74+
// skip duplicate pairs with the same left
75+
while (left < right && nums[left] == nums[left - 1]) {
76+
left++;
77+
}
78+
// skip duplicate pairs with the same right
79+
while (left < right && nums[right] == nums[right + 1]) {
80+
right--;
81+
}
82+
} else if (nums[left] + nums[right] < target) {
83+
left++;
84+
} else {
85+
right--;
86+
}
87+
}
88+
}
89+
}
90+
91+
// 九章硅谷求职算法集训营版本
92+
public class Solution {
93+
/**
94+
* @param nums : Give an array numbers of n integer
95+
* @return : Find all unique triplets in the array which gives the sum of zero.
96+
*/
97+
List<List<Integer>> results = new ArrayList<>();
98+
99+
// This is soring results using 1st number as 1st key, 2nd number as 2nd key...
100+
class ListComparator<T extends Comparable<T>> implements Comparator<List<Integer>> {
101+
@Override
102+
public int compare(List<Integer> a, List<Integer> b) {
103+
for (int i = 0; i < Math.min(a.size(), b.size()); i++) {
104+
int c = a.get(i).compareTo(b.get(i));
105+
if (c != 0) {
106+
return c;
107+
}
108+
}
109+
return Integer.compare(a.size(), b.size());
110+
}
111+
}
112+
113+
public List<List<Integer>> threeSum(int[] A) {
114+
if (A == null || A.length < 3) {
115+
return results;
116+
}
117+
118+
Arrays.sort(A);
119+
// enumerate c, the maximum element
120+
int i;
121+
int n = A.length;
122+
for (i = 2; i < n; ++i) {
123+
// c is always the last in a bunch of duplicates
124+
if (i + 1 < n && A[i + 1] == A[i]) {
125+
continue;
126+
}
127+
128+
// want to find all pairs of A[j]+A[k]=-A[i], such that
129+
// j < k < i
130+
twoSum(A, i - 1, -A[i]);
131+
}
132+
133+
Collections.sort(results, new ListComparator<>());
134+
135+
return results;
136+
}
137+
138+
// find all unique pairs such that A[i]+A[j]=S, and i < j <= right
139+
private void twoSum(int[] A, int right, int S) {
140+
int i, j;
141+
j = right;
142+
for (i = 0; i <= right; ++i) {
143+
// A[i] must be the first in its duplicates
144+
if (i > 0 && A[i] == A[i - 1]) {
145+
continue;
146+
}
147+
148+
while (j > i && A[j] > S - A[i]) {
149+
--j;
150+
}
151+
152+
if (i >= j) {
153+
break;
154+
}
155+
156+
if (A[i] + A[j] == S) {
157+
List<Integer> t = new ArrayList<>();
158+
t.add(A[i]);
159+
t.add(A[j]);
160+
t.add(-S); // t.add(A[right+1])
161+
results.add(t);
162+
}
163+
}
164+
}
165+
}
166+
```
167+
更多题解请参考: [LintCode 提供 2000+ 算法题和名企阶梯训练](https://www.lintcode.com/problem/?utm_source=sc-github-wzz)

0 commit comments

Comments
 (0)