Skip to content

Commit 4040a94

Browse files
committed
complete-swordtooffer
1 parent 92607f8 commit 4040a94

29 files changed

+1004
-159
lines changed

README.md

+27-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@
102102

103103
- [***构建乘积数组***](./SwordToOffer/Doc/构建乘积数组.md)
104104

105-
- [正则表达式匹配(未完成)](./SwordToOffer/Doc/正则表达式匹配.md)
105+
- [***正则表达式匹配***](./SwordToOffer/Doc/正则表达式匹配.md)
106106

107-
- [字符流中第一个不重复的字符(未完成)](./SwordToOffer/Doc/字符流中第一个不重复的字符.md)
107+
- [***字符流中第一个不重复的字符***](./SwordToOffer/Doc/字符流中第一个不重复的字符.md)
108108

109109
- [***最大陆地问题***](./SwordToOffer/Doc/最大陆地问题.md)
110110

@@ -114,7 +114,31 @@
114114

115115
- [滑动窗口的最大值](./SwordToOffer/Doc/滑动窗口的最大值.md)
116116

117-
- [数据流的中位数(未完成)](./SwordToOffer/Doc/数据流的中位数.md)
117+
- [***数据流的中位数***](./SwordToOffer/Doc/数据流的中位数.md)
118+
119+
- [***数组中的逆序对***](./SwordToOffer/Doc/数组中的逆序对.md)
120+
121+
- [表示数值的字符串](./SwordToOffer/Doc/表示数值的字符串.md)
122+
123+
- [二叉搜索树的第k个节点](./SwordToOffer/Doc/二叉搜索树的第k个节点.md)
124+
125+
- [序列化二叉树](./SwordToOffer/Doc/序列化二叉树.md)
126+
127+
- [把二叉树打印成多行(层序遍历)](./SwordToOffer/Doc/把二叉树打印成多行.md)
128+
129+
- [***按之字形打印二叉树***](./SwordToOffer/Doc/按之字形打印二叉树.md)
130+
131+
- [对称的二叉树](./SwordToOffer/Doc/对称的二叉树.md)
132+
133+
- [二叉树的下一个节点](./SwordToOffer/Doc/二叉树的下一个节点.md)
134+
135+
- [**删除有序链表中重复的节点**](./SwordToOffer/Doc/删除链表中重复的节点.md)
136+
137+
- [**链表中环的入口节点**](./SwordToOffer/Doc/链表中环的入口节点.md)
138+
139+
- [**字符流中第一个不重复的字符**](./SwordToOffer/Doc/字符流中第一个不重复的字符.md)
140+
141+
- [表示数字的字符串](./SwordToOffer/Doc/表示数字的字符串.md)
118142

119143

120144

