Object: Binary Tree
Depth First Traversals:
(a) Inorder (Left, Root, Right) : 4 2 5 1 3
(b) Preorder (Root, Left, Right) : 1 2 4 5 3
(c) Postorder (Left, Right, Root) : 4 5 2 3 1
isalnum()
isalpha()
isnumeric()
1 | # 判断是否整个string都由alpha and numbers 组成 |
Boolean
1 | # No space in string |
Remove all elements from a linked list of integers that have value val.
Example:
1
2
3 > Input: 1->2->6->3->4->5->6, val = 6
> Output: 1->2->3->4->5
>
pre —> 通过检验的Node
dummy —> 标记head pre, 防止head消失
Note:val相等的情况有可能连续出现,所以只有已验证 != val, 才能pre = pre.next
Note2: 如果写while pre.next
, 前面要有while pre
所以: while pre and pre.next
1 | # Definition for singly-linked list. |
遍历 0—> x, 直到遇到 sqrt(x)
1 | class Solution: |
Use Binary Search, 降低搜索时间
1 | class Solution: |
int()
int()
不需要import math
int()
直接舍弃小数点后位数1 | 3.5) int( |
math.ceil()
, math.floor()
import math
math.ceil()
return 最大值math.floor()
return 最小值1 | import math |
round
1 | round(x [, n]) |
浮点数x的四舍五入值, 默认返回整数
round遇到5时,并不一定是四舍五入,受计算机精度影响
1 | 3.6) round( |
round()
1 | 2.333334,2) round( |
ord()
和chr()
, unichr()
相反
ord('A')
: ‘A ‘ —> 65
ord(u'\u2020')
: return 8224
chr(65)
: 65 —> A
ASCII code for ‘A’ is 65, ‘a’ is 97
1 | 'A') ord( |
Note:
if {A:1, B:2, ….}
ord('A')-64
注意最开始A为65,如果设定A为1,则 - 64
Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).
Example 1:
1
2
3
4 > Input: 11
> Output: 3
> Explanation: Integer 11 has binary representation 00000000000000000000000000001011
>
Example 2:
1
2
3
4 > Input: 128
> Output: 1
> Explanation: Integer 128 has binary representation 00000000000000000000000010000000
>
1 | class Solution(object): |
Int
一共32为,每位数字检验是否为1
具体操作:检验最后一个数字,检验完右移,一共32次
1 | class Solution(object): |
N = N & (N-1)
可以消除N的二进制表示的最后一个1
N | N-1 | N & (N-1) |
---|---|---|
1011 | 1010 | 1010 |
1010 | 1001 | 1000 |
1000 | 0111 | 0000 |
1 | class Solution(object): |
Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
Note:
- The given integer is guaranteed to fit within the range of a 32-bit signed integer.
- You could assume no leading zero bit in the integer’s binary representation.
Example 1:
1
2
3
4 > Input: 5
> Output: 2
> Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
>
Example 2:
1
2
3
4 > Input: 1
> Output: 0
> Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
>
1 ^ 1 = 0
0 ^ 1 = 1
==> 11111 ^ 10101 = 01010 —> The answer
==> Target: Establish 1111111 (len == num的二进制数字)
n = len(bin(num))-2
设定num的二进制长度n 为 6
6个1: $111111 —> 2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5= 2^6 -1$
1 | class Solution: |
设定为5
十进制 | 2进制 | |
---|---|---|
5 | …000101 | |
~5 | …111010 | 010部分为ans |
Target | …111000 | Target^ ~num为ans |
Target: …..111000
1 | mask = ~0 |
1 | class Solution: |
bin()
函数 in PythonInput type: int/ long int
Return type: String
将int/ long int 转换为:二进制表示的String, 数字前面+ “0b”, 表示二进制
1 | S = bin(2) |
既然返回值为String,就可以用String的方式来使用
EX:ans = bin(2^3).count("1")
bin(2^3)
—> 0b111
"0b111".count("1")
—>3
int(,)
函数 in Pythonint(String 二进制表示,2)
The Input String can directly be the number, or the bin()
represents
1 | I1 = int('11',2) |
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
Given two integers
x
andy
, calculate the Hamming distance.Note:
0 ≤x
,y
< 231.Example:
1
2
3
4
5
6
7
8
9
10
11 > Input: x = 1, y = 4
>
> Output: 2
>
> Explanation:
> 1 (0 0 0 1)
> 4 (0 1 0 0)
> ↑ ↑
>
> The above arrows point to positions where the corresponding bits are different.
>
1 | class Solution: |