Skip to content

Commit bfd9531

Browse files
author
Xiaopei
committed
leecode
1 parent 73a1236 commit bfd9531

File tree

3 files changed

+250
-0
lines changed

3 files changed

+250
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Interview-Coding
22

33
Archive some common interview questions
4+

binary_search.ipynb

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": null,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"import os\n",
10+
"print(\"hello world\")"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 1,
16+
"metadata": {},
17+
"outputs": [
18+
{
19+
"name": "stdout",
20+
"output_type": "stream",
21+
"text": [
22+
"*************\n"
23+
]
24+
}
25+
],
26+
"source": [
27+
"print(\"*************\")"
28+
]
29+
},
30+
{
31+
"cell_type": "markdown",
32+
"metadata": {},
33+
"source": [
34+
"A message containing letters from A-Z can be encoded into numbers using the following mapping:\n",
35+
"\n",
36+
"'A' -> \"1\"\n",
37+
"'B' -> \"2\"\n",
38+
"...\n",
39+
"'Z' -> \"26\"\n",
40+
"To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, \"11106\" can be mapped into:\n",
41+
"\n",
42+
"\"AAJF\" with the grouping (1 1 10 6)\n",
43+
"\"KJF\" with the grouping (11 10 6)\n",
44+
"Note that the grouping (1 11 06) is invalid because \"06\" cannot be mapped into 'F' since \"6\" is different from \"06\".\n",
45+
"\n",
46+
"Given a string s containing only digits, return the number of ways to decode it.\n",
47+
"\n",
48+
"The test cases are generated so that the answer fits in a 32-bit integer.\n",
49+
"\n"
50+
]
51+
},
52+
{
53+
"cell_type": "code",
54+
"execution_count": 8,
55+
"metadata": {},
56+
"outputs": [
57+
{
58+
"data": {
59+
"text/plain": [
60+
"3"
61+
]
62+
},
63+
"execution_count": 8,
64+
"metadata": {},
65+
"output_type": "execute_result"
66+
}
67+
],
68+
"source": [
69+
"def NumDecodings(s:str)->int:\n",
70+
"\n",
71+
" if s.strip()==\"\":\n",
72+
" return 0\n",
73+
"\n",
74+
" ALL_DIGITS = [ i for i in range(10)]\n",
75+
"\n",
76+
" VALID_2ND_DIGITS = [ i for i in range(7)]\n",
77+
"\n",
78+
" num = 0\n",
79+
"\n",
80+
" pre_i = \"\"\n",
81+
"\n",
82+
"\n",
83+
" for i in s:\n",
84+
" if pre_i == \"1\" and int(i) in ALL_DIGITS:\n",
85+
" num +=2\n",
86+
" if pre_i == \"2\" and int(i) in VALID_2ND_DIGITS:\n",
87+
" num +=2\n",
88+
" if pre_i in [\"\",\"0\"] and int(i) in ALL_DIGITS and int(i) !=0:\n",
89+
" num +=1\n",
90+
" pre_i = i\n",
91+
" \n",
92+
" return num\n",
93+
"\n",
94+
"NumDecodings(\"12\")\n",
95+
"\n",
96+
"\n",
97+
"\n"
98+
]
99+
},
100+
{
101+
"cell_type": "code",
102+
"execution_count": 6,
103+
"metadata": {},
104+
"outputs": [
105+
{
106+
"data": {
107+
"text/plain": [
108+
"[1, 2, 3, 4, 5, 6, 7, 8, 9]"
109+
]
110+
},
111+
"execution_count": 6,
112+
"metadata": {},
113+
"output_type": "execute_result"
114+
}
115+
],
116+
"source": [
117+
"NON_ZERO_DIGITS = [ 1 + i for i in range(9)]\n",
118+
"NON_ZERO_DIGITS"
119+
]
120+
}
121+
],
122+
"metadata": {
123+
"interpreter": {
124+
"hash": "e8d2d80473aa95d50a1c81516f1d5b4a59533016ed6d41e75f06aec8c08c83aa"
125+
},
126+
"kernelspec": {
127+
"display_name": "Python 3.10.1 64-bit",
128+
"language": "python",
129+
"name": "python3"
130+
},
131+
"language_info": {
132+
"codemirror_mode": {
133+
"name": "ipython",
134+
"version": 3
135+
},
136+
"file_extension": ".py",
137+
"mimetype": "text/x-python",
138+
"name": "python",
139+
"nbconvert_exporter": "python",
140+
"pygments_lexer": "ipython3",
141+
"version": "3.10.1"
142+
},
143+
"orig_nbformat": 4
144+
},
145+
"nbformat": 4,
146+
"nbformat_minor": 2
147+
}

