mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-07 15:45:40 +08:00
更新 001.两数之和 排版格式修复
This commit is contained in:
@ -5,7 +5,7 @@
|
||||
<p align="center"><strong><a href="https://mp.weixin.qq.com/s/tqCxrMEU-ajQumL1i8im9A">参与本项目</a>,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!</strong></p>
|
||||
|
||||
|
||||
## 1. 两数之和
|
||||
# 1. 两数之和
|
||||
|
||||
[力扣题目链接](https://leetcode.cn/problems/two-sum/)
|
||||
|
||||
@ -21,11 +21,13 @@
|
||||
|
||||
所以返回 [0, 1]
|
||||
|
||||
## 算法公开课
|
||||
|
||||
**[《代码随想录》算法视频公开课](https://programmercarl.com/other/gongkaike.html):[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),相信结合视频再看本篇题解,更有助于大家对本题的理解**。
|
||||
|
||||
|
||||
## 思路
|
||||
|
||||
建议看一下我录的这期视频:[梦开始的地方,Leetcode:1.两数之和](https://www.bilibili.com/video/BV1aT41177mK),结合本题解来学习,事半功倍。
|
||||
|
||||
很明显暴力的解法是两层for循环查找,时间复杂度是O(n^2)。
|
||||
|
||||
建议大家做这道题目之前,先做一下这两道
|
||||
@ -128,8 +130,8 @@ public:
|
||||
|
||||
## 其他语言版本
|
||||
|
||||
### Java:
|
||||
|
||||
Java:
|
||||
```java
|
||||
public int[] twoSum(int[] nums, int target) {
|
||||
int[] res = new int[2];
|
||||
@ -150,8 +152,9 @@ public int[] twoSum(int[] nums, int target) {
|
||||
}
|
||||
```
|
||||
|
||||
Python:
|
||||
### Python:
|
||||
(版本一) 使用字典
|
||||
|
||||
```python
|
||||
class Solution:
|
||||
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||
@ -211,7 +214,7 @@ class Solution:
|
||||
return [i,j]
|
||||
```
|
||||
|
||||
Go:
|
||||
### Go:
|
||||
|
||||
```go
|
||||
// 暴力解法
|
||||
@ -242,7 +245,7 @@ func twoSum(nums []int, target int) []int {
|
||||
}
|
||||
```
|
||||
|
||||
Rust
|
||||
### Rust:
|
||||
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
@ -263,9 +266,7 @@ impl Solution {
|
||||
}
|
||||
}
|
||||
```
|
||||
Rust
|
||||
|
||||
```
|
||||
```rust
|
||||
use std::collections::HashMap;
|
||||
|
||||
impl Solution {
|
||||
@ -284,7 +285,7 @@ impl Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Javascript
|
||||
### Javascript:
|
||||
|
||||
```javascript
|
||||
var twoSum = function (nums, target) {
|
||||
@ -299,7 +300,7 @@ var twoSum = function (nums, target) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
### TypeScript:
|
||||
|
||||
```typescript
|
||||
function twoSum(nums: number[], target: number): number[] {
|
||||
@ -317,7 +318,7 @@ function twoSum(nums: number[], target: number): number[] {
|
||||
};
|
||||
```
|
||||
|
||||
php
|
||||
### php:
|
||||
|
||||
```php
|
||||
function twoSum(array $nums, int $target): array
|
||||
@ -337,7 +338,8 @@ function twoSum(array $nums, int $target): array
|
||||
}
|
||||
```
|
||||
|
||||
Swift:
|
||||
### Swift:
|
||||
|
||||
```swift
|
||||
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
|
||||
// 值: 下标
|
||||
@ -353,8 +355,8 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
|
||||
}
|
||||
```
|
||||
|
||||
### Scala:
|
||||
|
||||
Scala:
|
||||
```scala
|
||||
object Solution {
|
||||
// 导入包
|
||||
@ -377,7 +379,8 @@ object Solution {
|
||||
}
|
||||
```
|
||||
|
||||
C#:
|
||||
### C#:
|
||||
|
||||
```csharp
|
||||
public class Solution {
|
||||
public int[] TwoSum(int[] nums, int target) {
|
||||
@ -396,7 +399,8 @@ public class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
Dart:
|
||||
### Dart:
|
||||
|
||||
```dart
|
||||
List<int> twoSum(List<int> nums, int target) {
|
||||
var tmp = [];
|
||||
@ -411,7 +415,8 @@ List<int> twoSum(List<int> nums, int target) {
|
||||
}
|
||||
```
|
||||
|
||||
C:
|
||||
### C:
|
||||
|
||||
```c
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user