Skip to content

Commit d5a1000

Browse files
committedMar 6, 2019
3.6
1 parent 83f8410 commit d5a1000

17 files changed

+667
-24
lines changed
 

‎README.md

+10
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,15 @@
6868

6969
- [数组中的逆序对](./SwordToOffer/Doc/数组中的逆序对.md)
7070

71+
- [两个链表的第一个公共节点](./SwordToOffer/Doc/两个链表的第一个公共节点.md)
72+
73+
- [数字在排序数组中出现的次数](./SwordToOffer/Doc/数字在排序数组中出现的次数.md)
74+
75+
- [二叉树的深度](./SwordToOffer/Doc/二叉树的深度.md)
76+
77+
- [平衡二叉树](./SwordToOffer/Doc/平衡二叉树.md)
78+
79+
- [***数组中只出现一次的数字***](./SwordToOffer/Doc/数组中只出现一次的数字.md)
80+
7181

7282

‎SwordToOffer/Code/26.py

+92-24
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,105 @@
11
# -*- coding:utf-8 -*-
2-
class Solution:
2+
import copy
3+
4+
5+
def cmp(x, y):
6+
if x > y:
7+
return -1
8+
else:
9+
return 0
310

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)
11+
12+
class Solution:
1013

1114
def Permutation(self, ss):
12-
# write code here
1315
if ss is None:
1416
return []
15-
if len(ss)==0:
17+
if len(ss) == 0:
1618
return []
17-
if len(ss) == 1:
18-
return [ss]
19+
listss = list(ss)
20+
listss.sort()
21+
myList = []
22+
myList.append(ss)
23+
self.PermutationHelper(listss, myList)
24+
return myList
25+
26+
def PermutationHelper2(self, ss):
27+
if ss is None:
28+
return []
29+
if len(ss) == 0:
30+
return []
31+
mylist = []
32+
mylist.append(ss)
33+
size = len(ss)
34+
while True:
35+
p = len(ss) - 1
36+
37+
while p >= 1 and ss[p - 1] >= ss[p]:
38+
p -= 1
39+
40+
if p == 0:
41+
break
42+
43+
q = p
44+
p -= 1
45+
46+
while q < size and ss[q] > ss[p]:
47+
q += 1
48+
q -= 1
49+
50+
ss = list(ss)
51+
t = ss[p]
52+
ss[p] = ss[q]
53+
ss[q] = t
54+
55+
tempss = ss[p + 1:]
56+
tempss = tempss[::-1]
57+
ss = ss[0:p + 1] + tempss
58+
ss = "".join(ss)
59+
mylist.append(ss)
60+
61+
return mylist
62+
63+
def PermutationHelper(self, ss, Mylist):
1964
size = len(ss)
20-
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
65+
for i in range(size - 2, -1, -1):
66+
if ss[i] < ss[i + 1]:
67+
flag = 1
68+
for j in range(size - 1, i, -1):
69+
if ss[j] > ss[i]:
70+
t = ss[i]
71+
ss[i] = ss[j]
72+
ss[j] = t
73+
74+
tempss = ss[i + 1:]
75+
tempss = tempss[::-1]
76+
ss = ss[0:i + 1] + tempss
77+
flag = 0
78+
break
79+
if flag == 0:
80+
Mylist.append("".join(copy.copy(ss)))
81+
self.PermutationHelper(ss, Mylist)
82+
83+
84+
def reversedemo(ss):
85+
return reversed(ss)
86+
3087

31-
def Permutation2(self,ss):
32-
intSs=[]
88+
def demo():
89+
ss = "122"
90+
ss = list(ss)
91+
mylist = []
92+
mylist.append(copy.copy(ss))
93+
s = Solution()
94+
s.PermutationHelper(ss, mylist)
95+
for items in mylist:
96+
print items
3397

3498

99+
# demo()
35100

101+
ss = '122'
36102
s = Solution()
37-
print s.Permutation('aab')
103+
res = s.PermutationHelper2(ss)
104+
for itemRes in res:
105+
print itemRes

‎SwordToOffer/Code/34.py

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ def GuiBingSort(data):
3535
return GuiBingSort(data[0:index])+GuiBingSort(data[index:])
3636

3737

38+
3839
s = Solution()
3940
#print s.InversePairs([1, 2, 3, 4, 5, 6, 7, 0])
4041
print GuiBingSort([1, 2, 3, 4, 5, 6, 7, 0])

