Skip to content

Commit 199fb02

Browse files
committed
2.19
1 parent d61b463 commit 199fb02

20 files changed

+296
-4
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@
1111
- [***矩形覆盖***](./SwordToOffer/Doc/矩形覆盖.md)
1212
- [二进制中1的个数](./SwordToOffer/Doc/二进制中1的个数.md)
1313
- [***数值的整数次方***](./SwordToOffer/Doc/数值的整数次方.md)
14+
- [调整数组顺序使奇数位于偶数前面](./SwordToOffer/Doc/调整数组顺序使奇数位于偶数前面.md)
15+
- [链表中倒数第k个节点](./SwordToOffer/Doc/链表中倒数第k个节点.md)
16+
- [反转链表](./SwordToOffer/Doc/反转链表.md)
17+
- [合并两个排序的链表](./SwordToOffer/Doc/合并两个排序的链表.md)
18+
- [树的子结构](./SwordToOffer/Doc/树的子结构.md)
1419

SwordToOffer/Code/12.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# -*- coding:utf-8 -*-
2+
class Solution:
3+
def reOrderArray(self, array):
4+
# write code here
5+
6+
jiCount = 0
7+
ouCount = 0
8+
9+
for itemArray in array:
10+
if itemArray % 2 == 0:
11+
ouCount += 1
12+
else:
13+
jiCount += 1
14+
15+
jiIndex = 0
16+
ouIndex = jiCount
17+
18+
arraySize = len(array)
19+
newArray = [0] * arraySize
20+
21+
for i in range(arraySize):
22+
if array[i] % 2 == 1:
23+
newArray[jiIndex] = array[i]
24+
jiIndex += 1
25+
26+
else:
27+
newArray[ouIndex] = array[i]
28+
ouIndex += 1
29+
30+
return newArray
31+
32+
33+
solution = Solution()
34+
print solution.reOrderArray([1, 2, 3, 4, 5])

SwordToOffer/Code/13.py

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# -*- coding:utf-8 -*-
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
8+
class Solution:
9+
def FindKthToTail(self, head, k):
10+
# write code here
11+
if head == None or k <= 0:
12+
return None
13+
node = head
14+
node2 = head
15+
while node.next != None:
16+
if k == 1:
17+
node2 = node2.next
18+
else:
19+
k -= 1
20+
node = node.next
21+
22+
if k != 1:
23+
return None
24+
25+
return node2
26+
27+
28+
solution = Solution()
29+
head = ListNode(1)
30+
node1 = ListNode(2)
31+
node2 = ListNode(3)
32+
node3 = ListNode(4)
33+
node4 = ListNode(5)
34+
35+
head.next = node1
36+
node1.next = node2
37+
node2.next = node3
38+
# node3.next = node4
39+
40+
print solution.FindKthToTail(head, 20)

SwordToOffer/Code/14.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# -*- coding:utf-8 -*-
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
8+
class Solution:
9+
# 返回ListNode
10+
def ReverseList(self, pHead):
11+
# write code here
12+
if pHead == None:
13+
return None
14+
15+
newHead=ListNode(pHead.val)
16+
newHead.next=None
17+
18+
pHead=pHead.next
19+
20+
while pHead!=None:
21+
cHead=ListNode(pHead.val)
22+
cHead.next=newHead
23+
newHead=cHead
24+
25+
pHead=pHead.next
26+
27+
return newHead
28+
29+
30+
head = ListNode(1)
31+
node1 = ListNode(2)
32+
node2 = ListNode(3)
33+
node3 = ListNode(4)
34+
node4 = ListNode(5)
35+
36+
head.next = node1
37+
node1.next = node2
38+
node2.next = node3
39+
node3.next = node4
40+
solution = Solution()
41+
newHead=solution.ReverseList(head)
42+
a=1

SwordToOffer/Code/15.py

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding:utf-8 -*-
2+
class ListNode:
3+
def __init__(self, x):
4+
self.val = x
5+
self.next = None
6+
7+
8+
import copy
9+
10+
11+
class Solution:
12+
# 返回合并后列表
13+
def Merge(self, pHead1, pHead2):
14+
# write code here
15+
16+
if pHead1 != None and pHead2 != None:
17+
if pHead1.val <= pHead2.val:
18+
newHead = pHead1
19+
pHead1 = pHead1.next
20+
else:
21+
newHead = pHead2
22+
pHead2 = pHead2.next
23+
elif pHead1!=None:
24+
return pHead1
25+
else:
26+
return pHead2
27+
28+
29+
curHead = newHead
30+
31+
while pHead1 != None and pHead2 != None:
32+
if pHead1.val <= pHead2.val:
33+
curHead.next = pHead1
34+
35+
pHead1 = pHead1.next
36+
else:
37+
curHead.next = pHead2
38+
pHead2 = pHead2.next
39+
40+
curHead = curHead.next
41+
42+
if pHead1 != None:
43+
curHead.next = pHead1
44+
if pHead2 != None:
45+
curHead.next = pHead2
46+
47+
return newHead
48+
49+
50+
solution = Solution()
51+
head = ListNode(1)
52+
node1 = ListNode(3)
53+
node2 = ListNode(5)
54+
node3 = ListNode(7)
55+
node4 = ListNode(9)
56+
57+
head.next = node1
58+
node1.next = node2
59+
node2.next = node3
60+
node3.next = node4
61+
62+
headb = ListNode(2)
63+
node1b = ListNode(4)
64+
node2b = ListNode(6)
65+
node3b = ListNode(8)
66+
node4b = ListNode(10)
67+
68+
headb.next = node1b
69+
node1b.next = node2b
70+
node2b.next = node3b
71+
node3b.next = node4b
72+
73+
newHead = solution.Merge(head, headb)
74+
a = 0

