Skip to content

Commit b8050d1

Browse files
committed
structure refactor
1 parent fe05f02 commit b8050d1

13 files changed

+90
-41
lines changed

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ LeetCode
385385
|30|[Substring with Concatenation of All Words](https://leetcode.com/problems/substring-with-concatenation-of-all-words/)| |Hard|
386386
|29|[Divide Two Integers](https://leetcode.com/problems/divide-two-integers/)| |Medium|
387387
|28|[Implement strStr()](https://leetcode.com/problems/implement-strstr/)| |Easy|
388-
|27|[Remove Element](https://leetcode.com/problems/remove-element/)| [js](./algorithms/removeElement/removeElement-js.md) |Easy|
388+
|27|[Remove Element](https://leetcode.com/problems/remove-element/)| [js](./algorithms/removeElement/removeElement.js) |Easy|
389389
|26|[Remove Duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)| |Easy|
390390
|25|[Reverse Nodes in k-Group](https://leetcode.com/problems/reverse-nodes-in-k-group/)| |Hard|
391391
|24|[Swap Nodes in Pairs](https://leetcode.com/problems/swap-nodes-in-pairs/)| |Medium|
@@ -397,8 +397,8 @@ LeetCode
397397
|18|[4Sum](https://leetcode.com/problems/4sum/)| |Medium|
398398
|17|[Letter Combinations of a Phone Number](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)| |Medium|
399399
|16|[3Sum Closest](https://leetcode.com/problems/3sum-closest/)| |Medium|
400-
|15|[3Sum](https://leetcode.com/problems/3sum/)| [js](./algorithms/3Sum/3Sum-js.md) |Medium|
401-
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [js](./algorithms/longestCommonPrefix/longestCommonPrefix-js.md) |Easy|
400+
|15|[3Sum](https://leetcode.com/problems/3sum/)| [js](./algorithms/3Sum/3Sum.js) |Medium|
401+
|14|[Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)| [js](./algorithms/longestCommonPrefix/longestCommonPrefix.js) |Easy|
402402
|13|[Roman to Integer](https://leetcode.com/problems/roman-to-integer/)| |Easy|
403403
|12|[Integer to Roman](https://leetcode.com/problems/integer-to-roman/)| |Medium|
404404
|11|[Container With Most Water](https://leetcode.com/problems/container-with-most-water/)| |Medium|
@@ -411,4 +411,4 @@ LeetCode
411411
|4|[Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)| |Hard|
412412
|3|[Longest Substring Without Repeating Characters](https://leetcode.com/problems/longest-substring-without-repeating-characters/)| |Medium|
413413
|2|[Add Two Numbers](https://leetcode.com/problems/add-two-numbers/)| |Medium|
414-
|1|[Two Sum](https://leetcode.com/problems/two-sum/) | [java](./algorithms/twoSum/twoSum-java.md), [js](./algorithms/twoSum/twoSum-js.md),[python](./algorithms/twoSum/twoSum-python.md) |Easy|
414+
|1|[Two Sum](https://leetcode.com/problems/two-sum/) | [java](./algorithms/twoSum/twoSum.java), [js](./algorithms/twoSum/twoSum.js),[python](./algorithms/twoSum/twoSum.py) |Easy|
File renamed without changes.

algorithms/3Sum/3Sum.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var threeSum = function(nums) {
2+
const result = [];
3+
4+
const len = nums.length;
5+
if (len < 3) {
6+
return result;
7+
}
8+
9+
nums = nums.sort((a, b) => a - b);
10+
for (let i = 0; i < len; i++) {
11+
// 如果当前数组大于0,则三数之和一定大于0,所以结束循环
12+
if (nums[i] > 0) break;
13+
// 重复项跳过循环
14+
if (i > 0 && nums[i] === nums[i-1]) continue;
15+
let L = i + 1;
16+
let R = len - 1;
17+
while (L < R) {
18+
const sum = nums[i] + nums[L] + nums[R];
19+
if (sum === 0) {
20+
// 跳过重复项
21+
result.push([nums[i], nums[L], nums[R]])
22+
while (L < R && nums[L] === nums[L + 1]) L++;
23+
while (L < R && nums[R] === nums[R - 1]) R--;
24+
L++;
25+
R--;
26+
}
27+
else if (sum < 0) L++;
28+
else if (sum > 0) R--;
29+
}
30+
}
31+
return result;
32+
};
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
var longestCommonPrefix = function(strs) {
2+
if (strs === null || strs.length === 0) return "";
3+
if(strs.length === 1) return strs[0]
4+
let min = 0, max = 0
5+
for(let i = 1; i < strs.length; i++) {
6+
if(strs[min] > strs[i]) min = i
7+
if(strs[max] < strs[i]) max = i
8+
}
9+
for(let j = 0; j < strs[min].length; j++) {
10+
if(strs[min].charAt(j) !== strs[max].charAt(j)) {
11+
return strs[min].substring(0, j)
12+
}
13+
}
14+
return strs[min];
15+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
var removeElement = (nums, val) => {
2+
while (nums.indexOf(val) !== -1) {
3+
nums.splice(nums.indexOf(val),1)
4+
}
5+
return nums.length
6+
}
File renamed without changes.

algorithms/twoSum/twoSum-python.md

-37
This file was deleted.
File renamed without changes.

algorithms/twoSum/twoSum.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
public int[] twoSum(int[] nums, int target) {
2+
Map<Integer, Integer> numsIndexMap = new HashMap();
3+
for (int i = 0; i < nums.length; i++) {
4+
if (numsIndexMap.containsKey(target - nums[i])) {
5+
return new int[]{numsIndexMap.get(target - nums[i]), i};
6+
}
7+
numsIndexMap.put(nums[i], i);
8+
}
9+
throw new IllegalArgumentException();
10+
}

algorithms/twoSum/twoSum.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var twoSum = function(nums, target) {
2+
const len = nums.length, map = {};
3+
for(let i = 0; i < len; i++) {
4+
const result = map[nums[i]];
5+
// 第一个数字的index为0时,其实是一个假值,需要额外处理下
6+
if (result === 0 || result) {
7+
return [result, i];
8+
}
9+
map[target - nums[i]] = i;
10+
}
11+
};

algorithms/twoSum/twoSum.py

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from typing import List
2+
3+
class Solution:
4+
def twoSum(self, nums: List[int], target: int) -> List[int]:
5+
d = {}
6+
n = len(nums)
7+
for i in range(n):
8+
if target - nums[i] in d:
9+
return d[target - nums[i]], i
10+
else:
11+
d[nums[i]] = i

0 commit comments

Comments
 (0)