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
#!/bin/python3 | |
import math | |
import os | |
import random | |
import re | |
import sys | |
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
#testStr = '#ab!c#de!f' | |
#testStr = '##!r#po#' | |
testStr ='#a!b#c' | |
cnt=0 | |
head = -1 | |
tail = -1 | |
ans = 0 | |
for i in range(len(testStr)): | |
if testStr[i]=='#': | |
cnt+=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
'''Ex | |
testStr = "423692" | |
kb = "923857614" | |
''' | |
'''ex1 | |
testStr = "5111" | |
kb = "752961348" | |
''' |
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
''' | |
Ex: | |
talentsCount = 3 | |
talent=[1,2,3,2,1] | |
''' | |
''' | |
talent=[1,2,3,2,1] | |
talentsCount = 3 | |
''' | |
talent=[1,1,2,2,3,1,3,2] |
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
class Solution: | |
def rotate(self, matrix: List[List[int]]) -> None: | |
""" | |
Do not return anything, modify matrix in-place instead. | |
""" | |
for i in range(len(matrix)): | |
for j in range(i+1,len(matrix)): | |
matrix[i][j],matrix[j][i] = matrix[j][i],matrix[i][j] | |
for k in range(len(matrix)): |
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
def combinations(data, length): | |
def dfs(cur, pos): | |
if len(cur) == length: | |
yield cur | |
if pos == len(data): | |
return | |
if type(data) is list: | |
for i in range(pos, len(data)): | |
for j in dfs(cur + [data[i]], i + 1): | |
yield j |
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
class Solution: | |
def countAndSay(self, n: int) -> str: | |
if n==1: | |
return "1" | |
first = 0 | |
test = str(1) | |
for j in range(2,n+1): | |
ans = "" |
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
# 0-1 Knapsack Problem 0-1 背包問題 | |
''' | |
[0, 0, 0, 0, 0, 0, 0, 0, 0] | |
[0, 0, 0, 0, 6, 6, 6, 6, 6] | |
[0, 0, 0, 0, 6, 6, 6, 6, 6] | |
[0, 0, 0, 0, 6, 6, 6, 6, 6] | |
[0, 0, 3, 3, 6, 6, 9, 9, 9] | |
[0, 0, 6, 6, 9, 9, 12, 12, 15] | |
ans: [0, 0, 6, 6, 9, 9, 12, 12, 15] | |
''' |
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
weight = [4,5,6,2,2] | |
value = [6,4,5,3,6] | |
W = 8 # 負重 | |
num = len(value) | |
# or num = len(weight) | |
# ans = 15 # 最大價值 | |
# 0 1 2 3 4 5 6 7 8 |
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
# 370. Range addition | |
updates = [[1,3,2],[2,4,3],[0,2,-2]] | |
length = 5 | |
# [-2,0,3,5,3] | |
initial_arr = [0]*length | |
for i in updates: | |
#print(i) | |
s,d,inc = i[0],i[1],i[2] | |
print(s,d,inc) |