Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 46638a3

Browse files
committed
leetcode
1 parent 1cba074 commit 46638a3

6 files changed

+187
-2
lines changed

二叉树的所有路径.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#-*-coding:utf8-*-
2+
#author : Lenovo
3+
#date: 2018/10/2
4+
class TreeNode(object):
5+
def __init__(self, x):
6+
self.val = x
7+
self.left = None
8+
self.right = None
9+
10+
11+
class Solution(object):
12+
def binaryTreePaths(self, root):
13+
"""
14+
:type root: TreeNode
15+
:rtype: List[str]
16+
"""

回文素数.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#-*- coding:utf8 -*-
2+
#author : Lenovo
3+
#date: 2018/10/3
4+
5+
#回文素数 还是超出时间限制
6+
7+
#从这个数开始逐个加一判断这个数是否是回文数 和 素数 满足返回
8+
def primePalindrome(N):
9+
"""
10+
:type N: int
11+
:rtype: int
12+
"""
13+
def is_sushu(n): #判断是否是素数
14+
if n==1: return False
15+
for i in range(2,n/2+1):
16+
if n%i==0:
17+
return False
18+
return True
19+
20+
def is_huiwen(n): #回文
21+
return str(n)[::-1]==str(n)
22+
23+
tem=N
24+
while 1:
25+
if is_huiwen(tem) and is_sushu(tem):
26+
return tem
27+
else:
28+
print(tem)
29+
if len(str(tem))<=2:
30+
tem+=1
31+
continue
32+
if len(str(tem)) %2==0: #偶数位数变为奇数
33+
tem=int('1'+'0'*len(str(tem)))
34+
else: #如果是奇数位将后面的数字对应变为前面的数
35+
tem=list(str(tem))
36+
for i in range(-1,-len(tem)/2-1,-1):
37+
tem[i]=tem[-i-1]
38+
tem=int(''.join(tem)) #变为回文数
39+
if is_sushu(tem) and tem>N: return tem
40+
else: #此时为回文数 在除最高位的其他位数逐渐加 而不是只加一
41+
tem+=int('1'+'0'*(len(str(tem))/2)) #例如10000 10101 10201 10301这种变化
42+
# for i in range(1,10000): #发现除11外 所有满足条件的数 位数均为奇数
43+
# res=primePalindrome(i)
44+
# print(res)
45+
46+
#7519158
47+
res=primePalindrome(9989900)
48+
print(res)

岛屿的最大面积.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#-*- coding:utf8 -*-
2+
#author : Lenovo
3+
#date: 2018/10/4
4+
# from collections import OrderedDict #垃圾
5+
# def maxAreaOfIsland(grid):
6+
# """
7+
# :type grid: List[List[int]]
8+
# :rtype: int
9+
# """
10+
# if len(grid)==1: #变为一维的 求连续的1的个数
11+
# res=0
12+
# count=0
13+
# for i in range(len(grid[0])):
14+
# if grid[0][i]==1: count+=1
15+
# else: count=0
16+
# res=max(res,count)
17+
# return res
18+
# dic=OrderedDict()
19+
# for i in range(len(grid)): #将1的值存起来
20+
# for j in range(len(grid[0])):
21+
# if grid[i][j]==1:
22+
# dic[i,j]=1
23+
# def get_around_max(i,j): #得到该位置四周的四个元素在字典
24+
# max_x=1
25+
# count=0
26+
# for one in [[i-1,j],[i+1,j],[i,j-1],[i,j+1]]:
27+
# if (one[0],one[1]) in dic:
28+
# if dic[one[0],one[1]]>1 and dic[one[0],one[1]]>max_x:
29+
# if max_x>dic[one[0],one[1]]:
30+
# count+=1
31+
# max_x=dic[one[0],one[1]]
32+
# else:
33+
# count+=1
34+
# return max_x+count
35+
# for one in dic:
36+
# dic[one]=get_around_max(one[0],one[1])
37+
#
38+
# return dic
39+
40+
class Solution(object):
41+
def dfs(self, grid, x0, y0):
42+
s = 1
43+
n = len(grid)
44+
m = len(grid[0])
45+
46+
grid[x0][y0] = 0
47+
dire = [[0,1],[0,-1],[1,0],[-1,0]]
48+
for i in range(0,4):
49+
x = x0 + dire[i][0]
50+
y = y0 + dire[i][1]
51+
if x>=0 and x<n and y>=0 and y<m and grid[x][y] == 1:
52+
s = s + self.dfs(grid, x, y)
53+
return s
54+
55+
56+
def maxAreaOfIsland(self, grid):
57+
"""
58+
:type grid: List[List[int]]
59+
:rtype: int
60+
"""
61+
mx = 0
62+
n = len(grid)
63+
m = len(grid[0])
64+
for i in range(0, n):
65+
for j in range(0, m):
66+
if grid[i][j] == 1:
67+
mx = max(mx, self.dfs(grid, i,j))
68+
return mx
69+
70+
S=Solution()
71+
res=S.maxAreaOfIsland([[1,0,1,1],[1,0,1,1]])
72+
73+
74+
print(res)
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#-*-coding:utf8-*-
2+
#author : Lenovo
3+
#date: 2018/10/9
4+
5+
#未完成
6+
#垃圾思路 将所有可能出现的情况组合 此处利用itertools中的函数
7+
#但p很大时肯定会超时 会产生很多种组合
8+
def findAnagrams(s, p):
9+
"""
10+
:type s: str
11+
:type p: str
12+
:rtype: List[int]
13+
"""
14+
import itertools
15+
res={}
16+
17+
#这个函数可以根据一个字符串产生所有可能的组合 但是是列表形式
18+
for one in itertools.permutations(p):
19+
res[''.join(one)]=1
20+
ans=[]
21+
for i in range(len(s)-len(p)):
22+
if s[i] in p:
23+
if s[i:i+len(p)] in res:
24+
ans.append(i)
25+
return ans
26+
res=findAnagrams("cbaebabacd","abc")
27+
print(res)

构造矩形.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#-*-coding:utf8-*-
2+
#author : Lenovo
3+
#date: 2018/10/2
4+
def constructRectangle(area):
5+
"""
6+
:type area: int
7+
:rtype: List[int]
8+
"""
9+
import math
10+
l=int(math.sqrt(area))
11+
r=int(math.sqrt(area))
12+
while l*r!= area:
13+
l-=1
14+
r=area/l
15+
if r>l:
16+
l,r=r,l
17+
return [l,r]
18+
19+
res=constructRectangle(10)
20+
print(res)

格雷编码.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#-*-coding:utf8-*-
1+
#-*- coding:utf8 -*-
22
#author : Lenovo
33
#date: 2018/10/10
44
class Solution(object):
@@ -43,4 +43,4 @@ def trans(n):
4343
# return result
4444
S=Solution()
4545
res=S.grayCode(2)
46-
print(res)
46+
print(res)

0 commit comments

Comments
 (0)