Skip to content

Commit 83f8410

Browse files
committed
2.28
1 parent 7b6d612 commit 83f8410

15 files changed

+491
-45
lines changed

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,13 @@
6060

6161
- [把数组排成最小的数](./SwordToOffer/Doc/把数组排成最小的数.md)
6262

63+
- [整数中1出现的次数](./SwordToOffer/Doc/整数中1出现的次数.md)
64+
65+
- [***丑数***](./SwordToOffer/Doc/丑数.md)
66+
67+
- [第一次只出现一次的字符](./SwordToOffer/Doc/第一次只出现一次的字符.md)
68+
69+
- [数组中的逆序对](./SwordToOffer/Doc/数组中的逆序对.md)
70+
6371

72+

SwordToOffer/Code/25.py

+83-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,87 @@
11
# -*- coding:utf-8 -*-
2-
# class TreeNode:
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
2+
class TreeNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.left = None
6+
self.right = None
7+
8+
79
class Solution:
810
def Convert(self, pRootOfTree):
911
# write code here
10-
pass
12+
stack = []
13+
p = pRootOfTree
14+
a = []
15+
pre = None
16+
next = None
17+
preP = None
18+
head = None
19+
20+
while len(stack) != 0 or p is not None:
21+
if p is not None:
22+
stack.append(p)
23+
p = p.left
24+
else:
25+
p = stack[-1]
26+
del stack[-1]
27+
if head is None:
28+
head = p
29+
# a.append(p.val)
30+
p.left = preP
31+
32+
if preP is not None:
33+
preP.right = p
34+
preP = p
35+
36+
p = p.right
37+
38+
return head
39+
40+
41+
def MidPrint(root):
42+
stack = []
43+
p = root
44+
a = []
45+
pre = None
46+
next = None
47+
preP = None
48+
head = None
49+
50+
while len(stack) != 0 or p is not None:
51+
if p is not None:
52+
stack.append(p)
53+
p = p.left
54+
else:
55+
p = stack[-1]
56+
del stack[-1]
57+
if head is None:
58+
head = p
59+
# a.append(p.val)
60+
p.left = preP
61+
62+
if preP is not None:
63+
preP.right = p
64+
preP = p
65+
66+
p = p.right
67+
68+
return head
69+
70+
71+
pRoot1 = TreeNode(1)
72+
treeNode2 = TreeNode(2)
73+
treeNode3 = TreeNode(3)
74+
treeNode4 = TreeNode(4)
75+
treeNode5 = TreeNode(5)
76+
treeNode6 = TreeNode(6)
77+
treeNode7 = TreeNode(7)
78+
79+
pRoot1.left = treeNode2
80+
pRoot1.right = treeNode3
81+
treeNode2.left = treeNode4
82+
treeNode2.right = treeNode5
83+
treeNode3.left = treeNode6
84+
treeNode3.right = treeNode7
85+
s = Solution()
86+
head = MidPrint(pRoot1)
87+
a = 1

SwordToOffer/Code/26.py

+29-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,37 @@
11
# -*- coding:utf-8 -*-
22
class Solution:
3+
4+
def swap(self, arr, one, two):
5+
arr = list(arr)
6+
t = arr[one]
7+
arr[one] = arr[two]
8+
arr[two] = t
9+
return ''.join(arr)
10+
311
def Permutation(self, ss):
412
# write code here
5-
if len(ss)==1:
6-
return ss
7-
13+
if ss is None:
14+
return []
15+
if len(ss)==0:
16+
return []
17+
if len(ss) == 1:
18+
return [ss]
19+
size = len(ss)
820
t = self.Permutation(ss[1:])
21+
arr = []
22+
for i in range(len(t)):
23+
t[i] = ss[0] + t[i]
24+
arr.append(t[i])
25+
for j in range(1, size):
26+
if t[i][0]==t[i][j]:
27+
continue
28+
arr.append(self.swap(t[i], 0, j))
29+
return arr
30+
31+
def Permutation2(self,ss):
32+
intSs=[]
33+
934

1035

1136
s = Solution()
12-
s.Permutation('abcd')
37+
print s.Permutation('aab')

SwordToOffer/Code/30.py

