Skip to content

Commit 3d9c8ef

Browse files
committed
update-doc-at-3.12
1 parent d5a1000 commit 3d9c8ef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+1332
-26
lines changed

LeetCode/Doc/doc-sample.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# candy
2+
3+
<center>知识点:</center>
4+
5+
6+
## 题目描述
7+
8+
9+
10+
## 解题思路
11+
12+
13+
14+
## 代码
15+
16+
[这里](../Code/n.py)

LeetCode/Doc/triange.md

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# candy
2+
3+
<center>知识点:</center>
4+
5+
6+
## 题目描述
7+
8+
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
9+
10+
For example, given the following triangle
11+
12+
[
13+
[2],
14+
[3,4],
15+
[6,5,7],
16+
[4,1,8,3]
17+
]
18+
19+
The minimum path sum from top to bottom is11(i.e., 2 + 3 + 5 + 1 = 11).
20+
21+
Note:
22+
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle.
23+
24+
25+
26+
## 解题思路
27+
28+
29+
30+
## 代码
31+
32+
[这里](../Code/n.py)

README.md

+36
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,41 @@
7878

7979
- [***数组中只出现一次的数字***](./SwordToOffer/Doc/数组中只出现一次的数字.md)
8080

81+
- [和为s的连续正数序列](./SwordToOffer/Doc/和为s的连续正数序列.md)
82+
83+
- [和为s的两个数字](./SwordToOffer/Doc/和为s的两个数字.md)
84+
85+
- [左旋字符串](./SwordToOffer/Doc/左旋字符串.md)
86+
87+
- [翻转单词序列](./SwordToOffer/Doc/翻转单词序列.md)
88+
89+
- [扑克牌顺子](./SwordToOffer/Doc/扑克牌顺子.md)
90+
91+
- [圆圈中的最后数字](./SwordToOffer/Doc/圆圈中的最后数字.md)
92+
93+
- [不能用循环的1到n之和](./SwordToOffer/Doc/不能用循环的1到n之和.md)
94+
95+
- [***不用加减乘除做加法***](./SwordToOffer/Doc/不用加减乘除做加法.md)
96+
97+
- [将字符串转为整数](./SwordToOffer/Doc/将字符串转为整数.md)
98+
99+
- [***寻找数组中任意一个重复的数字***](./SwordToOffer/Doc/寻找数组中任意一个重复的数字.md)
100+
101+
- [***构建乘积数组***](./SwordToOffer/Doc/构建乘积数组.md)
102+
103+
- [正则表达式匹配(未完成)](./SwordToOffer/Doc/正则表达式匹配.md)
104+
105+
- [字符流中第一个不重复的字符(未完成)](./SwordToOffer/Doc/字符流中第一个不重复的字符.md)
106+
107+
- [***最大陆地问题***](./SwordToOffer/Doc/最大陆地问题.md)
108+
109+
- [***机器人的运动范围***](./SwordToOffer/Doc/机器人的运动范围.md)
110+
111+
- [***矩阵中的路径***](./SwordToOffer/Doc/矩阵中的路径.md)
112+
113+
- [滑动窗口的最大值](./SwordToOffer/Doc/滑动窗口的最大值.md)
114+
115+
- [数据流的中位数(未完成)](./SwordToOffer/Doc/数据流的中位数.md)
116+
81117

82118

SwordToOffer/Code/41.py

+20-24
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
11
# -*- coding:utf-8 -*-
2+
'''
3+
和为s的连续正数序列
4+
小明很喜欢数学,有一天他在做数学作业时,要求计算出9~16的和,他马上就写出了正确答案是100。但是他并不满足于此,他在想究竟有多少种连续的正数序列的和为100(至少包括两个数)。没多久,他就得到另一组连续正数和为100的序列:18,19,20,21,22。现在把问题交给你,你能不能也很快的找出所有和为S的连续正数序列? Good Luck!
5+
输出描述:
6+
输出所有和为S的连续正数序列。序列内按照从小至大的顺序,序列间按照开始数字从小到大的顺序
7+
8+
'''
29
class Solution:
310
def FindContinuousSequence(self, tsum):
4-
# write code here
5-
arrayList = []
6-
for i in range(1, tsum):
7-
startIndex, array = self.findArray(tsum, i, [], 0)
8-
if startIndex == -1:
9-
continue
10-
else:
11-
i = i + startIndex
12-
arrayList.append(array)
13-
return arrayList
14-
15-
def findArray(self, tsum, startIndex, array, sum):
16-
if startIndex == tsum - 1:
17-
return -1, []
18-
for i in range(startIndex, tsum):
19-
sum += i
20-
array.append(i)
11+
lowIndex = 1
12+
highIndex = 2
2113

14+
res = []
15+
while highIndex > lowIndex:
16+
sum = int((highIndex + lowIndex) * (highIndex - lowIndex + 1) / 2)
2217
if sum == tsum:
23-
return startIndex, array
18+
curSqr = range(lowIndex, highIndex+1)
19+
res.append(curSqr)
20+
lowIndex += 1
21+
2422
elif sum < tsum:
25-
array.append(i)
23+
highIndex += 1
2624
else:
27-
startIndex += 1
28-
sum -= array[0]
29-
del array[0]
30-
return -1, []
25+
lowIndex += 1
26+
return res
3127

