Skip to content

Commit 380a0ce

Browse files
committed
add 001 027 243
1 parent 9137d02 commit 380a0ce

File tree

7 files changed

+98
-47
lines changed

7 files changed

+98
-47
lines changed

001.Two Sum/Two Sum.md renamed to 001.Two Sum/001.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
##[Two Sum](https://leetcode.com/problems/two-sum/)
1+
##tag
2+
Array Hash Table
23

4+
##Two Sum
35
Given an array of integers, find two numbers such that they add up to a specific target number.
46

57
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

001.Two Sum/Jimmy Xiang/001_update.go

Lines changed: 15 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,16 @@
1-
package main
2-
3-
import (
4-
"fmt"
5-
)
6-
7-
func twoSum(arr []int, value int) []int {
8-
9-
res := [2]int{-1, -1}
10-
11-
for i, v := range arr {
12-
13-
for j, o := range arr {
14-
15-
if i < j && (value-v) == o {
16-
17-
res[0] = i
18-
res[1] = j
19-
20-
return res[:]
21-
}
22-
}
23-
}
24-
25-
return res[:]
26-
27-
}
28-
29-
func main() {
30-
31-
arr := []int{222, 333, 11, 2, 3, 0, -2, 1, 2, 3}
32-
33-
sum := 0
34-
35-
result := twoSum(arr, sum)
36-
37-
if result[0] != -1 {
38-
39-
fmt.Printf("arr[%d]:%d + arr[%d]:%d = %d \n", result[0], arr[result[0]], result[1], arr[result[1]], sum)
40-
} else {
41-
fmt.Println("cann't find the numbers")
42-
}
43-
1+
func twoSum(nums []int, target int) []int {
2+
res := [2]int{-1,-1}
3+
4+
for i, v := range nums {
5+
for j, o := range nums {
6+
if i < j && (target-v) == o {
7+
res[0]=i
8+
res[1]=j
9+
10+
return res[:]
11+
}
12+
}
13+
}
14+
15+
return res[:]
4416
}

001.Two Sum/Jimmy Xiang/001_update.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ def twoSum(self, nums, target):
77
"""
88
d = {}
99
for i in range(len(nums)):
10-
if d.get(target - nums[i], None) == None:
10+
find = target - nums[i]
11+
if d.get(find, None) == None:
1112
d[nums[i]] = i
1213
else:
13-
return (d[target - nums[i] ], i)
14-
14+
return [d[find], i]

027.Remove Element/027.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
##tag
2+
Array Two Pointers
3+
4+
5+
##Remove Element
6+
Given an array and a value, remove all instances of that value in place and return the new length.
7+
8+
Do not allocate extra space for another array, you must do this in place with constant memory.
9+
10+
The order of elements can be changed. It doesn't matter what you leave beyond the new length.
11+
12+
Example:
13+
Given input array nums = [3,2,2,3], val = 3
14+
15+
Your function should return length = 2, with the first two elements of nums being 2.
16+
17+

027.Remove Element/jimmy Xiang/027.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution(object):
2+
def removeElement(self, nums, val):
3+
"""
4+
:type nums: List[int]
5+
:type val: int
6+
:rtype: int
7+
"""
8+
9+
num = 0
10+
11+
for i in range(len(nums)):
12+
if nums[i] == val:
13+
continue
14+
nums[num] = nums[i]
15+
num += 1
16+
17+
return num
18+
19+

243.Shortest Word Distance/243.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#tag
2+
Array
3+
4+
##Shortest Word Distance
5+
6+
Given a list of words and two words word1 and word2, return the shortest distance between these two words in the list.
7+
8+
For example,
9+
Assume that words = ["practice", "makes", "perfect", "coding", "makes"].
10+
11+
Given word1 = "coding", word2 = "practice", return 3.
12+
Given word1 = "makes", word2 = "coding", return 1.
13+
14+
Note:
15+
You may assume that word1 does not equal to word2, and word1 and word2 are both in the list.
16+
17+
18+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution:
2+
def sortestDistance(self, words, word1, word2):
3+
dist = float("inf")
4+
i, index1,index2 = 0, None, None
5+
while i < len(words):
6+
if words[i] == word1:
7+
index1 = i
8+
elif words[i] == word2:
9+
index2 = i
10+
if index1 is not None and index2 is not None:
11+
dist = min(dist, abs(index1 - index2))
12+
13+
i += 1
14+
15+
return dist
16+
17+
18+
19+
s=Solution()
20+
a=["a","b","c","d"]
21+
print s.sortestDistance(a,"c","d")
22+
23+

0 commit comments

Comments
 (0)