+29-30
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
11
# -*- coding:utf-8 -*-
22
class Solution:
3-
4-
def PrintMinNumber(self, numbers):
3+
def NumberOf1Between1AndN_Solution(self, n):
54
# write code here
6-
if numbers is None:
7-
return ""
8-
if len(numbers)==0:
9-
return ""
10-
strs = []
11-
for item in numbers:
12-
strs.append(str(item))
13-
14-
z = sorted(strs, cmp)
15-
16-
r=''
17-
18-
for item in z:
19-
r=r+item
20-
21-
22-
return int(r)
23-
24-
25-
def cmp(str1, str2):
26-
s1 = str1 + str2
27-
s2 = str2 + str1
28-
29-
if int(s1) < int(s2):
30-
return -1
31-
else:
32-
return 0
5+
strN = str(n)
6+
size = len(strN)
7+
sum = 0
8+
9+
# 从个位开始
10+
for i in range(size - 1, -1, -1):
11+
curBit = int(strN[i])
12+
multiValue = pow(10, size - 1 - i)
13+
if i == 0:
14+
biggerValue = 0
15+
else:
16+
biggerValue = strN[0:i]
17+
if i == size - 1:
18+
smallerValue = 0
19+
else:
20+
smallerValue = strN[i + 1:]
21+
22+
if curBit == 1:
23+
count = int(biggerValue) * multiValue + int(smallerValue) + 1
24+
elif curBit == 0:
25+
count = int(biggerValue) * multiValue
26+
else:
27+
count = (int(biggerValue) + 1) * multiValue
28+
29+
sum += count
30+
31+
return sum
3332

3433

3534
s = Solution()
36-
print s.PrintMinNumber([3,32,321])
35+
print s.NumberOf1Between1AndN_Solution(13)

SwordToOffer/Code/31.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# -*- coding:utf-8 -*-
2+
3+
def cmp(str1, str2):
4+
s1 = str1 + str2
5+
s2 = str2 + str1
6+
if int(s1) < int(s2):
7+
return -1
8+
else:
9+
return 0
10+
11+
12+
class Solution:
13+
14+
def PrintMinNumber(self, numbers):
15+
# write code here
16+
if numbers is None:
17+
return ""
18+
if len(numbers) == 0:
19+
return ""
20+
21+
strs = []
22+
for item in numbers:
23+
strs.append(str(item))
24+
25+
strs.sort(cmp)
26+
print strs
27+
28+
r = ''
29+
for item in strs:
30+
r = r + item
31+
32+
return int(r)
33+
34+
35+
s = Solution()
36+
print s.PrintMinNumber([3, 5, 1, 4, 2])

SwordToOffer/Code/32.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def GetUglyNumber_Solution(self, index):
4+
# write code here
5+
if index==0:
6+
return 0
7+
8+
t2Index = 0
9+
t3Index = 0
10+
t5Index = 0
11+
12+
res = [1]
13+
14+
for i in range(0, index - 1):
15+
minValue = min(res[t2Index] * 2, min(res[t3Index] * 3, res[t5Index] * 5))
16+
res.append(minValue)
17+
if minValue == res[t2Index] * 2:
18+
t2Index += 1
19+
if minValue == res[t3Index] * 3:
20+
t3Index += 1
21+
if minValue == res[t5Index] * 5:
22+
t5Index += 1
23+
24+
return res[-1]
25+
26+
27+
s = Solution()
28+
print s.GetUglyNumber_Solution(7)

SwordToOffer/Code/33.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def FirstNotRepeatingChar(self, s):
4+
# write code here
5+
size = len(s)
6+
#存储不同字符
7+
array1 = []
8+
#存储不同字符对应的出现次数
9+
array2 = []
10+
#存储不同字符第一次出现的下标
11+
array3=[]
12+
13+
14+
for i in range(size):
15+
if s[i] not in array1:
16+
array1.append(s[i])
17+
array2.append(1)
18+
array3.append(i)
19+
else:
20+
index = array1.index(s[i])
21+
array2[index] += 1
22+
23+
for i in range(len(array2)):
24+
if array2[i] == 1:
25+
return array3[i]
26+
27+
return -1
28+
29+
s=Solution()
30+
print s.FirstNotRepeatingChar('aabbccd')

SwordToOffer/Code/34.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding:utf-8 -*-
2+
def cmp(a, b):
3+
if a < b:
4+
return 0
5+
else:
6+
return -1
7+
8+
9+
class Solution:
10+
11+
def InversePairs(self, data):
12+
# write code here
13+
size = len(data)
14+
count = 0
15+
for i in range(size - 1, -1, -1):
16+
for j in range(i - 1, -1, -1):
17+
if data[i] < data[j]:
18+
count += 1
19+
20+
return count % 1000000007
21+
22+
23+
def GuiBingSort(data):
24+
size = len(data)
25+
if size == 2:
26+
if data[0] < data[1]:
27+
return data
28+
else:
29+
return [data[1], data[0]]
30+
31+
if size==0:
32+
return data
33+
34+
index=int(size/2)
35+
return GuiBingSort(data[0:index])+GuiBingSort(data[index:])
36+
37+
38+
s = Solution()
39+
#print s.InversePairs([1, 2, 3, 4, 5, 6, 7, 0])
40+
print GuiBingSort([1, 2, 3, 4, 5, 6, 7, 0])

0 commit comments

Comments
 (0)