Skip to content

Commit 99dba19

Browse files
committed
Add Leetcode part
1 parent b2ad165 commit 99dba19

5 files changed

Lines changed: 593 additions & 10 deletions

File tree

‎JavaKnowledge/python3入门.md‎

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ print(message)
99

1010
## 字符串
1111

12-
13-
1412
字符串就是一系列字符。在Python中,用引号括起来的都是字符串,其中的引号可以是单引号,也可以是双引号。例如:
1513
```python
1614
message = 'Hello World'
@@ -20,11 +18,13 @@ print(message)
2018
```
2119

2220
### 大小写
21+
2322
修改单词中的大小写:
2423

25-
title()方法是以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写。
26-
upper()方法将字符串改为全部大写。
27-
lower()方法将字符串改为全部小写。
24+
- title()方法是以首字母大写的方式显示每个单词,即将每个单词的首字母都改为大写。
25+
- upper()方法将字符串改为全部大写。
26+
- lower()方法将字符串改为全部小写。
27+
2828
```python
2929
message = 'hello world'
3030
print(message.title())
@@ -46,7 +46,7 @@ print(full_name)
4646

4747
### 空格
4848

49-
rstrip(): 去除字符串末尾空白。注意去除只是暂时的,等你再次访问该变量是,你会发现这个字符串仍然包含末尾空白。
49+
rstrip(): 去除字符串末尾空白。注意去除只是暂时的,等你再次访问该变量时,你会发现这个字符串仍然包含末尾空白。
5050
要永久删除这个字符串中的空白,必须将删除的结果存回到变量中:
5151

5252

@@ -55,8 +55,8 @@ print(first_name)
5555
first_name = first_name.rstrip()
5656
print(first_name)
5757

58-
lstrip(): 去除字符串开头空白
59-
strip(): 同时去除字符串两端的空白
58+
- lstrip(): 去除字符串开头空白
59+
- strip(): 同时去除字符串两端的空白
6060

6161

6262
## 整数
@@ -179,7 +179,6 @@ print(size)
179179
- 没有缩进的是for循环之外的
180180

181181

182-
-
183182
```python
184183
bicycles = ['trek', 'cannondale', 'redline', 'specialized']
185184
for bcy in bicycles:
@@ -369,7 +368,7 @@ for language in set(favorite_languages.values()):❶
369368

370369
函数input()让程序暂停运行,等待用户输入一些文本。
371370

