Skip to content

Commit e13500e

Browse files
committedMay 16, 2019
Initial commit
0 parents  commit e13500e

21 files changed

+1231
-0
lines changed
 

‎.gitattributes

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

‎.gitignore

+114
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
# Byte-compiled / optimized / DLL files
2+
__pycache__/
3+
*.py[cod]
4+
*$py.class
5+
6+
# C extensions
7+
*.so
8+
9+
# Distribution / packaging
10+
.Python
11+
build/
12+
develop-eggs/
13+
dist/
14+
downloads/
15+
eggs/
16+
.eggs/
17+
lib/
18+
lib64/
19+
parts/
20+
sdist/
21+
var/
22+
wheels/
23+
*.egg-info/
24+
.installed.cfg
25+
*.egg
26+
MANIFEST
27+
28+
# PyInstaller
29+
# Usually these files are written by a python script from a template
30+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
31+
*.manifest
32+
*.spec
33+
34+
# Installer logs
35+
pip-log.txt
36+
pip-delete-this-directory.txt
37+
38+
# Unit test / coverage reports
39+
htmlcov/
40+
.tox/
41+
.nox/
42+
.coverage
43+
.coverage.*
44+
.cache
45+
nosetests.xml
46+
coverage.xml
47+
*.cover
48+
.hypothesis/
49+
.pytest_cache/
50+
51+
# Translations
52+
*.mo
53+
*.pot
54+
55+
# Django stuff:
56+
*.log
57+
local_settings.py
58+
db.sqlite3
59+
60+
# Flask stuff:
61+
instance/
62+
.webassets-cache
63+
64+
# Scrapy stuff:
65+
.scrapy
66+
67+
# Sphinx documentation
68+
docs/_build/
69+
70+
# PyBuilder
71+
target/
72+
73+
# Jupyter Notebook
74+
.ipynb_checkpoints
75+
76+
# IPython
77+
profile_default/
78+
ipython_config.py
79+
80+
# pyenv
81+
.python-version
82+
83+
# celery beat schedule file
84+
celerybeat-schedule
85+
86+
# SageMath parsed files
87+
*.sage.py
88+
89+
# Environments
90+
.env
91+
.venv
92+
env/
93+
venv/
94+
ENV/
95+
env.bak/
96+
venv.bak/
97+
98+
# Spyder project settings
99+
.spyderproject
100+
.spyproject
101+
102+
# Rope project settings
103+
.ropeproject
104+
105+
# mkdocs documentation
106+
/site
107+
108+
# mypy
109+
.mypy_cache/
110+
.dmypy.json
111+
dmypy.json
112+
113+
# Pyre type checker
114+
.pyre/

‎0001.two-sum/two-sum.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
vector<int> twoSum(vector<int>& nums, int target) {
4+
unordered_map<int, int> m;
5+
vector<int> res;
6+
for (int i = 0; i < nums.size(); i++){
7+
int temp = target - nums[i];
8+
if (m.count(temp)){
9+
res.push_back(m[temp]);
10+
res.push_back(i);
11+
break;
12+
}
13+
m[nums[i]] = i;
14+
}
15+
return res;
16+
}
17+
};

‎0001.two-sum/two-sum.md

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<p>Given an array of integers, return <strong>indices</strong> of the two numbers such that they add up to a specific target.</p>
2+
3+
<p>You may assume that each input would have <strong><em>exactly</em></strong> one solution, and you may not use the <em>same</em> element twice.</p>
4+
5+
<p><strong>Example:</strong></p>
6+
7+
<pre>
8+
Given nums = [2, 7, 11, 15], target = 9,
9+
10+
Because nums[<strong>0</strong>] + nums[<strong>1</strong>] = 2 + 7 = 9,
11+
return [<strong>0</strong>, <strong>1</strong>].
12+
</pre>
13+
14+
<p>&nbsp;</p>

