-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 8117838
Showing
32 changed files
with
765 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
root = true | ||
|
||
[*] | ||
indent_style = space | ||
indent_size = 4 | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = false | ||
insert_final_newline = false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
.DS_Store | ||
Thumbs.db | ||
db.json | ||
*.log | ||
node_modules/ | ||
public/ | ||
.deploy*/ | ||
.vscode/ | ||
package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
nodes = ('A', 'B', 'C', 'D', 'E', 'F') | ||
infinity = float("inf") # 最大 | ||
graph = { | ||
'A': {'B': 10, 'D': 30, 'E': 45}, | ||
'B': {'A': 10, 'C': 50, 'E': 40, 'F': 25}, | ||
'C': {'B': 50, 'E': 35, 'F': 15}, | ||
'D': {'A': 30, 'F': 20}, | ||
'E': {'A': 45, 'B': 40, 'C': 35, 'F': 55}, | ||
'F': {'B': 25, 'C': 15, 'D': 20, 'E': 55} | ||
} | ||
|
||
|
||
def bellman_ford(source): | ||
dist = {} | ||
p = {} | ||
for v in graph: | ||
dist[v] = infinity | ||
p[v] = None | ||
dist[source] = 0 | ||
|
||
# print(dist,p) | ||
for i in range(len(graph) - 1): | ||
for u in graph: | ||
for v in graph[u]: | ||
# print(u,v) | ||
# 比较,要小的 | ||
if dist[v] > graph[u][v] + dist[u]: | ||
dist[v] = graph[u][v] + dist[u] | ||
p[v] = u | ||
print(dist) | ||
|
||
|
||
bellman_ford('B') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Solution: | ||
def search(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: int | ||
""" | ||
low = 0 | ||
high = len(nums)-1 | ||
while low <= high: | ||
mid=(low+high)//2 | ||
if nums[low] == target: | ||
return low | ||
if nums[high] == target: | ||
return high | ||
if nums[mid] == target: | ||
return mid | ||
elif nums[mid] < target: | ||
low = mid + 1 | ||
else: | ||
high = mid - 1 | ||
return -1 | ||
|
||
|
||
# nums = [-1, 0, 3, 5, 9, 12] | ||
# target = 9 | ||
nums = [-1, 0, 3, 5, 9, 12] | ||
target = 2 | ||
print(Solution().search(nums,target)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
def find(sou, target): | ||
left = 0 | ||
right = len(sou) - 1 | ||
mid = (left + right) // 2 | ||
while left <= right: | ||
if sou[left] == target: return left | ||
if sou[right] == target: return right | ||
if sou[mid] == target: return mid | ||
if sou[left] < sou[mid]: | ||
if sou[left] < target < sou[mid]: | ||
left += 1 | ||
right = mid - 1 | ||
else: | ||
left = mid + 1 | ||
right -= 1 | ||
else: | ||
if target > sou[left] or target < sou[right]: | ||
left += 1 | ||
right = mid - 1 | ||
else: | ||
left = mid + 1 | ||
right -= 1 | ||
mid=(left+right)//2 | ||
return -1 | ||
a=[8,10,20,1,3,5,6] | ||
# print(find(a,)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
nodes = ('A', 'B', 'C', 'D', 'E', 'F') | ||
infinity = float("inf") #最大 | ||
graph = { | ||
'A': {'B': 10, 'D': 30, 'E': 45}, | ||
'B': {'A': 10, 'C': 50, 'E': 40, 'F': 25}, | ||
'C': {'B': 50, 'E': 35, 'F': 15}, | ||
'D': {'A': 30, 'F': 20}, | ||
'E': {'A': 45, 'B': 40, 'C': 35, 'F': 55}, | ||
'F': {'B': 25, 'C': 15, 'D': 20, 'E': 55} | ||
} | ||
unvisited = {node: infinity for node in nodes} | ||
visited = {} | ||
current = 'A' # 假设是 A | ||
currentDistance = 0 | ||
unvisited[current] = currentDistance | ||
# print(unvisited) | ||
while True: | ||
for neighbour, distance in graph[current].items(): | ||
if neighbour not in unvisited: | ||
# print(neighbour,unvisited) | ||
continue | ||
newDistance = currentDistance + distance | ||
if unvisited[neighbour] > newDistance: | ||
unvisited[neighbour] = newDistance | ||
visited[current] = currentDistance | ||
del unvisited[current] | ||
if not unvisited: | ||
break | ||
candidates = [node for node in unvisited.items() if node[1]] | ||
current, currentDistance = sorted(candidates, key=lambda x: x[1])[0] | ||
|
||
print(visited) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def intToRoman(self, num): | ||
|
||
m = [ | ||
['', 'M', 'MM', 'MMM'], | ||
['', 'C', 'CC', 'CCC', 'CD', 'D', 'DC', 'DCC', 'DCCC', 'CM'], | ||
['', 'X', 'XX', 'XXX', 'XL', 'L', 'LX', 'LXX', 'LXXX', 'XC'], | ||
['', 'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX'] | ||
] | ||
|
||
d = [1000, 100, 10, 1] | ||
|
||
r = '' | ||
|
||
for k, v in enumerate(d): | ||
r += m[k][num//v] | ||
num = num % v | ||
|
||
return r | ||
|
||
print(Solution().intToRoman(41)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Solution: | ||
def findMin(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
low,high=0,len(nums)-1 | ||
while low < high: | ||
mid = (low + high) // 2 | ||
if nums[mid] <= nums[high]: | ||
high = mid | ||
else: | ||
low = mid + 1 | ||
return nums[low] | ||
|
||
test=[4,5,6,7,0,1,2] | ||
test = [3, 3, 1, 3] | ||
test=[10,1,10,10,10] | ||
print(Solution().findMin(test)) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
def findMin(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
low, high = 0, len(nums) - 1 | ||
while low < high: | ||
mid = (low + high) // 2 | ||
if nums[mid] < nums[high]: | ||
high = mid | ||
elif nums[mid] > nums[high]: | ||
low = mid + 1 | ||
else: | ||
return min(self.findMin(nums[:mid + 1]), self.findMin(nums[mid + 1:])) | ||
|
||
return nums[low] | ||
|
||
print(Solution().findMin([10,1,10,10,10])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class Solution: | ||
def twoSum(self, numbers, target): | ||
""" | ||
:type numbers: List[int] | ||
:type target: int | ||
:rtype: List[int] | ||
""" | ||
low, high = 0, len(numbers) - 1 | ||
while low < high: | ||
temp=numbers[low] + numbers[high] | ||
if temp< target: | ||
low += 1 | ||
elif temp > target: | ||
high -= 1 | ||
else: | ||
return low + 1, high + 1 | ||
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class Solution(object): | ||
def titleToNumber(self, s): | ||
""" | ||
:type s: str | ||
:rtype: int | ||
""" | ||
a = len(s) | ||
sum=0 | ||
for i in s: | ||
sum += (ord(i) - 64) * 26 **(a-1) | ||
a -= 1 | ||
print(sum) | ||
|
||
Solution().titleToNumber('ZY') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class Solution: | ||
def addDigits(self, num): | ||
""" | ||
:type num: int | ||
:rtype: int | ||
""" | ||
if num <= 0: | ||
return 0 | ||
else: | ||
return((num-1)%9 +1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class Solution: | ||
def findDuplicate(self, nums): | ||
""" | ||
:type nums: List[int] | ||
:rtype: int | ||
""" | ||
|
||
|
||
|
||
# return (sum(nums)-sum(set(nums)))//(len(nums) - len(set(nums))) | ||
|
||
a = [1, 3, 4, 2, 2] | ||
print(Solution().findDuplicate(a)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# class Solution: | ||
# def nthSuperUglyNumber(self, n, primes): | ||
# """ | ||
# :type n: int | ||
# :type primes: List[int] | ||
# :rtype: int | ||
# """ | ||
# res=[1] | ||
# if n == 0: | ||
# return None | ||
# if n==1: | ||
# return res[0] | ||
# while len(res) < n: | ||
# yy = [] | ||
# for i in primes: | ||
# for j in res: | ||
# temp=i*j | ||
# if temp > res[-1]: | ||
# yy.append(temp) | ||
# break | ||
# res.append(min(yy)) | ||
# return res[-1] | ||
class Solution: | ||
def nthSuperUglyNumber(self, n, primes): | ||
""" | ||
:type n: int | ||
:type primes: List[int] | ||
:rtype: int | ||
""" | ||
candidates = [1]*len(primes) | ||
ids = [0]*len(primes) | ||
superNums = [1] | ||
nextMin = 1 | ||
for count in range(1, n) : | ||
for i in range(len(primes)) : | ||
if nextMin == candidates[i] : | ||
candidates[i] = superNums[ids[i]]*primes[i] | ||
ids[i] += 1 | ||
nextMin = min(candidates) | ||
superNums.append(nextMin) | ||
return superNums[-1] | ||
|
||
|
||
n=100000 | ||
primes=[7,19,29,37,41,47,53,59,61,79,83,89,101,103,109,127,131,137,139,157,167,179,181,199,211,229,233,239,241,251] | ||
|
||
print(Solution().nthSuperUglyNumber(n, primes)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
class Solution: | ||
def intersection(self, nums1, nums2): | ||
""" | ||
:type nums1: List[int] | ||
:type nums2: List[int] | ||
:rtype: List[int] | ||
""" | ||
numa = set(nums1) | ||
numb = set(nums2) | ||
return list(numa.intersection(numb)) | ||
|
||
|
||
# 集合的 & | ||
# nums1 = [1, 2, 2, 1] | ||
# nums2 = [2,2] | ||
nums1 = [4, 9, 5] | ||
nums2 = [9,4,9,8,4] | ||
print(Solution().intersection(nums1,nums2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class Solution: | ||
def searchInsert(self, nums, target): | ||
""" | ||
:type nums: List[int] | ||
:type target: int | ||
:rtype: int | ||
""" | ||
low, high = 0, len(nums) - 1 | ||
while low <= high: | ||
mid = (low + high) // 2 | ||
if target > nums[-1]: | ||
return len(nums) | ||
if nums[mid] < target: | ||
low = mid+1 | ||
elif target < nums[mid]: | ||
high = mid-1 | ||
else: | ||
return mid | ||
return low | ||
|
||
print(Solution().searchInsert( [1,3,5,6], 0)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class Solution: | ||
def isPerfectSquare(self, num): | ||
""" | ||
:type num: int | ||
:rtype: bool | ||
""" | ||
low, high = 0, num | ||
while low <= high: | ||
mid = (low + high) // 2 | ||
temp=mid ** 2 | ||
if temp == num: | ||
return True | ||
elif temp < num: | ||
low = mid + 1 | ||
else: | ||
high = mid - 1 | ||
return False | ||
|
||
print(Solution().isPerfectSquare(16)) |
Oops, something went wrong.