8000 Merge pull request #28 from Tony-CCIE/master · pythonpeixun/interview_python@92b625a · GitHub
[go: up one dir, main page]

Skip to content

Commit 92b625a

Browse files
authored
Merge pull request taizilongxu#28 from Tony-CCIE/master
update
2 parents 2547309 + 075459d commit 92b625a

File tree

1 file changed

+86
-2
lines changed

1 file changed

+86
-2
lines changed

Readme.md

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
- [19 求两棵树是否相同](#19-求两棵树是否相同)
113113
- [20 前序中序求后序](#20-前序中序求后序)
114114
- [21 单链表逆置](#21-单链表逆置)
115-
115+
- [22 两个字符串是否是变位词](#22-两个字符串是否是变位词)
116116
<!-- markdown-toc end -->
117117

118118
# Python语言特性
@@ -847,7 +847,7 @@ InnoDB 的趋势会是一个非常复杂的存储引擎,对于一些小的应
847847

848848
## 3 ARP协议
849849

850-
地址解析协议(Address Resolution Protocol): 根据IP地址获取物理地址的一个TCP/IP协议
850+
地址解析协议(Address Resolution Protocol),其基本功能为透过目标设备的IP地址,查询目标的MAC地址,以保证通信的顺利进行。它是IPv4网络层必不可少的协议,不过在IPv6中已不再适用,并被邻居发现协议(NDP)所替代。
851851

852852
## 4 urllib和urllib2的区别
853853

@@ -1437,3 +1437,87 @@ while root:
14371437
print root.data
14381438
root = root.next
14391439
```
1440+
1441+
## 22 两个字符串是否是变位词
1442+
1443+
```python
1444+
class Anagram:
1445+
"""
1446+
@:param s1: The first string
1447+
@:param s2: The second string
1448+
@:return true or false
1449+
"""
1450+
def Solution1(s1,s2):
1451+
alist = list(s2)
1452+
1453+
pos1 = 0
1454+
stillOK = True
1455+
1456+
while pos1 < len(s1) and stillOK:
1457+
pos2 = 0
1458+
found = False
1459+
while pos2 < len(alist) and not found:
1460+
if s1[pos1] == alist[pos2]:
1461+
found = True
1462+
else:
1463+
pos2 = pos2 + 1
1464+
1465+
if found:
1466+
alist[pos2] = None
1467+
else:
1468+
stillOK = False
1469+
1470+
pos1 = pos1 + 1
1471+
1472+
return stillOK
1473+
1474+
print(Solution1('abcd','dcba'))
1475+
1476+
def Solution2(s1,s2):
1477+
alist1 = list(s1)
1478+
alist2 = list(s2)
1479+
1480+
alist1.sort()
1481+
alist2.sort()
1482+
1483+
1484+
pos = 0
1485+
matches = True
1486+
1487+
while pos < len(s1) and matches:
1488+
if alist1[pos] == alist2[pos]:
1489+
pos = pos + 1
1490+
else:
1491+
matches = False
1492+
1493+
return matches
1494+
1495+
print(Solution2('abcde','edcbg'))
1496+
1497+
def Solution3(s1,s2):
1498+
c1 = [0]*26
1499+
c2 = [0]*26
1500+
1501+
for i in range(len(s1)):
1502+
pos = ord(s1[i])-ord('a')
1503+
c1[pos] = c1[pos] + 1
1504+
1505+
for i in range(len(s2)):
1506+
pos = ord(s2[i])-ord('a')
1507+
c2[pos] = c2[pos] + 1
1508+
1509+
j = 0
1510+
stillOK = True
1511+
while j<26 and stillOK:
1512+
if c1[j] == c2[j]:
1513+
j = j + 1
1514+
else:
1515+
stillOK = False
1516+
1517+
return stillOK
1518+
1519+
print(Solution3('apple','pleap'))
1520+
1521+
```
1522+
1523+

0 commit comments

Comments
 (0)
0