Skip to content

Commit d04ec9e

Browse files
committed
note
1 parent 234b2bb commit d04ec9e

File tree

4 files changed

+128
-4
lines changed

4 files changed

+128
-4
lines changed

guangxu/202103/20210301.md

+72-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,81 @@
11
## Algorithm
22

3+
[119. Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii/)
4+
35
### Description
46

5-
### Solution
7+
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
8+
9+
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
10+
11+
![](assets/20210301-937f15cd.png)
12+
13+
Example 1:
14+
15+
```
16+
Input: rowIndex = 3
17+
Output: [1,3,3,1]
18+
```
19+
20+
21+
Example 2:
22+
23+
```
24+
Input: rowIndex = 0
25+
Output: [1]
26+
```
27+
628

7-
```java
29+
Example 3:
30+
31+
```
32+
Input: rowIndex = 1
33+
Output: [1,1]
34+
```
35+
36+
Constraints:
37+
38+
- 0 <= rowIndex <= 33
39+
40+
- Follow up: Could you optimize your algorithm to use only O(rowIndex) extra space?
41+
42+
### Solution
843

44+
```java
45+
class Solution {
46+
public List<Integer> getRow(int rowIndex) {
47+
rowIndex++;
48+
List<List<Integer>> lists = new ArrayList<>();
49+
if(rowIndex==0){
50+
List<Integer> list = new ArrayList<>();
51+
return list;
52+
}
53+
if(rowIndex>=1){
54+
List<Integer> list = new ArrayList<>();
55+
list.add(1);
56+
lists.add(list);
57+
}
58+
if(rowIndex>=2){
59+
List<Integer> list = new ArrayList<>();
60+
list.add(1);
61+
list.add(1);
62+
lists.add(list);
63+
}
64+
if(rowIndex>=3){
65+
for(int i=3;i<=rowIndex;i++){
66+
List<Integer> list = new ArrayList<>();
67+
List<Integer> pre = lists.get(i-1-1);
68+
list.add(1);
69+
for(int j=1;j<i-1;j++){
70+
list.add(pre.get(j-1)+pre.get(j));
71+
}
72+
list.add(1);
73+
lists.add(list);
74+
}
75+
}
76+
return lists.get(rowIndex-1);
77+
}
78+
}
979
```
1080

1181
### Discuss

guangxu/202103/20210302.md

+54-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,63 @@
11
## Algorithm
22

3+
[1. Two Sum](https://leetcode.com/problems/two-sum/)
4+
35
### Description
46

5-
### Solution
7+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
8+
9+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
10+
11+
You can return the answer in any order.
12+
13+
14+
Example 1:
15+
16+
```
17+
Input: nums = [2,7,11,15], target = 9
18+
Output: [0,1]
19+
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
20+
```
21+
22+
23+
Example 2:
624

7-
```java
25+
```
26+
Input: nums = [3,2,4], target = 6
27+
Output: [1,2]
28+
```
29+
30+
31+
Example 3:
32+
33+
```
34+
Input: nums = [3,3], target = 6
35+
Output: [0,1]
36+
```
37+
38+
Constraints:
39+
40+
- 2 <= nums.length <= 103
41+
- -109 <= nums[i] <= 109
42+
- -109 <= target <= 109
43+
- Only one valid answer exists.
44+
45+
### Solution
846

47+
```java
48+
class Solution {
49+
public int[] twoSum(int[] nums, int target) {
50+
Map<Integer, Integer> map = new HashMap<>();
51+
for(int i=0;i<nums.length;i++){
52+
int num = target - nums[i];
53+
if(map.containsKey(num)&&map.get(num)!=i){
54+
return new int[]{map.get(num),i};
55+
}
56+
map.put(nums[i], i);
57+
}
58+
throw new IllegalArgumentException("no solution!");
59+
}
60+
}
961
```
1062

1163
### Discuss
6.44 KB
Loading

guangxu/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| 标题 | Algorithm | Review | Tip | Share|
22
| :-----| :---- | :---- | :---- | :---- |
3+
| [20210302](./202103/20210302.md) |[1. Two Sum(两数之和)](https://leetcode.com/problems/two-sum/)||||
4+
| [20210301](./202103/20210301.md) |[119. Pascal's Triangle II(杨辉三角2)](https://leetcode.com/problems/pascals-triangle-ii/)||||
35
| [20210228](./202102/20210228.md) |[118. Pascal's Triangle(杨辉三角)](https://leetcode.com/problems/pascals-triangle/)||||
46
| [20210227](./202102/20210227.md) |[300. Longest Increasing Subsequence(最长递增序列)](https://leetcode.com/problems/longest-increasing-subsequence/)||||
57
| [20210226](./202102/20210226.md) |[103. Binary Tree Zigzag Level Order Traversal(之字形打印二叉树)](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/)||||

0 commit comments

Comments
 (0)