SwordToOffer/Code/34.java

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/**
2+
* @author dmrfcoder
3+
* @date 2019/3/12
4+
*/
5+
public class ThreeFour {
6+
7+
public int[] mergeSort(int[] data) {
8+
int[] tempArray = new int[data.length];
9+
10+
for (int segmentSize = 1; segmentSize < data.length; segmentSize = segmentSize * 2) {
11+
int tempArrayIndex = 0;
12+
for (int firstIndex = 0; firstIndex < data.length; firstIndex = firstIndex + segmentSize * 2) {
13+
int tempFirstIndex = firstIndex;
14+
int secondIndex = firstIndex + segmentSize;
15+
16+
while (tempFirstIndex < firstIndex + segmentSize && secondIndex < firstIndex + 2 * segmentSize && tempFirstIndex < data.length && secondIndex < data.length) {
17+
if (data[tempFirstIndex] < data[secondIndex]) {
18+
tempArray[tempArrayIndex++] = data[tempFirstIndex++];
19+
} else {
20+
tempArray[tempArrayIndex++] = data[secondIndex++];
21+
}
22+
}
23+
while (tempFirstIndex < firstIndex + segmentSize) {
24+
tempArray[tempArrayIndex++] = data[tempFirstIndex++];
25+
}
26+
27+
while (secondIndex < firstIndex + segmentSize * 2) {
28+
tempArray[tempArrayIndex++] = data[secondIndex++];
29+
}
30+
}
31+
data = tempArray.clone();
32+
}
33+
34+
return data;
35+
36+
}
37+
38+
public int InversePairs(int[] array) {
39+
int[] tempArray = new int[array.length];
40+
int count = 0;
41+
42+
for (int segmentSize = 1; segmentSize < array.length; segmentSize = segmentSize * 2) {
43+
for (int firstIndex = 0; firstIndex < array.length; firstIndex = firstIndex + segmentSize * 2) {
44+
int firstStartIndex = firstIndex;
45+
int secondStartIndex = firstIndex + segmentSize;
46+
47+
/**
48+
* sement:
49+
* [tempFirstIndex,tempFirstIndex+segmentSize]
50+
* [secondIndex,tempFirstIndex+segmentSize*2]
51+
*/
52+
53+
int firstEndStartIndex = firstStartIndex + segmentSize - 1;
54+
if (firstEndStartIndex >= array.length) {
55+
firstEndStartIndex = array.length - 1;
56+
}
57+
int firstEnd = firstEndStartIndex;
58+
59+
int secondEndStartIndex = secondStartIndex + segmentSize - 1;
60+
if (secondEndStartIndex >= array.length) {
61+
secondEndStartIndex = array.length - 1;
62+
}
63+
int secondEnd = secondEndStartIndex;
64+
65+
int tempArrayEnd = secondEnd;
66+
67+
while (firstEnd >= firstStartIndex && secondEnd >= secondStartIndex && firstEnd >= 0 && secondEnd >= 0 && firstEnd < array.length && secondEnd < array.length) {
68+
69+
if (array[firstEnd] > array[secondEnd]) {
70+
count = count + (secondEnd - secondStartIndex + 1);
71+
if (count > 1000000007) {
72+
count = count % 1000000007;
73+
}
74+
tempArray[tempArrayEnd--] = array[firstEnd--];
75+
76+
} else {
77+
tempArray[tempArrayEnd--] = array[secondEnd--];
78+
79+
}
80+
}
81+
82+
83+
while (firstEnd >= firstStartIndex) {
84+
tempArray[tempArrayEnd--] = array[firstEnd--];
85+
86+
87+
}
88+
89+
while (secondEnd >= secondStartIndex) {
90+
tempArray[tempArrayEnd--] = array[secondEnd--];
91+
92+
}
93+
}
94+
array = tempArray.clone();
95+
}
96+
97+
return count;
98+
99+
}
100+
101+
public static void main(String[] args) {
102+
ThreeFour threeFour = new ThreeFour();
103+
int[] data = {627126, 415347, 850134, 371085, 279048, 705820, 453064, 944751, 92317, 58592, 167988, 284065, 992573, 78043, 190215, 104546, 607528, 391775, 701214, 849731, 231053, 603058, 975374, 199773, 479544, 143961, 206797, 325662, 90035, 69615, 429916, 717161, 484962, 796403, 604598, 280362, 502223, 57662, 741466, 594540, 632606, 909454, 394957, 625180, 503849, 585172, 729726, 627729, 976947, 947293, 477461, 724352, 66703, 452835, 440478, 62599, 596797, 163627, 388261, 203184, 233243, 334529, 436697, 234557, 647284, 41295, 514920, 665859, 615310, 256386, 776752, 247916, 682192, 171709, 389448, 186041, 273234, 635527, 813771, 766533, 582820, 807584, 490886, 649523, 260419, 447716, 228474, 373568, 611343, 616735, 576752, 844586, 467616, 529801, 595496, 631253, 571097, 110416, 297112, 186407, 883154, 73864, 950675, 81698, 245574, 340124, 267739, 35160, 975651, 597862, 801693, 74823, 921798, 292579, 240698, 182218, 256647, 469172, 72138, 867991, 602259, 165243, 228929, 69875, 695044, 824425, 701128, 782493, 451193, 998241, 485252, 334347, 588457, 435928, 416045, 350383, 292404, 200137, 385543, 268055, 314351, 187237, 859230, 236150, 996168, 99928, 934720, 252816, 569100, 523210, 120807, 171359, 688453, 866088, 757586, 383498, 206866, 458715, 682343, 658059, 973308, 167596, 508759, 78117, 603524, 441156, 428501, 412280, 157645, 814044, 196687, 471997, 1281, 55917, 224499, 997450, 155845, 159219, 250266, 241297, 682429, 887425, 412656, 887235, 269865, 686594, 787085, 476731, 661661, 469428, 134791, 634969, 637024, 643550, 229439, 756900, 601058, 657940, 169180, 758704, 471984, 365867, 230701, 473266, 421784, 455200, 470716, 93981, 130771, 237334, 335278, 329552, 641111, 264286, 733139, 910976, 950881, 520224, 904060, 612542, 989653, 38851, 763864, 143029, 198753, 993303, 899930, 799811, 651243, 585462, 558515, 639579, 951330, 305568, 112845, 889466, 277120, 99913, 499800, 924243, 853599, 835078, 770148, 11062, 615717, 503287, 922039, 82950, 23512, 826099, 695492, 529517, 381302, 975708, 672546, 96407, 485363, 88828, 896218, 652958, 674291, 971086, 292538, 141973, 276654, 921735, 547791, 70127, 21649, 47591, 994370, 391600, 399022, 764518, 402663, 14739, 267806, 841054, 97689, 807670, 183505, 309533, 337187, 564807, 801594, 9733, 661214, 803309, 614914, 73784, 456268, 805557, 44870, 265158, 947530, 837877, 703245, 11673, 908004, 241246, 59265, 418726, 632847, 974639, 183245, 35510, 505730, 967403, 392916, 603419, 775073, 576421, 429304, 112260, 141228, 747250, 638345, 318794, 550560, 769611, 392578, 523180, 575168, 953801, 304690, 39050, 308030, 7935, 50724, 216034, 249182, 626341, 151112, 882029, 600980, 334357, 433891, 106710, 818112, 826807, 226481, 593185, 919580, 172137, 221797, 60808, 919388, 376495, 895954, 986300, 146106, 804884, 509480, 237627, 275037, 814170, 276677, 583067, 338457, 327401, 315453, 587639, 953742, 466566, 986020, 71074, 317275, 936263, 694136, 135388, 763070, 920617, 728573, 682650, 92755, 466723, 259810, 528495, 843218, 672116, 514795, 505676, 477001, 24275, 259655, 752038, 354797, 536333, 335106, 693254, 380086, 166911, 797246, 850181, 633477, 783266, 921255, 950753, 719530, 615392, 86141, 998952, 52361, 331066, 197955, 661468, 797789, 974117, 189963, 157359, 646234, 704758, 179388, 639587, 245385, 439043, 907977, 116534, 491728, 759435, 809789, 871815, 926347, 123387, 721996, 559824, 423005, 159603, 510577, 142535, 774995, 113070, 657840, 343709, 444137, 372147, 5177, 758278, 346264, 195141, 915638, 508850, 416251, 611378, 664789, 661637, 50421, 572767, 294523, 58502, 332202, 620664, 930317, 258549, 744051, 652313, 818374, 167057, 811916, 845303, 825944, 103264, 474726, 483784, 446973, 918863, 855931, 968502, 677141, 718548, 163643, 109131, 743750, 96247, 720509, 408540, 274236, 287283, 981307, 568759, 862137, 313509, 189424, 792454, 88411, 933475, 961119, 423137, 616884, 773035, 268440, 442829, 392651, 743166, 442965, 839624, 178381, 815249, 324479, 371875, 533797, 488122, 481006, 793899, 100721, 717868, 202439, 374957, 521503, 183746, 943717, 383640, 13608, 133141, 692446, 618371, 582968, 653565, 41508, 716205, 942952, 826300, 159034, 335604, 85819, 118351, 691580, 780552, 933600, 16059, 152427, 983749, 20534, 149786, 777649, 121255, 384006, 980088, 496213, 421861, 680187, 956282, 321853, 210147, 605775, 14299, 828518, 188743, 667864, 386378, 904948, 610816, 212678, 580334, 462772, 814849, 698686, 154353, 595402, 148638, 170412, 747829, 132388, 190946, 897615, 910037, 828554, 797973, 406477, 324767, 219834, 603016, 281049, 541687, 813163, 886824, 555986, 158033, 591919, 740202, 60763, 13220, 867371, 789794, 109906, 330143, 604643, 324944, 484496, 200045, 473583, 171261, 464227, 605971, 878559, 878194, 32360, 707113, 676168, 438837, 31880, 896002, 41854, 829281, 954042, 371369, 232457, 510028, 45755, 340729, 766583, 106518, 353949, 633954, 896312, 463855, 964097, 17308, 788800, 448594, 733705, 262383, 619855, 714284, 384706, 498414, 592479, 417066, 721880, 784999, 855903, 753760, 197353, 414109, 99394, 151395, 301831, 848203, 177776, 347586, 188932, 944359, 970456, 542881, 578313, 383121, 523089, 58762, 400429, 828241, 507356, 134134, 606976, 643563, 848419, 991682, 658330, 957250, 408748, 380210, 258601, 781003, 650322, 455954, 195113, 266068, 123702, 13296, 114272, 301478, 360882, 303204, 762189, 331338, 846086, 856854, 714459, 885527, 915616, 631240, 713768, 939325, 281727, 837096, 99240, 646498, 828778, 757570, 603748, 753878, 654132, 862349, 534881, 304455, 834655, 246346, 86875, 958357, 259642, 201147, 776187, 136876, 504352, 538376, 984567, 866790, 911582, 699026, 268669, 343551, 846619, 982437, 282876, 128346, 819533, 382116, 774844, 164663, 656039, 894944, 918541, 826523, 273645, 969774, 647330, 108300, 732473, 734206, 66658, 508467, 935353, 359197, 161696, 956057, 413926, 146263, 339199, 325508, 361641, 607868, 669059, 208260, 106657, 468287, 336606, 442542, 366756, 627802, 607205, 539147, 522746, 42098, 365670, 796391, 528225, 13001, 421044, 777050, 747207, 4054, 285517, 198912, 363251, 447213, 154970, 777177, 593476, 494169, 619038, 471470, 102038, 804449, 679730, 208695, 272737, 532689, 651238, 160491, 774795, 694992, 199590, 333246, 60662, 512333, 861471, 590015, 933377, 638521, 337222, 937431, 440390, 536135, 300683, 887604, 691105, 594212, 997432, 701626, 213250, 468902, 320016, 17700, 664985, 528712, 806789, 197674, 696302, 962634, 874517, 987449, 173978, 74107, 320695, 234640, 102793, 182166, 824656, 36170, 337039, 678230, 489954, 777430, 214365, 790637, 181386, 421822, 901201, 178818, 639801, 114452, 164073, 959817, 648504, 829058, 4881, 971645, 543084, 217535, 934279, 417601, 204985, 108257, 8061, 525680, 859249, 110854, 224199, 200257, 663376, 561238, 878488, 153330, 855020, 609205, 460319, 36406, 547380, 361521, 731577, 187181, 992325, 895650, 146998, 157181, 241060, 668232, 128826, 784144, 885767, 579457, 718097, 90752, 687714, 726158, 132785, 63315, 353364, 873336, 263573, 533093, 950926, 142061, 686423, 805947, 267618, 663095, 842353, 814998, 540968, 573930, 518531, 49645, 985932, 665530, 206826, 226992, 850114, 852004, 527488, 735881, 431461, 245586, 342986, 488096, 475771, 698842, 357813, 349107, 962415, 890906, 300033, 620828, 93681, 622332, 404799, 273128, 464686, 219797, 814096, 554968, 738329, 863741, 540901, 920211, 586919, 284245, 770325, 438923, 811734, 22558, 870384, 573672, 365544, 505911, 578120, 841315, 721106, 935933, 706774, 683521, 343191, 523160, 820702, 436873, 145492, 225501, 710001, 126530, 961650, 40450, 681499, 216331, 904191, 738752, 136542, 7463, 22997, 423219, 446386, 834731, 445778, 833123, 924755, 811322, 339034, 502876, 168990, 576492, 438809, 392116, 260014, 782001, 431628, 597068, 735226, 93473, 338921, 445227, 220003, 300571, 2029, 417854, 516903, 906221, 156606, 169797, 430036, 179604, 593017, 876422, 530687, 38795, 709545, 455443, 366469, 564932, 958319, 535459, 141424, 913480, 443928, 917790, 211833, 875556, 31210, 947059, 969029, 886483, 908639, 705385, 187055, 910668, 123239, 220310, 333241, 796198, 906459, 763277, 492154, 15828, 156052, 22841, 570975, 381949, 994636, 937445, 463233, 469307, 989256, 121010, 899140, 433184, 555152, 110973, 308741, 586363, 58033, 794122, 472846};
104+
int count = threeFour.InversePairs(data);
105+
System.out.println(count);
106+
}
107+
}

