diff --git "a/015\344\270\211\346\225\260\344\271\213\345\222\214.py" "b/015\344\270\211\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000..f72af3f --- /dev/null +++ "b/015\344\270\211\346\225\260\344\271\213\345\222\214.py" @@ -0,0 +1,16 @@ +class Solution: + def threeSum(self, nums): + length=len(nums) + ls=[] + same=[] + for i in range(length): + for j in range(i+1,length): + if 0-nums[i]-nums[j] in nums[j+1:]: + lst=[nums[i],nums[j],0-nums[i]-nums[j]] + if set(lst)not in same: + ls.append(lst) + same.append(set(lst)) + print(ls) + +p1=Solution() +print(p1.threeSum([-1,0,1,2,-1,-4])) diff --git a/1002.py b/1002.py new file mode 100644 index 0000000..6623d76 --- /dev/null +++ b/1002.py @@ -0,0 +1,17 @@ +def find_use(A): + res=[] + for i in A[0]: + flag=1 + for j in A: + if i not in j: + flag=0 + break + if flag==1: + res.append(i) + + + return res + +if __name__=="__main__": + A=["bella","label","roller"] + print(find_use(A)) diff --git a/1078.py b/1078.py new file mode 100644 index 0000000..cad6810 --- /dev/null +++ b/1078.py @@ -0,0 +1,13 @@ +import re +def find_third(s,first,second): + #查找指定字符后的单词 + dest=re.findall(f'{first}\s{second}\s\w+',s) + print(dest) + res=[] + for i in dest: + res.append(i.split()[-1]) + print(res) +s = "we will we will rock you" +first = "we" +second = "will" +find_third(s,first,second) diff --git a/118.py b/118.py new file mode 100644 index 0000000..54b878d --- /dev/null +++ b/118.py @@ -0,0 +1,15 @@ +def gene_tri(num): + if num==1: + return [[1]] + if num==2: + return [[1],[1,1]] + res=[[1],[1,1]] + for i in range(2,num): + nrow=[1] + for j in range(i-1): + nrow.append(res[i-1][j]+res[i-1][j+1]) + nrow.append(1) + res.append(nrow) + return res +if __name__=='__main__': + print(gene_tri(5)) diff --git a/1337.py b/1337.py new file mode 100644 index 0000000..fa94cfa --- /dev/null +++ b/1337.py @@ -0,0 +1,16 @@ +def sort_army(mat,k): + res=[] + for i in range(len(mat)): + res.append((mat[i].count(1),i)) + res.sort(key=lambda x:x[0]) + row=[res[i][1] for i in range(k)] + return row +mat=[[1,1,0,0,0], + [1,1,1,1,0], + [1,0,0,0,0], + [1,1,0,0,0], + [1,1,1,1,1]] +print(sort_army(mat,3)) + +mat = [[1,0,0,0], [1,1,1,1], [1,0,0,0], [1,0,0,0]] +print(sort_army(mat,2)) diff --git a/1491.py b/1491.py new file mode 100644 index 0000000..1305910 --- /dev/null +++ b/1491.py @@ -0,0 +1,6 @@ +class Solution: + def average(self, salary) -> float: + salary.sort() + return sum(salary[1:-1])/(len(salary)-2) +pl=Solution() +print(pl.average([4000,3000,1000,2000])) diff --git a/171.py b/171.py new file mode 100644 index 0000000..672cc14 --- /dev/null +++ b/171.py @@ -0,0 +1,9 @@ +def _171(s): + nums=0 + for i in range(1,len(s)+1): + num=ord(s[-i])-ord('A')+1 + nums+=num*(26**(i-1)) + return nums +if __name__=='__main__': + s="A" + print(_171(s)) diff --git a/215.py b/215.py new file mode 100644 index 0000000..236b5f2 --- /dev/null +++ b/215.py @@ -0,0 +1,8 @@ +class Solution: + #在未排序的数组中找到第 k 个最大的元素。 + #请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 + def findKthLargest(self, nums, k: int) -> int: + nums.sort() + return nums[-k] +pl=Solution() +print(pl.findKthLargest([3,2,3,1,2,4,5,5,6],4)) diff --git a/283.py b/283.py new file mode 100644 index 0000000..4d4bbba --- /dev/null +++ b/283.py @@ -0,0 +1,10 @@ +def move_list(nums:list): + #调整数组顺序 + zero=nums.count(0) #统计0的个数 + for i in range(zero): #先删去数组中的0 + nums.remove(0) + nums.sort() + for i in range(zero): #再在末尾添加相同个数的0 + nums.append(0) + return nums +print(move_list([0,1,0,3,12])) diff --git a/476.py b/476.py new file mode 100644 index 0000000..650476f --- /dev/null +++ b/476.py @@ -0,0 +1,10 @@ +def _476(n): + num='0b' + for i in bin(n)[2:]: + if i=='0': + num=num+'1' + else: + num=num+'0' + return int(num,2) +if __name__=='__main__': + print(_476(1)) diff --git a/485.py b/485.py new file mode 100644 index 0000000..eedcab4 --- /dev/null +++ b/485.py @@ -0,0 +1,16 @@ +def count_1(lst): + mcnt=0 + length=len(lst) + for i in range(length): + if lst[i]==1: + cnt=1 + for j in range(i+1,length): + if lst[j]!=1: + break + cnt+=1 + if cnt>mcnt: + mcnt=cnt + return mcnt +if __name__=='__main__': + print(count_1([1,1,0,1,1,1])) + diff --git a/561.py b/561.py new file mode 100644 index 0000000..3a3e0e9 --- /dev/null +++ b/561.py @@ -0,0 +1,12 @@ +def _561(lst): + lst.sort() + res=[] + for i in range(len(lst)//2): + tem=[lst[2*i],lst[2*i+1]] + res.append(tem) + num=0 + for i in res: + num+=min(i) + return num +if __name__=='__main__': + print(_561( [1,4,3,2])) diff --git a/575.py b/575.py new file mode 100644 index 0000000..4ee59a3 --- /dev/null +++ b/575.py @@ -0,0 +1,5 @@ +def kandy_split(lst): + return min(len(set(lst)),int(len(lst)/2)) +if __name__=='__main__': + lst=[1,1,2,3] + print(kandy_split(lst)) diff --git a/657.py b/657.py new file mode 100644 index 0000000..2a159fe --- /dev/null +++ b/657.py @@ -0,0 +1,9 @@ +def _657(s): + if s.count('U')==s.count('D') and s.count('R')==s.count('L'): + return True + else: + return False +if __name__=='__main__': + s='UD' + s='LL' + print(_657(s)) diff --git a/748.py b/748.py new file mode 100644 index 0000000..1c22833 --- /dev/null +++ b/748.py @@ -0,0 +1,17 @@ +def _748(lst,words): + import re + word=re.findall('[a-z]',lst.lower()) + words.sort(key=lambda x:len(x)) + for i in words: + flag=1 + li=i.lower() + for j in set(word): + if li.count(j)words[1][i]''' + length=len(words) + for i in range(length): + A=words[i] + for j in range(i+1,length): + B=words[j] + n=0 + mlen=min(len(A),len(B)) + while n