‎SwordToOffer/Code/35.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# -*-coding:utf-8
2+
import copy
3+
4+
5+
def FindC(data, n, findedList, s, finded, curS):
6+
"""
7+
从data数组中找出n个数使其和为s,可能的数组组合可能不止一个
8+
9+
:param data: 数组,必须需要从小到大排过序
10+
:param n: 一共找n个元素
11+
:param findedList:item是finded,记录所有的可能的组合
12+
:param s:和为s
13+
:param finded: 当前可能数组组合中元素对应的下标
14+
:param curS:当前finded元素中的和,用它记录起来就不用每次都计算sum(finded)的值了
15+
16+
"""
17+
if n == 0:
18+
return
19+
# 开始下标,如果finded中有元素就应该是finded的最后一个元素
20+
startIndex = 0
21+
if len(finded) != 0:
22+
startIndex = finded[-1] + 1
23+
# 从startIndex向后遍历data
24+
for i in range(startIndex, m):
25+
# 假设data[i]是组合中的一个数
26+
curS += data[i]
27+
# 如果加上data[i]之后curS的值还是小于s
28+
if curS < s:
29+
# 先将i加入finded
30+
finded.append(i)
31+
# 利用递归从data中找下一个元素
32+
FindC(data, n, findedList, s, finded, curS)
33+
# 回溯,消除刚才的i,因为i不一定是最终组合的数,消除其影响后方便下一次遍历
34+
curS -= data[i]
35+
del finded[-1]
36+
# 如果curS等于s而且元素数列为n-1(因为finded中没append i,所以只用等于n-1即可)
37+
elif curS == s and len(finded) == n - 1:
38+
findedValue = []
39+
for itemFinded in finded:
40+
findedValue.append(data[itemFinded])
41+
findedValue.append(data[i])
42+
findedList.append(findedValue)
43+
break
44+
else:
45+
break
46+
47+
48+
def quickSort(data, low, high):
49+
if low>=high:
50+
return
51+
centerIndex = partation(data, low, high)
52+
quickSort(data, low, centerIndex - 1)
53+
quickSort(data, centerIndex + 1, high)
54+
55+
56+
def partation(data, low, high):
57+
curValue = data[low]
58+
while low < high:
59+
while low < high and data[high] >= curValue:
60+
high -= 1
61+
62+
t = data[low]
63+
data[low] = data[high]
64+
data[high] = t
65+
66+
while low < high and data[low] <= curValue:
67+
low += 1
68+
69+
t = data[low]
70+
data[low] = data[high]
71+
data[high] = t
72+
73+
return low
74+
75+
76+
data = [4, 2, 1, 4, 6, 3]
77+
quickSort(data,0,len(data)-1)
78+
print data
79+
# findedList = []
80+
# finded = []
81+
# curS = 0
82+
# m = 6
83+
# s = 12
84+
# n = 4
85+
# FindC(data, n, findedList, s, finded, curS)
86+
# print findedList

‎SwordToOffer/Code/36.py

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# -*- coding:utf-8 -*-
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
import copy
8+
class Solution:
9+
def FindFirstCommonNode(self, pHead1, pHead2):
10+
# write code here
11+
size=0
12+
13+
head1copy=copy.copy(pHead1)
14+
head2copy=copy.copy(pHead2)
15+
16+
17+
while head1copy is not None and head2copy is not None:
18+
head1copy=head1copy.next
19+
head2copy=head2copy.next
20+
21+
flag=-1
22+
while head1copy is not None:
23+
head1copy = head1copy.next
24+
flag=1
25+
size+=1
26+
27+
while head2copy is not None:
28+
head2copy = head2copy.next
29+
flag=2
30+
size+=1
31+
32+
33+
if flag==2:
34+
for i in range(size):
35+
pHead2=pHead2.next
36+
else:
37+
for i in range(size):
38+
pHead1=pHead1.next
39+
40+
while pHead1 is not None:
41+
if pHead1.val==pHead2.val:
42+
return pHead1
43+
else:
44+
pHead1=pHead1.next
45+
pHead2=pHead2.next
46+
return None
47+
48+
49+
50+
51+
52+
s = Solution()
53+
node1 = ListNode(1)
54+
node2 = ListNode(2)
55+
node3 = ListNode(3)
56+
node4 = ListNode(4)
57+
node5 = ListNode(5)
58+
node6 = ListNode(6)
59+
60+
node1.next = node2
61+
node2.next = node3
62+
node3.next = node4
63+
64+
node5.next = node6
65+
node6.next = node3
66+
67+
node = s.FindFirstCommonNode(node1, node5)
68+
a = 0

‎SwordToOffer/Code/37.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def GetNumberOfK(self, data, k):
4+
# write code here
5+
size = len(data)
6+
if size == 0:
7+
return 0
8+
if k > data[-1] or k < data[0]:
9+
return 0
10+
11+
count = 0
12+
13+
startIndex = 0
14+
endIndex = size - 1
15+
16+
index = size - 1
17+
18+
while True:
19+
if index < 0 or index > size:
20+
break
21+
if data[index] > k:
22+
endIndex = index
23+
index = int((startIndex + endIndex) / 2)
24+
elif data[index] > k:
25+
startIndex = index
26+
index = int((startIndex + endIndex) / 2)
27+
28+
else:
29+
for i in range(index, -1, -1):
30+
if data[i] == k:
31+
count += 1
32+
33+
else:
34+
break
35+
36+
for i in range(index + 1, size):
37+
if data[i] == k:
38+
count += 1
39+
else:
40+
break
41+
break
42+
43+
return count
44+
45+
46+
a = [1, 2, 3, 4, 5, 5]
47+
s = Solution()
48+
count = s.GetNumberOfK(a, 5)
49+
print count

‎SwordToOffer/Code/38.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# -*- coding:utf-8 -*-
2+
# class TreeNode:
3+
# def __init__(self, x):
4+
# self.val = x
5+
# self.left = None
6+
# self.right = None
7+
class Solution:
8+
def TreeDepth(self, pRoot):
9+
# write code here
10+
if pRoot is None:
11+
return 0
12+
return max(1 + self.TreeDepth(pRoot.left), 1 + self.TreeDepth(pRoot.right))
13+
14+
15+
#迭代版本
16+
'''
17+
class Solution {
18+
public:
19+
int TreeDepth(TreeNode* pRoot) {
20+
if (!pRoot) return 0;
21+
queue<TreeNode*> que;
22+
que.push(pRoot);int depth=0;
23+
while (!que.empty()) {
24+
int size=que.size();
25+
depth++;
26+
for (int i=0;i<size;i++) { //一次处理一层的数据
27+
TreeNode *node=que.front();
28+
que.pop();
29+
if (node->left) que.push(node->left);
30+
if (node->right) que.push(node->right);
31+
}
32+
}
33+
return depth;
34+
}
35+
};
36+
37+
38+
'''

0 commit comments

Comments
 (0)