Skip to content

Commit dc56d78

Browse files
committed
python-practice
1 parent 6f9ef16 commit dc56d78

4 files changed

Lines changed: 494 additions & 0 deletions

File tree

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"每个非负整数 N 都有其二进制表示。例如, 5 可以被表示为二进制 \"101\",11 可以用二进制 \"1011\" 表示,依此类推。注意,除 N = 0 外,任何二进制表示中都不含前导零。\n",
9+
"\n",
10+
"二进制的反码表示是将每个 1 改为 0 且每个 0 变为 1。例如,二进制数 \"101\" 的二进制反码为 \"010\"\n",
11+
"\n",
12+
"给定十进制数 N,返回其二进制表示的反码所对应的十进制整数。\n",
13+
"\n",
14+
" \n",
15+
"\n",
16+
"示例 1:\n",
17+
"\n",
18+
"- 输入:5\n",
19+
"- 输出:2\n",
20+
"- 解释:5 的二进制表示为 \"101\",其二进制反码为 \"010\",也就是十进制中的 2 。\n",
21+
"\n",
22+
"#### 思路\n",
23+
"- 十进制转化为二进制\n",
24+
"- 二进制遍历转化为反码\n",
25+
"- 反码结果转化为十进制"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 20,
31+
"metadata": {
32+
"ExecuteTime": {
33+
"end_time": "2019-07-03T04:06:31.572554Z",
34+
"start_time": "2019-07-03T04:06:31.554224Z"
35+
}
36+
},
37+
"outputs": [
38+
{
39+
"data": {
40+
"text/plain": [
41+
"5"
42+
]
43+
},
44+
"execution_count": 20,
45+
"metadata": {},
46+
"output_type": "execute_result"
47+
}
48+
],
49+
"source": [
50+
"class Solution:\n",
51+
" def bitwiseComplement(self, N: int) -> int:\n",
52+
" result = 0\n",
53+
" temp = list(bin(N)[2:][::-1])\n",
54+
" for i, v in enumerate(temp):\n",
55+
" if v == '1':\n",
56+
" temp[i] = '0'\n",
57+
" else:\n",
58+
" temp[i] = '1'\n",
59+
" result += int(temp[i])*(2**i)\n",
60+
" return result\n",
61+
" \n",
62+
"N = 10\n",
63+
"solution = Solution()\n",
64+
"solution.bitwiseComplement(N)"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 24,
70+
"metadata": {
71+
"ExecuteTime": {
72+
"end_time": "2019-07-03T04:16:04.067626Z",
73+
"start_time": "2019-07-03T04:16:04.053568Z"
74+
}
75+
},
76+
"outputs": [
77+
{
78+
"data": {
79+
"text/plain": [
80+
"5"
81+
]
82+
},
83+
"execution_count": 24,
84+
"metadata": {},
85+
"output_type": "execute_result"
86+
}
87+
],
88+
"source": [
89+
"class Solution:\n",
90+
" def bitwiseComplement(self, N: int) -> int:\n",
91+
" return int(\"\".join(['0' if i == '1' else '1' for i in bin(N)[2:]]), 2)\n",
92+
" \n",
93+
"N = 10\n",
94+
"solution = Solution()\n",
95+
"solution.bitwiseComplement(N)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": []
104+
}
105+
],
106+
"metadata": {
107+
"kernelspec": {
108+
"display_name": "python pratice",
109+
"language": "python",
110+
"name": "env_name"
111+
},
112+
"language_info": {
113+
"codemirror_mode": {
114+
"name": "ipython",
115+
"version": 3
116+
},
117+
"file_extension": ".py",
118+
"mimetype": "text/x-python",
119+
"name": "python",
120+
"nbconvert_exporter": "python",
121+
"pygments_lexer": "ipython3",
122+
"version": "3.7.1"
123+
},
124+
"toc": {
125+
"base_numbering": 1,
126+
"nav_menu": {},
127+
"number_sections": true,
128+
"sideBar": true,
129+
"skip_h1_title": false,
130+
"title_cell": "Table of Contents",
131+
"title_sidebar": "Contents",
132+
"toc_cell": false,
133+
"toc_position": {},
134+
"toc_section_display": true,
135+
"toc_window_display": false
136+
}
137+
},
138+
"nbformat": 4,
139+
"nbformat_minor": 2
140+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"编写一个算法来判断一个数是不是“快乐数”。\n",
9+
"\n",
10+
"一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。\n",
11+
"\n",
12+
"示例: \n",
13+
"\n",
14+
"输入: 19\n",
15+
"输出: true\n",
16+
"解释: \n",
17+
"- 12 + 92 = 82\n",
18+
"- 82 + 22 = 68\n",
19+
"- 62 + 82 = 100\n",
20+
"- 12 + 02 + 02 = 1\n",
21+
"\n",
22+
"#### 思路\n",
23+
"- 注意,非快乐树会进入结果为4的死循环"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 25,
29+
"metadata": {
30+
"ExecuteTime": {
31+
"end_time": "2019-07-03T05:19:27.505676Z",
32+
"start_time": "2019-07-03T05:19:27.490526Z"
33+
}
34+
},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"False"
40+
]
41+
},
42+
"execution_count": 25,
43+
"metadata": {},
44+
"output_type": "execute_result"
45+
}
46+
],
47+
"source": [
48+
"class Solution:\n",
49+
" def isHappy(self, n: int) -> bool:\n",
50+
" result = 0\n",
51+
" for i in str(n):\n",
52+
" result += int(i)**2\n",
53+
" if result == 1:\n",
54+
" return True\n",
55+
" elif result == 4:\n",
56+
" return False\n",
57+
" else:\n",
58+
" return self.isHappy(result)\n",
59+
" \n",
60+
"n = 6\n",
61+
"solution = Solution()\n",
62+
"solution.isHappy(n)"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": null,
68+
"metadata": {},
69+
"outputs": [],
70+
"source": []
71+
}
72+
],
73+
"metadata": {
74+
"kernelspec": {
75+
"display_name": "python pratice",
76+
"language": "python",
77+
"name": "env_name"
78+
},
79+
"language_info": {
80+
"codemirror_mode": {
81+
"name": "ipython",
82+
"version": 3
83+
},
84+
"file_extension": ".py",
85+
"mimetype": "text/x-python",
86+
"name": "python",
87+
"nbconvert_exporter": "python",
88+
"pygments_lexer": "ipython3",
89+
"version": "3.7.1"
90+
},
91+
"toc": {
92+
"base_numbering": 1,
93+
"nav_menu": {},
94+
"number_sections": true,
95+
"sideBar": true,
96+
"skip_h1_title": false,
97+
"title_cell": "Table of Contents",
98+
"title_sidebar": "Contents",
99+
"toc_cell": false,
100+
"toc_position": {},
101+
"toc_section_display": true,
102+
"toc_window_display": false
103+
}
104+
},
105+
"nbformat": 4,
106+
"nbformat_minor": 2
107+
}
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"每个非负整数 N 都有其二进制表示。例如, 5 可以被表示为二进制 \"101\",11 可以用二进制 \"1011\" 表示,依此类推。注意,除 N = 0 外,任何二进制表示中都不含前导零。\n",
9+
"\n",
10+
"二进制的反码表示是将每个 1 改为 0 且每个 0 变为 1。例如,二进制数 \"101\" 的二进制反码为 \"010\"\n",
11+
"\n",
12+
"给定十进制数 N,返回其二进制表示的反码所对应的十进制整数。\n",
13+
"\n",
14+
" \n",
15+
"\n",
16+
"示例 1:\n",
17+
"\n",
18+
"- 输入:5\n",
19+
"- 输出:2\n",
20+
"- 解释:5 的二进制表示为 \"101\",其二进制反码为 \"010\",也就是十进制中的 2 。\n",
21+
"\n",
22+
"#### 思路\n",
23+
"- 十进制转化为二进制\n",
24+
"- 二进制遍历转化为反码\n",
25+
"- 反码结果转化为十进制"
26+
]
27+
},
28+
{
29+
"cell_type": "code",
30+
"execution_count": 20,
31+
"metadata": {
32+
"ExecuteTime": {
33+
"end_time": "2019-07-03T04:06:31.572554Z",
34+
"start_time": "2019-07-03T04:06:31.554224Z"
35+
}
36+
},
37+
"outputs": [
38+
{
39+
"data": {
40+
"text/plain": [
41+
"5"
42+
]
43+
},
44+
"execution_count": 20,
45+
"metadata": {},
46+
"output_type": "execute_result"
47+
}
48+
],
49+
"source": [
50+
"class Solution:\n",
51+
" def bitwiseComplement(self, N: int) -> int:\n",
52+
" result = 0\n",
53+
" temp = list(bin(N)[2:][::-1])\n",
54+
" for i, v in enumerate(temp):\n",
55+
" if v == '1':\n",
56+
" temp[i] = '0'\n",
57+
" else:\n",
58+
" temp[i] = '1'\n",
59+
" result += int(temp[i])*(2**i)\n",
60+
" return result\n",
61+
" \n",
62+
"N = 10\n",
63+
"solution = Solution()\n",
64+
"solution.bitwiseComplement(N)"
65+
]
66+
},
67+
{
68+
"cell_type": "code",
69+
"execution_count": 24,
70+
"metadata": {
71+
"ExecuteTime": {
72+
"end_time": "2019-07-03T04:16:04.067626Z",
73+
"start_time": "2019-07-03T04:16:04.053568Z"
74+
}
75+
},
76+
"outputs": [
77+
{
78+
"data": {
79+
"text/plain": [
80+
"5"
81+
]
82+
},
83+
"execution_count": 24,
84+
"metadata": {},
85+
"output_type": "execute_result"
86+
}
87+
],
88+
"source": [
89+
"class Solution:\n",
90+
" def bitwiseComplement(self, N: int) -> int:\n",
91+
" return int(\"\".join(['0' if i == '1' else '1' for i in bin(N)[2:]]), 2)\n",
92+
" \n",
93+
"N = 10\n",
94+
"solution = Solution()\n",
95+
"solution.bitwiseComplement(N)"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": null,
101+
"metadata": {},
102+
"outputs": [],
103+
"source": []
104+
}
105+
],
106+
"metadata": {
107+
"kernelspec": {
108+
"display_name": "python pratice",
109+
"language": "python",
110+
"name": "env_name"
111+
},
112+
"language_info": {
113+
"codemirror_mode": {
114+
"name": "ipython",
115+
"version": 3
116+
},
117+
"file_extension": ".py",
118+
"mimetype": "text/x-python",
119+
"name": "python",
120+
"nbconvert_exporter": "python",
121+
"pygments_lexer": "ipython3",
122+
"version": "3.7.1"
123+
},
124+
"toc": {
125+
"base_numbering": 1,
126+
"nav_menu": {},
127+
"number_sections": true,
128+
"sideBar": true,
129+
"skip_h1_title": false,
130+
"title_cell": "Table of Contents",
131+
"title_sidebar": "Contents",
132+
"toc_cell": false,
133+
"toc_position": {},
134+
"toc_section_display": true,
135+
"toc_window_display": false
136+
}
137+
},
138+
"nbformat": 4,
139+
"nbformat_minor": 2
140+
}

0 commit comments

Comments
 (0)