SwordToOffer/Code/16.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
8+
9+
class Solution:
10+
def HasSubtree(self, pRoot1, pRoot2):
11+
# write code here
12+
if pRoot1==None or pRoot2==None:
13+
return False
14+
15+

SwordToOffer/Doc/doc-sample.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# title
1+
#
2+
3+
<center>知识点:</center>
24

35

46
## 题目描述

SwordToOffer/Doc/二维数组中的查找.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
二维数组中的查找.md
1+
# 二维数组中的查找.md
2+
3+
<center>数组</center>
24

35
## 题目描述
46

SwordToOffer/Doc/二进制中1的个数.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 二进制中1的个数
22

3+
<center>知识点:位运算</center>
4+
35

46
## 题目描述
57
输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

SwordToOffer/Doc/反转链表.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 反转链表
2+
3+
<center>知识点:链表</center>
4+
5+
6+
## 题目描述
7+
输入一个链表,反转链表后,输出新链表的表头。
8+
## 解题思路
9+
10+
没啥很复杂的思路,就是基础的指针
11+
12+
## 代码
13+
14+
[这里](../Code/14.py)

SwordToOffer/Doc/变态跳台阶.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 变态跳台阶
22

3+
<center>知识点:递归和循环</center>
4+
35

46
## 题目描述
57
一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级。求该青蛙跳上一个n级的台阶总共有多少种跳法。
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# 合并两个排序的链表
2+
3+
<center>知识点:链表</center>
4+
5+
6+
## 题目描述
7+
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
8+
9+
## 代码
10+
11+
[这里](../Code/15.py)

SwordToOffer/Doc/数值的整数次方.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 数值的整数次方
22

3+
<center>知识点:代码的完整性</center>
4+
35

46
## 题目描述
57
给定一个double类型的浮点数base和int类型的整数exponent。求base的exponent次方。

SwordToOffer/Doc/旋转数组的最小数字.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 旋转数组的最小数字
22

3-
<center>知识点:查找</center>
3+
<center>知识点:查找和排序</center>
44

55
## 题目描述
66
把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

SwordToOffer/Doc/替换空格.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 题目
22

3-
<center>知识点:xx</center>
3+
<center>知识点:字符串</center>
44

55
## 题目描述
66
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

SwordToOffer/Doc/树的子结构.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 树的子结构
2+
3+
<center>知识点:树</center>
4+
5+
6+
## 题目描述
7+
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
8+
## 解题思路
9+
10+
11+
12+
## 代码
13+
14+
[这里](../Code/16.py)

SwordToOffer/Doc/矩形覆盖.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 矩形覆盖
22

3+
<center>知识点:递归和循环</center>
4+
35

46
## 题目描述
57
我们可以用2$\times$1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2$\times$1的小矩形无重叠地覆盖一个2$\times​$n的大矩形,总共有多少种方法?
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 调整数组顺序使奇数位于偶数前面
2+
3+
<center>知识点:数组</center>
4+
5+
6+
## 题目描述
7+
输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有的偶数位于数组的后半部分,并保证奇数和奇数,偶数和偶数之间的相对位置不变。
8+
## 解题思路
9+
10+
遍历两次,第一次求出数组中奇数的个数,设两个下标用来标记奇数和偶数的下标,偶数下标从奇数个数开始,奇数下标从0开始,初始化一个相同大小的新数组,然后第二次遍历,如果是奇数将其放入新数组奇数下标对应的位置,同时更新奇数下标加一,如果是偶数则放入新数组偶数下标对应的位置,同时更新偶数下标加一。
11+
12+
## 代码
13+
14+
[这里](../Code/12.py)

SwordToOffer/Doc/跳台阶.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 跳台阶
22

3+
<center>知识点:递归和循环</center>
4+
35

46
## 题目描述
57

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# 链表中倒数第k个节点
2+
3+
<center>知识点:链表</center>
4+
5+
6+
## 题目描述
7+
输入一个链表,输出该链表中倒数第k个结点。
8+
9+
## 解题思路
10+
设两个指针,初始使都指向head节点,然后让第一个指针先走k步,然后之后的步骤让两个指针一起向后移动,当第一个指针到达最后的时候第二个指针刚好会在倒数第k个位置,需要注意head为空或者k小于0或者k的长度大于链表的节点个数的情况。
11+
12+
13+
## 代码
14+
15+
[这里](../Code/13.py)

0 commit comments

Comments
 (0)