Skip to content

Commit 6f9ef16

Browse files
committed
python-practice
1 parent 9d5bbbe commit 6f9ef16

6 files changed

Lines changed: 618 additions & 0 deletions
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"给定一个非负整数 num,反复将各个位上的数字相加,直到结果为一位数。\n",
9+
"\n",
10+
"示例:\n",
11+
"\n",
12+
"- 输入: 38\n",
13+
"- 输出: 2 \n",
14+
"- 解释: 各位相加的过程为:3 + 8 = 11, 1 + 1 = 2。 由于 2 是一位数,所以返回 2"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 11,
20+
"metadata": {
21+
"ExecuteTime": {
22+
"end_time": "2019-07-02T13:00:51.819778Z",
23+
"start_time": "2019-07-02T13:00:51.809399Z"
24+
}
25+
},
26+
"outputs": [
27+
{
28+
"data": {
29+
"text/plain": [
30+
"4"
31+
]
32+
},
33+
"execution_count": 11,
34+
"metadata": {},
35+
"output_type": "execute_result"
36+
}
37+
],
38+
"source": [
39+
"class Solution(object):\n",
40+
" def addDigits(self, num):\n",
41+
" \"\"\"\n",
42+
" :type num: int\n",
43+
" :rtype: int\n",
44+
" \"\"\"\n",
45+
" result = 0\n",
46+
" for i in str(num):\n",
47+
" result += int(i)\n",
48+
" return result if result <= 9 else self.addDigits(result)\n",
49+
"\n",
50+
"num = 22\n",
51+
"solution = Solution()\n",
52+
"solution.addDigits(num)"
53+
]
54+
}
55+
],
56+
"metadata": {
57+
"kernelspec": {
58+
"display_name": "python pratice",
59+
"language": "python",
60+
"name": "env_name"
61+
},
62+
"language_info": {
63+
"codemirror_mode": {
64+
"name": "ipython",
65+
"version": 3
66+
},
67+
"file_extension": ".py",
68+
"mimetype": "text/x-python",
69+
"name": "python",
70+
"nbconvert_exporter": "python",
71+
"pygments_lexer": "ipython3",
72+
"version": "3.7.1"
73+
},
74+
"toc": {
75+
"base_numbering": 1,
76+
"nav_menu": {},
77+
"number_sections": true,
78+
"sideBar": true,
79+
"skip_h1_title": false,
80+
"title_cell": "Table of Contents",
81+
"title_sidebar": "Contents",
82+
"toc_cell": false,
83+
"toc_position": {},
84+
"toc_section_display": true,
85+
"toc_window_display": false
86+
}
87+
},
88+
"nbformat": 4,
89+
"nbformat_minor": 2
90+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离。 \n",
9+
"\n",
10+
"如果没有两个连续的 1,返回 0 。\n",
11+
"\n",
12+
" \n",
13+
"\n",
14+
"示例 1:\n",
15+
"\n",
16+
"- 输入:22\n",
17+
"- 输出:2\n",
18+
"\n",
19+
"解释:\n",
20+
"- 22 的二进制是 0b10110 。\n",
21+
"- 在 22 的二进制表示中,有三个 1,组成两对连续的 1 。\n",
22+
"- 第一对连续的 1 中,两个 1 之间的距离为 2 。\n",
23+
"- 第二对连续的 1 中,两个 1 之间的距离为 1 。\n",
24+
"- 答案取两个距离之中最大的,也就是 2 。\n",
25+
"\n",
26+
"---\n",
27+
"#### 思路\n",
28+
"正常解法\n",
29+
"- 遍历,记录两个连续的1之间的距离\n",
30+
"- 比较记录的距离的最大值"
31+
]
32+
},
33+
{
34+
"cell_type": "code",
35+
"execution_count": 12,
36+
"metadata": {
37+
"ExecuteTime": {
38+
"end_time": "2019-07-03T03:36:17.949541Z",
39+
"start_time": "2019-07-03T03:36:17.935767Z"
40+
}
41+
},
42+
"outputs": [
43+
{
44+
"data": {
45+
"text/plain": [
46+
"2"
47+
]
48+
},
49+
"execution_count": 12,
50+
"metadata": {},
51+
"output_type": "execute_result"
52+
}
53+
],
54+
"source": [
55+
"class Solution:\n",
56+
" def binaryGap(self, N: int) -> int:\n",
57+
" result, last_one = 0, 0\n",
58+
" for i, v in enumerate(bin(N)[2:]):\n",
59+
" if v == '1':\n",
60+
" result = max(result, i - last_one)\n",
61+
" last_one = i\n",
62+
" return result\n",
63+
" \n",
64+
"N = 22\n",
65+
"solution = Solution()\n",
66+
"solution.binaryGap(N)"
67+
]
68+
},
69+
{
70+
"cell_type": "code",
71+
"execution_count": null,
72+
"metadata": {},
73+
"outputs": [],
74+
"source": []
75+
}
76+
],
77+
"metadata": {
78+
"kernelspec": {
79+
"display_name": "python pratice",
80+
"language": "python",
81+
"name": "env_name"
82+
},
83+
"language_info": {
84+
"codemirror_mode": {
85+
"name": "ipython",
86+
"version": 3
87+
},
88+
"file_extension": ".py",
89+
"mimetype": "text/x-python",
90+
"name": "python",
91+
"nbconvert_exporter": "python",
92+
"pygments_lexer": "ipython3",
93+
"version": "3.7.1"
94+
},
95+
"toc": {
96+
"base_numbering": 1,
97+
"nav_menu": {},
98+
"number_sections": true,
99+
"sideBar": true,
100+
"skip_h1_title": false,
101+
"title_cell": "Table of Contents",
102+
"title_sidebar": "Contents",
103+
"toc_cell": false,
104+
"toc_position": {},
105+
"toc_section_display": true,
106+
"toc_window_display": false
107+
}
108+
},
109+
"nbformat": 4,
110+
"nbformat_minor": 2
111+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"#### 题目描述\n",
8+
"给定一个整数数组 A,对于每个整数 A[i],我们可以选择任意 x 满足 -K <= x <= K,并将 x 加到 A[i] 中。\n",
9+
"\n",
10+
"在此过程之后,我们得到一些数组 B。\n",
11+
"\n",
12+
"返回 B 的最大值和 B 的最小值之间可能存在的最小差值。\n",
13+
"\n",
14+
" \n",
15+
"\n",
16+
"示例 1:\n",
17+
"\n",
18+
"- 输入:A = [1], K = 0\n",
19+
"- 输出:0\n",
20+
"- 解释:B = [1]\n",
21+
"\n",
22+
"示例 2:\n",
23+
"\n",
24+
"- 输入:A = [0,10], K = 2\n",
25+
"- 输出:6\n",
26+
"- 解释:B = [2,8]"
27+
]
28+
},
29+
{
30+
"cell_type": "code",
31+
"execution_count": 1,
32+
"metadata": {
33+
"ExecuteTime": {
34+
"end_time": "2019-07-02T12:10:24.645668Z",
35+
"start_time": "2019-07-02T12:10:24.632696Z"
36+
}
37+
},
38+
"outputs": [
39+
{
40+
"data": {
41+
"text/plain": [
42+
"0"
43+
]
44+
},
45+
"execution_count": 1,
46+
"metadata": {},
47+
"output_type": "execute_result"
48+
}
49+
],
50+
"source": [
51+
"class Solution(object):\n",
52+
" def smallestRangeI(self, A, K):\n",
53+
" \"\"\"\n",
54+
" :type A: List[int]\n",
55+
" :type K: int\n",
56+
" :rtype: int\n",
57+
" \"\"\"\n",
58+
" return max(0, max(A) - min(A) - 2*K)\n",
59+
" \n",
60+
"A = [1]\n",
61+
"K = 0\n",
62+
"solution = Solution()\n",
63+
"solution.smallestRangeI(A, K)"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": null,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": []
72+
}
73+
],
74+
"metadata": {
75+
"kernelspec": {
76+
"display_name": "python pratice",
77+
"language": "python",
78+
"name": "env_name"
79+
},
80+
"language_info": {
81+
"codemirror_mode": {
82+
"name": "ipython",
83+
"version": 3
84+
},
85+
"file_extension": ".py",
86+
"mimetype": "text/x-python",
87+
"name": "python",
88+
"nbconvert_exporter": "python",
89+
"pygments_lexer": "ipython3",
90+
"version": "3.7.1"
91+
},
92+
"toc": {
93+
"base_numbering": 1,
94+
"nav_menu": {},
95+
"number_sections": true,
96+
"sideBar": true,
97+
"skip_h1_title": false,
98+
"title_cell": "Table of Contents",
99+
"title_sidebar": "Contents",
100+
"toc_cell": false,
101+
"toc_position": {},
102+
"toc_section_display": true,
103+
"toc_window_display": false
104+
}
105+
},
106+
"nbformat": 4,
107+
"nbformat_minor": 2
108+
}

0 commit comments

Comments
 (0)