3228

3329
s = Solution()
34-
t = s.FindContinuousSequence(10)
30+
t = s.FindContinuousSequence(1)
3531
print t

SwordToOffer/Code/42.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
和为s的两个数字
4+
输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。
5+
输出描述:
6+
对应每个测试案例,输出两个数,小的先输出。
7+
'''
8+
class Solution:
9+
def FindNumbersWithSum(self, array, tsum):
10+
# write code here
11+
lowIndex = 0
12+
highIndex = len(array)-1
13+
while highIndex > lowIndex:
14+
sum = array[highIndex] + array[lowIndex]
15+
if sum == tsum:
16+
return [array[lowIndex], array[highIndex]]
17+
elif sum > tsum:
18+
highIndex-=1
19+
else:
20+
lowIndex += 1
21+
return []
22+
23+
24+
s=Solution()
25+
print s.FindNumbersWithSum([1,2,4,7,11,15],15)

SwordToOffer/Code/43.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
左旋字符串
4+
汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!
5+
'''
6+
class Solution:
7+
def LeftRotateString(self, s, n):
8+
# write code here
9+
size=len(s)
10+
if size==0:
11+
return s
12+
n=n%size
13+
listS=list(s)
14+
temp=listS[0:n]
15+
for i in range(n):
16+
del listS[0]
17+
18+
listS=listS+temp
19+
20+
s="".join(listS)
21+
22+
return s
23+
24+
25+
solution=Solution()
26+
print solution.LeftRotateString('abcde',3)

SwordToOffer/Code/44.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
翻转单词序列
4+
牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?
5+
'''
6+
class Solution:
7+
def ReverseSentence(self, s):
8+
# write code here
9+
splitList = s.split(" ")
10+
news = ""
11+
12+
for i in splitList:
13+
14+
news = i + " " + news
15+
16+
#由于最后多一个空格,这里忽略掉
17+
return news[0:-1]
18+
s=Solution()
19+
print s.ReverseSentence("student. a am I")

SwordToOffer/Code/45.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
扑克牌顺子
4+
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
5+
'''
6+
class Solution:
7+
def IsContinuous(self, numbers):
8+
# write code here
9+
numbers = sorted(numbers)
10+
size = len(numbers)
11+
count = 0
12+
pre = 0
13+
14+
if size==0:
15+
return False
16+
for i in range(size):
17+
if numbers[i] == 0:
18+
count += 1
19+
else:
20+
if pre == 0:
21+
pre = numbers[i]
22+
else:
23+
if numbers[i]==pre:
24+
return False
25+
if numbers[i] - pre == 1:
26+
pre += 1
27+
else:
28+
29+
count = count - (numbers[i] - pre - 1)
30+
pre =numbers[i]
31+
if count < 0:
32+
return False
33+
34+
35+
return True
36+
37+
38+
s = Solution()
39+
print s.IsContinuous([1,0,0,1,0])

SwordToOffer/Code/46.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
圆圈中的最后数字
4+
每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0...m-1报数....这样下去....直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!^_^)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)
5+
'''
6+
class Solution:
7+
def LastRemaining_Solution(self, n, m):
8+
# write code here
9+
if n==0:
10+
return -1
11+
array = range(n)
12+
startIndex = 0
13+
while len(array) != 1:
14+
startIndex = self.Helper(array, startIndex, m)
15+
return array[0]
16+
17+
def Helper(self, array, startIndex, m):
18+
size = len(array)
19+
curIndex = 0
20+
for i in range(m):
21+
curIndex = startIndex + i
22+
if curIndex >= size:
23+
curIndex = curIndex % size
24+
25+
del array[curIndex]
26+
27+
return curIndex
28+
29+
instr=raw_input("")
30+
lis=instr.split(" ")
31+
32+
s=Solution()
33+
s.LastRemaining_Solution(int(lis[0]),int(lis[2])-1)

SwordToOffer/Code/47.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
求1+2+3+...+n
4+
求1+2+3+...+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。
5+
'''
6+
7+
8+
class Solution:
9+
def Sum_Solution(self, n):
10+
# write code here
11+
if n==1:
12+
return 1
13+
tempans = n
14+
tempans += self.Sum_Solution(n - 1)
15+
return tempans
16+
17+
18+
s=Solution()
19+
print s.Sum_Solution(3)

SwordToOffer/Code/48.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding:utf-8 -*-
2+
'''
3+
不用加减乘除做加法
4+
写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
5+
'''
6+
class Solution:
7+
def Add(self, num1, num2):
8+
# write code here
9+
a = (num1 & num2) << 1
10+
b = num1 ^ num2
11+
sum=a|b
12+
while a != 0:
13+
c = a
14+
d = b
15+
16+
a = (c & d) << 1
17+
b = c ^ d
18+
sum=c|d
19+
20+
return sum
21+
22+
23+
s = Solution()
24+
print s.Add(111, 899)

0 commit comments

Comments
 (0)