dp.ipynb

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"Dynamic programing"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"A message containing letters from A-Z can be encoded into numbers using the following mapping:\n",
15+
"\n",
16+
"'A' -> \"1\"\n",
17+
"'B' -> \"2\"\n",
18+
"...\n",
19+
"'Z' -> \"26\"\n",
20+
"To decode an encoded message, all the digits must be grouped then mapped back into letters using the reverse of the mapping above (there may be multiple ways). For example, \"11106\" can be mapped into:\n",
21+
"\n",
22+
"\"AAJF\" with the grouping (1 1 10 6)\n",
23+
"\"KJF\" with the grouping (11 10 6)\n",
24+
"Note that the grouping (1 11 06) is invalid because \"06\" cannot be mapped into 'F' since \"6\" is different from \"06\".\n",
25+
"\n",
26+
"Given a string s containing only digits, return the number of ways to decode it.\n",
27+
"\n",
28+
"The test cases are generated so that the answer fits in a 32-bit integer.\n"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 1,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"data": {
38+
"text/plain": [
39+
"3"
40+
]
41+
},
42+
"execution_count": 1,
43+
"metadata": {},
44+
"output_type": "execute_result"
45+
}
46+
],
47+
"source": [
48+
"def NumDecodings(s:str)->int:\n",
49+
"\n",
50+
" if s.strip()==\"\":\n",
51+
" return 0\n",
52+
"\n",
53+
" ALL_DIGITS = [ i for i in range(10)]\n",
54+
"\n",
55+
" VALID_2ND_DIGITS = [ i for i in range(7)]\n",
56+
"\n",
57+
" num = 0\n",
58+
"\n",
59+
" pre_i = \"\"\n",
60+
"\n",
61+
"\n",
62+
" for i in s:\n",
63+
" if pre_i == \"1\" and int(i) in ALL_DIGITS:\n",
64+
" num +=2\n",
65+
" if pre_i == \"2\" and int(i) in VALID_2ND_DIGITS:\n",
66+
" num +=2\n",
67+
" if pre_i in [\"\",\"0\"] and int(i) in ALL_DIGITS and int(i) !=0:\n",
68+
" num +=1\n",
69+
" pre_i = i\n",
70+
" \n",
71+
" return num\n",
72+
"\n",
73+
"NumDecodings(\"12\")"
74+
]
75+
}
76+
],
77+
"metadata": {
78+
"interpreter": {
79+
"hash": "e8d2d80473aa95d50a1c81516f1d5b4a59533016ed6d41e75f06aec8c08c83aa"
80+
},
81+
"kernelspec": {
82+
"display_name": "Python 3.10.1 64-bit",
83+
"language": "python",
84+
"name": "python3"
85+
},
86+
"language_info": {
87+
"codemirror_mode": {
88+
"name": "ipython",
89+
"version": 3
90+
},
91+
"file_extension": ".py",
92+
"mimetype": "text/x-python",
93+
"name": "python",
94+
"nbconvert_exporter": "python",
95+
"pygments_lexer": "ipython3",
96+
"version": "3.10.1"
97+
},
98+
"orig_nbformat": 4
99+
},
100+
"nbformat": 4,
101+
"nbformat_minor": 2
102+
}

0 commit comments

Comments
 (0)