SwordToOffer/Code/34.py

-41
This file was deleted.

SwordToOffer/Code/52.py

+45-36
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,58 @@ class Solution:
88
# s, pattern都是字符串
99
def match(self, s, pattern):
1010
# write code here
11-
sizeS = len(s)
1211

13-
pre = ''
14-
preChar = ''
15-
patternIndex = len(pattern) - 1
16-
j = sizeS - 1
17-
while j >= 0:
18-
curPatternChar = pattern[patternIndex]
19-
curSChar = s[j]
20-
if patternIndex == -1:
21-
return False
22-
if pattern[patternIndex] == curSChar:
23-
patternIndex -= 1
24-
j -= 1
25-
elif curPatternChar != '*' and curPatternChar != '.':
26-
if pre == '*':
27-
if preChar == '':
28-
preChar = curPatternChar
29-
if curSChar == preChar or preChar=='.':
30-
j -= 1
31-
else:
32-
patternIndex -= 1
33-
preChar = ''
34-
pre = ''
35-
36-
else:
37-
return False
38-
elif curPatternChar == '.':
39-
patternIndex -= 1
40-
j -= 1
41-
else:
42-
pre = '*'
43-
patternIndex -= 1
12+
sizeS = len(s)
13+
sizeP = len(pattern)
4414