‎0001.two-sum/two-sum.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def twoSum(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: List[int]
7+
"""
8+
for i in range(len(nums)):
9+
for j in range(i + 1, len(nums)):
10+
if nums[i] + nums[j] == target:
11+
return [i,j]
12+

‎0036.valid-sudoku/valid-sudoku.md

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<p>Determine if a&nbsp;9x9 Sudoku board&nbsp;is valid.&nbsp;Only the filled cells need to be validated&nbsp;<strong>according to the following rules</strong>:</p>
2+
3+
<ol>
4+
<li>Each row&nbsp;must contain the&nbsp;digits&nbsp;<code>1-9</code> without repetition.</li>
5+
<li>Each column must contain the digits&nbsp;<code>1-9</code>&nbsp;without repetition.</li>
6+
<li>Each of the 9 <code>3x3</code> sub-boxes of the grid must contain the digits&nbsp;<code>1-9</code>&nbsp;without repetition.</li>
7+
</ol>
8+
9+
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px" /><br />
10+
<small>A partially filled sudoku which is valid.</small></p>
11+
12+
<p>The Sudoku board could be partially filled, where empty cells are filled with the character <code>&#39;.&#39;</code>.</p>
13+
14+
<p><strong>Example 1:</strong></p>
15+
16+
<pre>
17+
<strong>Input:</strong>
18+
[
19+
[&quot;5&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],
20+
[&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],
21+
[&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;],
22+
[&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;3&quot;],
23+
[&quot;4&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;],
24+
[&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;],
25+
[&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;8&quot;,&quot;.&quot;],
26+
[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;4&quot;,&quot;1&quot;,&quot;9&quot;,&quot;.&quot;,&quot;.&quot;,&quot;5&quot;],
27+
[&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;9&quot;]
28+
]
29+
<strong>Output:</strong> true
30+
</pre>
31+
32+
<p><strong>Example 2:</strong></p>
33+
34+
<pre>
35+
<strong>Input:</strong>
36+
[
37+
&nbsp; [&quot;8&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],
38+
&nbsp; [&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;,&quot;9&quot;,&quot;5&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;],
39+
&nbsp; [&quot;.&quot;,&quot;9&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;],
40+
&nbsp; [&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;3&quot;],
41+
&nbsp; [&quot;4&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;3&quot;,&quot;.&quot;,&quot;.&quot;,&quot;1&quot;],
42+
&nbsp; [&quot;7&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;6&quot;],
43+
&nbsp; [&quot;.&quot;,&quot;6&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;2&quot;,&quot;8&quot;,&quot;.&quot;],
44+
&nbsp; [&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;4&quot;,&quot;1&quot;,&quot;9&quot;,&quot;.&quot;,&quot;.&quot;,&quot;5&quot;],
45+
&nbsp; [&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;.&quot;,&quot;8&quot;,&quot;.&quot;,&quot;.&quot;,&quot;7&quot;,&quot;9&quot;]
46+
]
47+
<strong>Output:</strong> false
48+
<strong>Explanation:</strong> Same as Example 1, except with the <strong>5</strong> in the top left corner being
49+
modified to <strong>8</strong>. Since there are two 8&#39;s in the top left 3x3 sub-box, it is invalid.
50+
</pre>
51+
52+
<p><strong>Note:</strong></p>
53+
54+
<ul>
55+
<li>A Sudoku board (partially filled) could be valid but is not necessarily solvable.</li>
56+
<li>Only the filled cells need to be validated according to the mentioned&nbsp;rules.</li>
57+
<li>The given board&nbsp;contain only digits <code>1-9</code> and the character <code>&#39;.&#39;</code>.</li>
58+
<li>The given board size is always <code>9x9</code>.</li>
59+
</ul>

‎0036.valid-sudoku/valid-sudoku.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def isValidSudoku(self, board):
3+
"""
4+
:type board: List[List[str]]
5+
:rtype: bool
6+
"""
7+
from collections import defaultdict
8+
row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set)
9+
10+
for i in range(9):
11+
for j in range(9):
12+
if board[i][j].isdigit(): #Åųýµô¿ÕµÄÇé¿ö
13+
if board[i][j] in row[i] or board[i][j] in column[j] or (board[i][j]) in squre[(i // 3, j // 3)]:
14+
# print board[i][j], row[i], column[j], squre[(i // 3, j // 3)]
15+
return False
16+
else:
17+
row[i].add(board[i][j])
18+
column[j].add(board[i][j])
19+
squre[(i // 3, j // 3)].add(board[i][j])
20+
# print row[1]
21+
return True

‎0037.sudoku-solver/sudoku-solver.md

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<p>Write a program to solve a Sudoku puzzle by filling the empty cells.</p>
2+
3+
<p>A&nbsp;sudoku solution must satisfy <strong>all of&nbsp;the following rules</strong>:</p>
4+
5+
<ol>
6+
<li>Each of the digits&nbsp;<code>1-9</code> must occur exactly&nbsp;once in each row.</li>
7+
<li>Each of the digits&nbsp;<code>1-9</code>&nbsp;must occur&nbsp;exactly once in each column.</li>
8+
<li>Each of the the digits&nbsp;<code>1-9</code> must occur exactly once in each of the 9 <code>3x3</code> sub-boxes of the grid.</li>
9+
</ol>
10+
11+
<p>Empty cells are indicated by the character <code>&#39;.&#39;</code>.</p>
12+
13+
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/ff/Sudoku-by-L2G-20050714.svg/250px-Sudoku-by-L2G-20050714.svg.png" style="height:250px; width:250px" /><br />
14+
<small>A sudoku puzzle...</small></p>
15+
16+
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/3/31/Sudoku-by-L2G-20050714_solution.svg/250px-Sudoku-by-L2G-20050714_solution.svg.png" style="height:250px; width:250px" /><br />
17+
<small>...and its solution numbers marked in red.</small></p>
18+
19+
<p><strong>Note:</strong></p>
20+
21+
<ul>
22+
<li>The given board&nbsp;contain only digits <code>1-9</code> and the character <code>&#39;.&#39;</code>.</li>
23+
<li>You may assume that the given Sudoku puzzle will have a single unique solution.</li>
24+
<li>The given board size is always <code>9x9</code>.</li>
25+
</ul>

‎0037.sudoku-solver/sudoku-solver.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
class Solution(object):
2+
def solveSudoku(self, board):
3+
"""
4+
:type board: List[List[str]]
5+
:rtype: None Do not return anything, modify board in-place instead.
6+
"""
7+
from collections import defaultdict
8+
row, column, squre = defaultdict(set), defaultdict(set), defaultdict(set)
9+
fill_list = []
10+
for i in range(9):
11+
for j in range(9):
12+
if board[i][j].isdigit(): #Åųýµô¿ÕµÄÇé¿ö
13+
row[i].add(board[i][j].encode("utf-8"))
14+
column[j].add(board[i][j].encode("utf-8"))
15+
squre[(i // 3, j // 3)].add(board[i][j].encode("utf-8"))
16+
else:
17+
fill_list.append([i, j])
18+
19+
self.result = []
20+
def backtrack(idx):
21+
if idx == len(fill_list):
22+
for row1 in board:
23+
self.result.append(row1[:])
24+
return
25+
if not self.result:
26+
i, j = fill_list[idx][0], fill_list[idx][1]
27+
for digit in range(1, 10):
28+
if str(digit) in row[i] or str(digit) in column[j] or str(digit) in squre[(i // 3, j // 3)]:
29+
continue
30+
31+
board[i][j] = str(digit)
32+
row[i].add(board[i][j])
33+
column[j].add(board[i][j])
34+
squre[(i // 3, j // 3)].add(board[i][j])
35+
36+
backtrack(idx + 1)
37+
38+
row[i].remove(board[i][j])
39+
column[j].remove(board[i][j])
40+
squre[(i // 3, j // 3)].remove(board[i][j])
41+
42+
backtrack(0)
43+
for i in range(9):
44+
for j in range(9):
45+
board[i][j] = self.result[i][j]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<p>Given an unsorted integer array, find the smallest missing&nbsp;positive integer.</p>
2+
3+
<p><strong>Example 1:</strong></p>
4+
5+
<pre>
6+
Input: [1,2,0]
7+
Output: 3
8+
</pre>
9+
10+
<p><strong>Example 2:</strong></p>
11+
12+
<pre>
13+
Input: [3,4,-1,1]
14+
Output: 2
15+
</pre>
16+
17+
<p><strong>Example 3:</strong></p>
18+
19+
<pre>
20+
Input: [7,8,9,11,12]
21+
Output: 1
22+
</pre>
23+
24+
<p><strong>Note:</strong></p>
25+
26+
<p>Your algorithm should run in <em>O</em>(<em>n</em>) time and uses constant extra space.</p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution(object):
2+
def firstMissingPositive(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: int
6+
"""
7+
# 对于处于1~n之间的数,把它们放到nums[i - 1]的位置上
8+
9+
for i in range(len(nums)):
10+
while 1 <= nums[i] <= len(nums) and nums[i] != nums[nums[i] - 1]:
11+
# nums[i], nums[nums[i] - 1] = nums[nums[i] - 1], nums[i]
12+
nums[nums[i] - 1], nums[i] = nums[i], nums[nums[i] - 1]
13+
14+
for i, x in enumerate(nums):
15+
if x != i + 1:
16+
return i + 1
17+
18+
return len(nums) + 1

0 commit comments

Comments
 (0)
Please sign in to comment.