Skip to content

Commit 5981842

Browse files
committed
5.17
1 parent 3810709 commit 5981842

File tree

8 files changed

+179
-0
lines changed

8 files changed

+179
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<p>Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target.</p>
2+
3+
<p><b>Example:</b></p>
4+
5+
<pre>
6+
<i><b>nums</b></i> = [1, 2, 3]
7+
<i><b>target</b></i> = 4
8+
9+
The possible combination ways are:
10+
(1, 1, 1, 1)
11+
(1, 1, 2)
12+
(1, 2, 1)
13+
(1, 3)
14+
(2, 1, 1)
15+
(2, 2)
16+
(3, 1)
17+
18+
Note that different sequences are counted as different combinations.
19+
20+
Therefore the output is <i><b>7</b></i>.
21+
</pre>
22+
23+
<p>&nbsp;</p>
24+
25+
<p><b>Follow up:</b><br />
26+
What if negative numbers are allowed in the given array?<br />
27+
How does it change the problem?<br />
28+
What limitation we need to add to the question to allow negative numbers?</p>
29+
30+
<p><b>Credits:</b><br />
31+
Special thanks to <a href="https://leetcode.com/pbrother/">@pbrother</a> for adding this problem and creating all test cases.</p>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def combinationSum4(self, nums, target):
3+
"""
4+
:type nums: List[int]
5+
:type target: int
6+
:rtype: int
7+
"""
8+
dp = [0 for _ in range(target + 1)]
9+
dp[0] = 1
10+
nums.sort()
11+
for i in range(1, target + 1):
12+
for num in nums:
13+
if i >= num:
14+
dp[i] += dp[i - num]
15+
16+
return dp[target]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<p>Given a <b>non-empty</b> string containing an out-of-order English representation of digits <code>0-9</code>, output the digits in ascending order.</p>
2+
3+
<p><b>Note:</b><br />
4+
<ol>
5+
<li>Input contains only lowercase English letters.</li>
6+
<li>Input is guaranteed to be valid and can be transformed to its original digits. That means invalid inputs such as "abc" or "zerone" are not permitted.</li>
7+
<li>Input length is less than 50,000.</li>
8+
</ol>
9+
</p>
10+
11+
<p><b>Example 1:</b><br />
12+
<pre>
13+
Input: "owoztneoer"
14+
15+
Output: "012"
16+
</pre>
17+
</p>
18+
19+
<p><b>Example 2:</b><br />
20+
<pre>
21+
Input: "fviefuro"
22+
23+
Output: "45"
24+
</pre>
25+
</p>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution(object):
2+
def originalDigits(self, s):
3+
"""
4+
:type s: str
5+
:rtype: str
6+
"""
7+
# zero one two three four five six seven eight nine
8+
# z o(先2 w r(先4) u f(先4) x s(先6) t(先3) 最后看e
9+
#所以一个可行的查找的顺序是 two, four, six, one, three, five, seven, eight, nine
10+
order = ["zero", "two", "four", "six", "one", "three", "five", "seven", "eight", "nine"]
11+
find = ["z", "w", "u", "x", "o", "r", "f", "v", "t", "e"]
12+
digit = [0, 2, 4, 6, 1, 3, 5, 7, 8, 9]
13+
14+
record = [0 for _ in range(10)]
15+
dic = collections.Counter(s)
16+
17+
for idx in range(10): #按digit数组的顺序遍历0~9
18+
cnt = dic[find[idx]] #看看可以组成几个digit[idx]
19+
record[digit[idx]] += cnt #记录下来
20+
dic = dic - collections.Counter(order[idx] * cnt) #字典里减去对应的字母
21+
22+
if not dic:
23+
break
24+
25+
ress = ""
26+
for i in range(10): #转换成题目需要的格式
27+
ress += str(i) * record[i]
28+
29+
return ress
30+
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<p>Given a non-negative integer <code>c</code>, your task is to decide whether there&#39;re two integers <code>a</code> and <code>b</code> such that a<sup>2</sup> + b<sup>2</sup> = c.</p>
2+
3+
<p><b>Example 1:</b></p>
4+
5+
<pre>
6+
<b>Input:</b> 5
7+
<b>Output:</b> True
8+
<b>Explanation:</b> 1 * 1 + 2 * 2 = 5
9+
</pre>
10+
11+
<p>&nbsp;</p>
12+
13+
<p><b>Example 2:</b></p>
14+
15+
<pre>
16+
<b>Input:</b> 3
17+
<b>Output:</b> False
18+
</pre>
19+
20+
<p>&nbsp;</p>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution(object):
2+
def judgeSquareSum(self, c):
3+
"""
4+
:type c: int
5+
:rtype: bool
6+
"""
7+
8+
if c == (int(c ** 0.5) ** 2):
9+
return True
10+
11+
lo, hi = 0, int(c ** 0.5)
12+
while(lo <= hi):
13+
s = lo ** 2 + hi ** 2
14+
if s == c:
15+
return True
16+
elif s > c:
17+
hi -= 1
18+
else:
19+
lo += 1
20+
return False
21+

0912.sort-an-array/sort-an-array.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<p>Given an array of integers <code>nums</code>, sort the array in ascending order.</p>
2+
3+
<p>&nbsp;</p>
4+
5+
<ol>
6+
</ol>
7+
8+
<p><strong>Example 1:</strong></p>
9+
10+
<pre>
11+
<strong>Input:</strong>&nbsp;[5,2,3,1]
12+
<strong>Output:</strong> [1,2,3,5]
13+
</pre>
14+
15+
<p><strong>Example 2:</strong></p>
16+
17+
<pre>
18+
<strong>Input: </strong>[5,1,1,2,0,0]
19+
<strong>Output: </strong>[0,0,1,1,2,5]
20+
</pre>
21+
22+
<p>&nbsp;</p>
23+
24+
<p><strong>Note:</strong></p>
25+
26+
<ol>
27+
<li><code>1 &lt;= A.length &lt;= 10000</code></li>
28+
<li><code>-50000 &lt;= A[i] &lt;= 50000</code></li>
29+
</ol>

0912.sort-an-array/sort-an-array.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class Solution(object):
2+
def sortArray(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: List[int]
6+
"""
7+
return sorted(nums)

0 commit comments

Comments
 (0)