Skip to content

Commit 788e492

Browse files
committedFeb 11, 2022
let's go ! 2022.2.11
1 parent e653db3 commit 788e492

File tree

154 files changed

+1223
-1098
lines changed

Some content is hidden

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

154 files changed

+1223
-1098
lines changed
 

‎Daily-record/2022.1.md

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
## shell
2+
3+
### 批量修改文件名
4+
5+
想要统一文件名格式,比如批量删除前缀`leetcode`
6+
7+
```shell
8+
$ ls
9+
…………
10+
leetcode738-单调递增的数字.md
11+
leetcode746-使用最小花费爬楼梯.md
12+
leetcode75-颜色分类.md
13+
leetcode767-重构字符串.md
14+
leetcode832. 翻转图像.md
15+
leetcode834-树中距离之和.md
16+
leetcode842-将数组拆分成斐波那契序列.md
17+
leetcode860-柠檬水找零.md
18+
leetcode861-翻转矩阵后的得分.md
19+
leetcode888-公平的糖果棒交换.md
20+
leetcode922-按奇偶排序数组 II.md
21+
leetcode977-有序数组的平方.md
22+
23+
$ rename 's/leetcode//' *.md && ls
24+
……
25+
75-颜色分类.md
26+
767-重构字符串.md
27+
832. 翻转图像.md
28+
834-树中距离之和.md
29+
842-将数组拆分成斐波那契序列.md
30+
860-柠檬水找零.md
31+
861-翻转矩阵后的得分.md
32+
888-公平的糖果棒交换.md
33+
922-按奇偶排序数组 II.md
34+
977-有序数组的平方.md
35+
36+
$ rename 's/\-/\. /' *.md && ls
37+
…………
38+
75. 颜色分类.md
39+
767. 重构字符串.md
40+
832. 翻转图像.md
41+
834. 树中距离之和.md
42+
842. 将数组拆分成斐波那契序列.md
43+
860. 柠檬水找零.md
44+
861. 翻转矩阵后的得分.md
45+
888. 公平的糖果棒交换.md
46+
922. 按奇偶排序数组 II.md
47+
977. 有序数组的平方.md
48+
```
49+
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,47 @@
1-
#### [1. 两数之和](https://leetcode-cn.com/problems/two-sum/)
2-
3-
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
4-
5-
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
6-
7-
```
8-
示例:
9-
10-
给定 nums = [2, 7, 11, 15], target = 9
11-
12-
因为 nums[0] + nums[1] = 2 + 7 = 9
13-
所以返回 [0, 1]
14-
```
15-
16-
17-
18-
这道题本身如果通过暴力遍历的话也是很容易解决的,时间复杂度在 O(n2)。由于**哈希查找的时间复杂度为 O(1)**,所以可以利用哈希容器 map 降低时间复杂度
19-
20-
- 遍历数组 nums,i 为当前下标,每个值都判断map中是否存在 target-nums[i] 的 key 值
21-
- 如果存在则找到了两个值,如果不存在则将当前的 (nums[i],i) 存入 map 中,继续遍历直到找到为止
22-
23-
**动画图解:**
24-
25-
![leetcode1](image/leetcode1.gif)
26-
27-
```java
28-
class Solution {
29-
public int[] twoSum(int[] nums, int target) {
30-
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
31-
//注意:不能调用自身两次,所以不能先把所有值加入map,再判断是否contain,这样没办法避免重复调用的情况
32-
//解决办法:先判断是否contain,再加入map
33-
for (int i = 0; i < nums.length; ++i) {
34-
if (map.containsKey(target - nums[i])) {
35-
return new int[]{map.get(target - nums[i]), i};
36-
}
37-
map.put(nums[i], i);
38-
}
39-
return new int[0];
40-
}
41-
}
42-
```
43-
44-
> 时间复杂度:O(N),其中 N是数组中的元素数量。对于每一个元素 x,我们可以 O(1) 地寻找 target - x。
45-
>
46-
> 空间复杂度:O(N),其中 N 是数组中的元素数量。主要为哈希表的开销。
1+
#### [1. 两数之和](https://leetcode-cn.com/problems/two-sum/)
2+
3+
给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
4+
5+
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。
6+
7+
```
8+
示例:
9+
10+
给定 nums = [2, 7, 11, 15], target = 9
11+
12+
因为 nums[0] + nums[1] = 2 + 7 = 9
13+
所以返回 [0, 1]
14+
```
15+
16+
17+
18+
这道题本身如果通过暴力遍历的话也是很容易解决的,时间复杂度在 O(n2)。由于**哈希查找的时间复杂度为 O(1)**,所以可以利用哈希容器 map 降低时间复杂度
19+
20+
- 遍历数组 nums,i 为当前下标,每个值都判断map中是否存在 target-nums[i] 的 key 值
21+
- 如果存在则找到了两个值,如果不存在则将当前的 (nums[i],i) 存入 map 中,继续遍历直到找到为止
22+
23+
**动画图解:**
24+
25+
![leetcode1](image/leetcode1.gif)
26+
27+
```java
28+
class Solution {
29+
public int[] twoSum(int[] nums, int target) {
30+
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
31+
//注意:不能调用自身两次,所以不能先把所有值加入map,再判断是否contain,这样没办法避免重复调用的情况
32+
//解决办法:先判断是否contain,再加入map
33+
for (int i = 0; i < nums.length; ++i) {
34+
if (map.containsKey(target - nums[i])) {
35+
return new int[]{map.get(target - nums[i]), i};
36+
}
37+
map.put(nums[i], i);
38+
}
39+
return new int[0];
40+
}
41+
}
42+
```
43+
44+
> 时间复杂度:O(N),其中 N是数组中的元素数量。对于每一个元素 x,我们可以 O(1) 地寻找 target - x。
45+
>
46+
> 空间复杂度:O(N),其中 N 是数组中的元素数量。主要为哈希表的开销。
4747
>

0 commit comments

Comments
 (0)