372-
获取用户输入后,Python将其村村在一个变量中,以方便你使用。
371+
获取用户输入后,Python将其存在一个变量中,以方便你使用。
373372
```python3
374373
age = input("How old are you?")
375374
print(age)
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
1. LeetCode_两数之和
2+
===
3+
4+
给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。
5+
6+
你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。
7+
8+
你可以按任意顺序返回答案。
9+
10+
11+
12+
示例 1:
13+
14+
输入:nums = [2,7,11,15],>
15+
输出:[0,1]
16+
解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
17+
示例 2:
18+
19+
输入:nums = [3,2,4],>
20+
输出:[1,2]
21+
示例 3:
22+
23+
输入:nums = [3,3],>
24+
输出:[0,1]
25+
26+
27+
28+
提示:
29+
30+
2 <= nums.length <= 104
31+
-109 <= nums[i] <= 109
32+
-109 <= target <= 109
33+
只会存在一个有效答案
34+
35+
36+
进阶:你可以想出一个时间复杂度小于 O(n2) 的算法吗?
37+
38+
39+
### 方法一:暴力枚举
40+
41+
42+
最简单的方法就是对数组中的每一个数x,都去遍历找后面是否存在target - x的值是否存在。
43+
44+
```java
45+
class Solution {
46+
public int[] twoSum(int[] nums, int target) {
47+
int size = nums.length;
48+
49+
for (int i = 0; i < size; i++) {
50+
for (int j = i + 1; j < size; j++) {
51+
if (nums[i] + nums[j] == target) {
52+
return new int[]{i, j};
53+
}
54+
}
55+
}
56+
return new int[0];
57+
}
58+
}
59+
```
60+
61+
```c++
62+
#include <vector>
63+
using namespace std;
64+
65+
66+
class Solution {
67+
public:
68+
vector<int> twoSum(vector<int> &nums, int target) {
69+
int size = nums.size();
70+
71+
for (int i = 0; i < size; i++) {
72+
for (int j = i + 1; j < size; j++) {
73+
if (nums[i] + nums[j] == target) {
74+
return {i, j};
75+
}
76+
}
77+
}
78+
return {};
79+
}
80+
};
81+
```
82+
83+
复杂度分析:
84+
85+
- 时间复杂度:O(N²),其中N是数组中的元素数量。最坏情况下数组中任意两个数都要被匹配一次。
86+
87+
- 空间复杂度:O(1)。
88+
89+
90+
### 方法二: 哈希表
91+
92+
注意到方法一的时间复杂度较高的原因是寻找 target - x 的时间复杂度过高。
93+
94+
因此,我们需要一种更优秀的方法,能够快速寻找数组中是否存在目标元素。如果存在,我们需要找出它的索引。
95+
96+
使用哈希表,可以将寻找 target - x 的时间复杂度降低到从 O(N) 降低到 O(1)。
97+
98+
99+
---
100+
101+
如果想要快速确定某个元素是否在nums数组中,并且可以快速的获取所在下表index。
102+
我们的第一反应就是将数组维护成一个Map结构:
103+
104+
- key: 存储数组里的值
105+
- value: 存储数组的下标index
106+
107+
这样我们只需要通过target - nums[i]的值去Map中查找即可。
108+
但是这样存在一个问题,就是你需要先把数组中的值都放到Map中,需要多一步循环。
109+
110+
111+
112+
113+
---
114+
115+
其实我们可以先创建一个Map,在Map的初始化过程中什么元素都不放。
116+
117+
对于每一个 x,我们首先查询哈希表中是否存在 target - x,如果已存在就返回,如果不存在那再将 x 插入到哈希表中,即可保证不会让 x 和自己匹配。
118+
119+
这样只需要一次循环就可以了,而且Map数组不用提前初始化,在性能和内存占用率都比较低。
120+
121+
122+
```java
123+
class Solution {
124+
public int[] twoSum(int[] nums, int target) {
125+
Map<Integer, Integer> hashtable = new HashMap<Integer, Integer>();
126+
for (int i = 0; i < nums.length; ++i) {
127+
if (hashtable.containsKey(target - nums[i])) {
128+
return new int[]{hashtable.get(target - nums[i]), i};
129+
}
130+
hashtable.put(nums[i], i);
131+
}
132+
return new int[0];
133+
}
134+
}
135+
136+
```
137+
138+
```c++
139+
#include <unordered_map>
140+
#include <vector>
141+
142+
using namespace std;
143+
144+
class Solution {
145+
public:
146+
vector<int> twoSum(vector<int> &nums, int target) {
147+
unordered_map<int, int> map;
148+
for (int i = 0; i < nums.size(); i ++) {
149+
auto it = map.find(target - nums[i]);
150+
if (it != map.end()) {
151+
return {i, it->second};
152+
}
153+
154+
map[nums[i]] = i;
155+
}
156+
return {};
157+
}
158+
};
159+
```
160+
161+
162+
复杂度分析:
163+
164+
- 时间复杂度:O(N),其中 N 是数组中的元素数量。对于每一个元素 x,我们可以 O(1) 地寻找 target - x。
165+
166+
- 空间复杂度:O(N),其中 N 是数组中的元素数量。主要为哈希表的开销。
167+
168+
169+
---
170+
- 邮箱 :charon.chui@gmail.com
171+
- Good Luck!
172+
173+

0 commit comments

Comments
 (0)