45-
if patternIndex == -1:
15+
if s == '' and pattern == '':
4616
return True
47-
else:
17+
18+
if s != '' and pattern == '':
4819
return False
4920

21+
indexS = 0
22+
indexP = 0
23+
if indexS + 1 >= sizeS:
24+
temps = ''
25+
else:
26+
temps = s[indexS + 1:]
5027

51-
s = Solution()
52-
print s.match('', '.*')
28+
if indexP + 1 >= sizeP:
29+
tempp = ''
30+
arrayp = ''
31+
else:
32+
tempp = pattern[indexP + 1]
33+
arrayp = pattern[indexP + 1:]
34+
35+
if indexP + 2 >= sizeP:
36+
arrayp2 = ''
37+
else:
38+
arrayp2 = pattern[indexP + 2:]
39+
40+
if s == '':
41+
curS = ''
42+
else:
43+
curS = s[indexS]
44+
45+
if tempp != '*':
46+
if curS == pattern[indexP]:
47+
return self.match(temps, arrayp)
48+
elif pattern[indexP] == '.' and s != '':
49+
return self.match(temps, arrayp)
50+
else:
51+
return False
52+
else:
53+
if pattern[indexP] == curS:
54+
return self.match(temps, pattern) or self.match(s, arrayp2)
55+
elif pattern[indexP] == '.' and s != '':
56+
return self.match(temps, pattern) or self.match(s, arrayp2)
57+
else:
58+
return self.match(s, arrayp2)
5359

