mirror of
https://github.com/prdlk/leetcode.git
synced 2026-09-16 23:16:26 +00:00
docs(docs): add Python solutions and new Two Sum documentation
This commit is contained in:
@@ -0,0 +1,47 @@
|
|||||||
|
---
|
||||||
|
title: '1. Two Sum'
|
||||||
|
description: You are given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target
|
||||||
|
sidebar:
|
||||||
|
label: 'Two Sum'
|
||||||
|
badge: 'Easy'
|
||||||
|
---
|
||||||
|
|
||||||
|
<Badge variant="accent">Hash Table</Badge>
|
||||||
|
|
||||||
|
::::warning
|
||||||
|
Can you come up with an algorithm that is less than O(n^2) time complexity?
|
||||||
|
::::
|
||||||
|
|
||||||
|
### Example 1:
|
||||||
|
- Input: `nums = [2,7,11,15], target = 9`
|
||||||
|
- Output: `[0,1]`
|
||||||
|
- Explanation: Because `nums[0] + nums[1]` == `9`, we return [0, 1].
|
||||||
|
|
||||||
|
### Example 2:
|
||||||
|
- Input: `nums = [3,2,4], target = 6`
|
||||||
|
- Output: `[1,2]`
|
||||||
|
|
||||||
|
### Example 3:
|
||||||
|
- Input: `nums = [3,3], target = 6`
|
||||||
|
- Output: `[0,1]`
|
||||||
|
|
||||||
|
### Constraints:
|
||||||
|
|
||||||
|
- `2 <= nums.length <= 10^4`
|
||||||
|
- `-10^9 <= nums[i] <= 10^9`
|
||||||
|
- `-10^9 <= target <= 10^9`
|
||||||
|
- Only one valid answer exists.
|
||||||
|
|
||||||
|
## Solution
|
||||||
|
|
||||||
|
```py
|
||||||
|
class Solution:
|
||||||
|
def twoSum(self, nums: List[int], target: int) -> List[int]:
|
||||||
|
seen = {}
|
||||||
|
for i, num in enumerate(nums):
|
||||||
|
complement = target - num
|
||||||
|
if complement in seen:
|
||||||
|
return [seen[complement], i]
|
||||||
|
seen[num] = i
|
||||||
|
return []
|
||||||
|
```
|
||||||
@@ -34,12 +34,45 @@ Notice that the solution set must not contain duplicate triplets.
|
|||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
```js
|
```py
|
||||||
/**
|
class Solution:
|
||||||
* @param {number[]} nums
|
def threeSum(self, nums: list[int]) -> list[list[int]]:
|
||||||
* @return {number[][]}
|
nums.sort()
|
||||||
*/
|
result = []
|
||||||
var threeSum = function(nums) {
|
n = len(nums)
|
||||||
|
|
||||||
};
|
for i in range(n):
|
||||||
|
# skip all zero
|
||||||
|
if i > 0 and nums[i] == nums[i - 1]:
|
||||||
|
continue
|
||||||
|
|
||||||
|
# two pointers
|
||||||
|
left = i + 1
|
||||||
|
right = n - 1
|
||||||
|
target = -nums[i]
|
||||||
|
|
||||||
|
while left < right:
|
||||||
|
current = nums[left] + nums[right]
|
||||||
|
|
||||||
|
if current == target:
|
||||||
|
result.append([nums[i], nums[left], nums[right]])
|
||||||
|
|
||||||
|
# skip duplicates
|
||||||
|
while left < right and nums[left] == nums[left + 1]:
|
||||||
|
left += 1
|
||||||
|
while left < right and nums[right] == nums[right - 1]:
|
||||||
|
right -= 1
|
||||||
|
|
||||||
|
# shift pointers
|
||||||
|
left += 1
|
||||||
|
right -= 1
|
||||||
|
|
||||||
|
# since sorted, if current < target, then move left
|
||||||
|
elif current < target:
|
||||||
|
left += 1
|
||||||
|
# since sorted, if current > target, then move right
|
||||||
|
else:
|
||||||
|
right -= 1
|
||||||
|
|
||||||
|
return result
|
||||||
```
|
```
|
||||||
|
|||||||
@@ -37,27 +37,19 @@ Your solution must use only constant extra space.
|
|||||||
|
|
||||||
## Solution
|
## Solution
|
||||||
|
|
||||||
```js
|
```py
|
||||||
/**
|
class Solution:
|
||||||
* @param {number[]} numbers
|
def twoSum(self, numbers: List[int], target: int) -> List[int]:
|
||||||
* @param {number} target
|
i = 0
|
||||||
* @return {number[]}
|
j = len(numbers) - 1
|
||||||
*/
|
|
||||||
var twoSum = function(numbers, target) {
|
|
||||||
let i = 0, j = numbers.length - 1;
|
|
||||||
|
|
||||||
while (i < j) {
|
while i < j:
|
||||||
const curr = numbers[i] + numbers[j];
|
c = numbers[i] + numbers[j]
|
||||||
if (curr === target){
|
if c == target:
|
||||||
return [i + 1, j + 1];
|
return [i + 1, j + 1]
|
||||||
}else{
|
elif c < target:
|
||||||
if (curr < target){
|
i+=1
|
||||||
i++;
|
else:
|
||||||
}else{
|
j-=1
|
||||||
j--;
|
return []
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return [];
|
|
||||||
};
|
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user