37
37
- [ 26 Python的is] ( #26-python的is )
38
38
- [ 27 read,readline和readlines] ( #27-readreadline和readlines )
39
39
- [ 28 Python2和3的区别] ( #28-python2和3的区别 )
40
+ - [ 29 super.` __init__ ` ()] ( #29-super-init )
41
+ - [ 30 range-and-xrange] ( #30-range-and-xrange )
40
42
- [ 操作系统] ( #操作系统 )
41
43
- [ 1 select,poll和epoll] ( #1-selectpoll和epoll )
42
44
- [ 2 调度算法] ( #2-调度算法 )
110
112
- [ 19 求两棵树是否相同] ( #19-求两棵树是否相同 )
111
113
- [ 20 前序中序求后序] ( #20-前序中序求后序 )
112
114
- [ 21 单链表逆置] ( #21-单链表逆置 )
113
-
115
+ - [ 22 两个字符串是否是变位词 ] ( #22-两个字符串是否是变位词 )
114
116
<!-- markdown-toc end -->
115
117
116
118
# Python语言特性
@@ -137,6 +139,35 @@ print a # [1]
137
139
138
140
所有的变量都可以理解是内存中一个对象的“引用”,或者,也可以看似c中void* 的感觉。
139
141
142
+ 通过` id ` 来看引用` a ` 的内存地址可以比较理解:
143
+
144
+ ``` python
145
+ a = 1
146
+ def fun (a ):
147
+ print " func_in" ,id (a) # func_in 41322472
148
+ a = 2
149
+ print " re-point" ,id (a), id (2 ) # re-point 41322448 41322448
150
+ print " func_out" ,id (a), id (1 ) # func_out 41322472 41322472
151
+ fun(a)
152
+ print a # 1
153
+ ```
154
+
155
+ 注:具体的值在不同电脑上运行时可能不同。
156
+
157
+ 可以看到,在执行完` a = 2 ` 之后,` a ` 引用中保存的值,即内存地址发生变化,由原来` 1 ` 对象的所在的地址变成了` 2 ` 这个实体对象的内存地址。
158
+
159
+ 而第2个例子` a ` 引用保存的内存值就不会发生变化:
160
+
161
+ ``` python
162
+ a = []
163
+ def fun (a ):
164
+ print " func_in" ,id (a) # func_in 53629256
165
+ a.append(1 )
166
+ print " func_out" ,id (a) # func_out 53629256
167
+ fun(a)
168
+ print a # [1]
169
+ ```
170
+
140
171
这里记住的是类型是属于对象的,而不是变量。而对象有两种,“可更改”(mutable)与“不可更改”(immutable)对象。在python中,strings, tuples, 和numbers是不可更改的对象,而list,dict等则是可以修改的对象。(这就是这个问题的重点)
141
172
142
173
当一个引用传递给函数的时候,函数自动复制一份引用,这个函数里的引用和外边的引用没有半毛关系了.所以第一个例子里函数把引用指向了一个不可变对象,当函数返回的时候,外面的引用没半毛感觉.而第二个例子就不一样了,函数内的引用指向的是可变对象,对它的操作就和定位了指针地址一样,在内存里进行修改.
@@ -630,6 +661,22 @@ is是对比地址,==是对比值
630
661
## 28 Python2和3的区别
631
662
推荐:[ Python 2.7.x 与 Python 3.x 的主要差异] ( http://chenqx.github.io/2014/11/10/Key-differences-between-Python-2-7-x-and-Python-3-x/ )
632
663
664
+ ## 29 super init
665
+ super() lets you avoid referring to the base class explicitly, which can be nice. But the main advantage comes with multiple inheritance, where all sorts of fun stuff can happen. See the standard docs on super if you haven't already.
666
+
667
+ Note that the syntax changed in Python 3.0: you can just say super().` __init__ ` () instead of super(ChildB, self).` __init__ ` () which IMO is quite a bit nicer.
668
+
669
+ http://stackoverflow.com/questions/576169/understanding-python-super-with-init-methods
670
+
671
+ ## 30 range and xrange
672
+ 都在循环时使用,xrange内存性能更好。
673
+ for i in range(0, 20):
674
+ for i in xrange(0, 20):
675
+ What is the difference between range and xrange functions in Python 2.X?
676
+ range creates a list, so if you do range(1, 10000000) it creates a list in memory with 9999999 elements.
677
+ xrange is a sequence object that evaluates lazily.
678
+
679
+ http://stackoverflow.com/questions/94935/what-is-the-difference-between-range-and-xrange-functions-in-python-2-x
633
680
634
681
# 操作系统
635
682
@@ -730,7 +777,7 @@ Bulid过程可以分解为4个步骤:预处理(Prepressing), 编译(Compilation)
730
777
731
778
## 6 虚拟内存技术
732
779
733
- 虚拟存储器是值具有请求调入功能和置换功能 ,能从逻辑上对内存容量加以扩充的一种存储系统.
780
+ 虚拟存储器是指具有请求调入功能和置换功能 ,能从逻辑上对内存容量加以扩充的一种存储系统.
734
781
735
782
## 7 分页和分段
736
783
@@ -800,7 +847,7 @@ InnoDB 的趋势会是一个非常复杂的存储引擎,对于一些小的应
800
847
801
848
## 3 ARP协议
802
849
803
- 地址解析协议(Address Resolution Protocol): 根据IP地址获取物理地址的一个TCP/IP协议
850
+ 地址解析协议(Address Resolution Protocol),其基本功能为透过目标设备的IP地址,查询目标的MAC地址,以保证通信的顺利进行。它是IPv4网
6D4E
层必不可少的协议,不过在IPv6中已不再适用,并被邻居发现协议(NDP)所替代。
804
851
805
852
## 4 urllib和urllib2的区别
806
853
@@ -812,6 +859,7 @@ InnoDB 的趋势会是一个非常复杂的存储引擎,对于一些小的应
812
859
813
860
## 5 Post和Get
814
861
[ GET和POST有什么区别?及为什么网上的多数答案都是错的] ( http://www.cnblogs.com/nankezhishi/archive/2012/06/09/getandpost.html )
862
+ [ 知乎回答] ( https://www.zhihu.com/question/31640769?rf=37401322 )
815
863
816
864
get: [ RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1] ( http://tools.ietf.org/html/rfc2616#section-9.3 )
817
865
post: [ RFC 2616 - Hypertext Transfer Protocol -- HTTP/1.1] ( http://tools.ietf.org/html/rfc2616#section-9.5 )
@@ -1034,6 +1082,28 @@ f = lambda n: 1 if n < 2 else f(n - 1) + f(n - 2)
1034
1082
1035
1083
在一个m行n列二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
1036
1084
1085
+ 使用Step-wise线性搜索。
1086
+
1087
+ ``` python
1088
+ def get_value (l , r, c ):
1089
+ return l[r][c]
1090
+
1091
+ def find (l , x ):
1092
+ m = len (l) - 1
1093
+ n = len (l[0 ]) - 1
1094
+ r = 0
1095
+ c = n
1096
+ while c >= 0 and r <= m:
1097
+ value = get_value(l, r, c)
1098
+ if value == x:
1099
+ return True
1100
+ elif value > x:
1101
+ c = c - 1
1102
+ elif value < x:
1103
+ r = r + 1
1104
+ return False
1105
+ ```
1106
+
1037
1107
## 5 去除列表中的重复元素
1038
1108
1039
1109
用集合
@@ -1367,3 +1437,87 @@ while root:
1367
1437
print root.data
1368
1438
root = root.next
1369
1439
```
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