60+
61+
s = Solution()
62+
print s.match("bbbba", ".*a*a")
5463
'''
5564
aaa
5665
ab*ac*a

SwordToOffer/Code/53.py

+29-19
Original file line numberDiff line numberDiff line change
@@ -8,36 +8,46 @@
88

99
class Solution:
1010
def __init__(self):
11-
self.array = []
12-
self.char = []
11+
self.array = [-1 for i in range(256)]
12+
self.index = 0
1313

1414
# 返回对应char
1515
def FirstAppearingOnce(self):
16-
return self.char[-1]
16+
i = 0
17+
min = -1
18+
index = 0
19+
for itemArray in self.array:
20+
if itemArray != -1 and itemArray != -2:
21+
if min == -1:
22+
min = itemArray
23+
index = i
24+
elif itemArray < min:
25+
min = itemArray
26+
index = i
27+
i += 1
28+
if min == -1:
29+
return '#'
30+
return chr(index)
1731

1832
def Insert(self, char):
19-
20-
if char not in self.array:
21-
if len(self.array)==0:
22-
self.char.append(char)
23-
else:
24-
if len(self.char)>=1 and self.char[-1] == '#':
25-
self.char.append(char)
26-
elif len(self.char)<1 :
27-
self.char.append('#')
28-
else:
29-
self.char.append(char)
30-
33+
intChar = ord(char)
34+
if self.array[intChar] > -1:
35+
self.array[intChar] = -2
3136
else:
32-
self.char.append(char[-1])
33-
self.array.append(char)
37+
self.array[intChar] = self.index
38+
self.index += 1
39+
3440

35-
s=Solution()
41+
s = Solution()
3642
s.Insert('g')
3743
print s.FirstAppearingOnce()
3844
s.Insert('o')
3945
print s.FirstAppearingOnce()
4046
s.Insert('o')
4147
print s.FirstAppearingOnce()
4248
s.Insert('g')
43-
print s.FirstAppearingOnce()
49+
print s.FirstAppearingOnce()
50+
s.Insert('l')
51+
print s.FirstAppearingOnce()
52+
s.Insert('e')
53+
print s.FirstAppearingOnce()

0 commit comments

Comments
 (0)