8000 2017 8.29 · pythonpeixun/interview_python@2235846 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2235846

Browse files
committed
2017 8.29
1 parent c2947cd commit 2235846

File tree

1 file changed

+84
-1
lines changed

1 file changed

+84
-1
lines changed

Readme.md

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1160,7 +1160,11 @@ l2 = []
11601160
[l2.append(i) for i in l1 if not i in l2]
11611161
```
11621162

1163-
面试官提到的,先排序然后删除.
1163+
sorted排序并且用列表推导式.
1164+
1165+
l = ['b','c','d','b','c','a','a']
1166+
[single.append(i) for i in sorted(l) if i not in single]
1167+
print single
11641168

11651169
## 6 链表成对调换
11661170

@@ -1184,6 +1188,35 @@ class Solution:
11841188
return head
11851189
```
11861190

1191+
```python
1192+
class ListNode:
1193+
def __init__(self, x):
1194+
self.val = x
1195+
self.next = None
1196+
1197+
class nonrecurse(head):
1198+
if head is None or head.next is None:
1199+
return head
1200+
pre = None
1201+
cur = head
1202+
h = head
1203+
while cur:
1204+
h = cur
1205+
1206+
temp = cur.next
1207+
cur.next = pre
1208+
1209+
pre = cur
1210+
cur = temp
1211+
return h
1212+
1213+
1214+
```
1215+
1216+
思路: http://blog.csdn.net/feliciafay/article/details/6841115
1217+
方法: http://www.xuebuyuan.com/2066385.html?mobile=1
1218+
1219+
11871220
## 7 创建字典的方法
11881221

11891222
### 1 直接创建
@@ -1236,6 +1269,20 @@ def recursion_merge_sort2(l1, l2):
12361269

12371270
循环算法
12381271

1272+
思路:
1273+
1274+
定义一个新的空列表
1275+
1276+
比较两个列表的首个元素
1277+
1278+
小的就插入到新列表里
1279+
1280+
把已经插入新列表的元素从旧列表删除
1281+
1282+
直到两个旧列表有一个为空
1283+
1284+
再把旧列表加到新列表后面
1285+
12391286
```pyhton
12401287
def loop_merge_sort(l1, l2):
12411288
tmp = []
@@ -1284,6 +1331,42 @@ def node(l1, l2):
12841331
l2 = l2.next
12851332
```
12861333

1334+
修改了一下:
1335+
```
1336+
#coding:utf-8
1337+
class ListNode:
1338+
def __init__(self, x):
1339+
self.val = x
1340+
self.next = None
1341+
1342+
def node(l1, l2):
1343+
length1, length2 = 0, 0
1344+
# 求两个链表长度
1345+
while l1.next:
1346+
l1 = l1.next#尾节点
1347+
length1 += 1
1348+
while l2.next:
1349+
l2 = l2.next#尾节点
1350+
length2 += 1
1351+
1352+
#如果相交
1353+
if l1.next == l2.next:
1354+
# 长的链表先走
1355+
if length1 > length2:
1356+
for _ in range(length1 - length2):
1357+
l1 = l1.next
1358+
return l1#返回交点
1359+
else:
1360+
for _ in range(length2 - length1):
1361+
l2 = l2.next
1362+
return l2#返回交点
1363+
# 如果不相交
1364+
else:
1365+
return
1366+
```
1367+
1368+
思路: http://humaoli.blog.163.com/blog/static/13346651820141125102125995/
1369+
12871370
## 10 二分查找
12881371

12891372
```python

0 commit comments

Comments
 (0)
0