LeetCode Notes 014

Combinations

Given two integers $n$ and $k$, return all possible combinations of $k$ numbers out of 1 … $n$.

For example:
If n = 4 and k = 2, a solution is:

1
2
3
4
5
6
7
8
[
[2,4],
[3,4],
[2,3],
[1,2],
[1,3],
[1,4],
]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* @param {number} n
* @param {number} k
* @return {number[][]}
*/

// return an array of k numbers with 1 ~ n

var ans = []
, tmp = [];

function dfs(next, n, num, k) {
if (num === k) {
var res = tmp.map(function(item) {
return item;
});

ans.push(res);
return;
}

for (var i = next; i <= n; i++) {
tmp.push(i);
dfs(i + 1, n, num + 1, k);
tmp.pop();
}
}

var combine = function(n, k) {
ans = [];
// select the 1st number
for (var i = 1; i <= n; i++) {
tmp[0] = i;
dfs(i + 1, n, 1, k);
}

return ans;
};

Compare Version Numbers

Compare two version numbers version1 and version2.
If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.

You may assume that the version strings are non-empty and contain only digits and the . character.
The . character does not represent a decimal point and is used to separate number sequences.
For instance, 2.5 is not “two and a half” or “half way to version three”, it is the fifth second-level revision of the second first-level revision.

Here is an example of version numbers ordering:

0.1 < 1.1 < 1.2 < 13.37

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
* @param {string} version1
* @param {string} version2
* @return {number}
*/
var compareVersion = function(version1, version2) {
var a = version1.split('.');
var b = version2.split('.');

var len1 = a.length;
var len2 = b.length;
for (var i = 0, len = Math.max(len1, len2); i < len; i++) {
var item1 = i < len1 ? +a[i] : 0;
var item2 = i < len2 ? +b[i] : 0;

if (item1 > item2)
return 1;

if (item1 < item2)
return -1;
}
};

Complex Number Multiplication

Given two strings representing two complex numbers.

You need to return a string representing their multiplication. Note $i^2 = -1$ according to the definition.

Example 1:

1
2
3
Input: "1+1i", "1+1i"
Output: "0+2i"
Explanation: (1 + i) * (1 + i) = 1 + i2 + 2 * i = 2i, and you need convert it to the form of 0+2i.

Example 2:

1
2
3
Input: "1+-1i", "1+-1i"
Output: "0+-2i"
Explanation: (1 - i) * (1 - i) = 1 + i2 - 2 * i = -2i, and you need convert it to the form of 0+-2i.

Note

  • The input strings will not have extra blank.
  • The input strings will be given in the form of a+bi, where the integer a and b will both belong to the range of [-100, 100]. And the output should be also in this form.
1
2
3
4
5
6
7
8
9
10
11
12
class Solution(object):
def complexNumberMultiply(self, a, b):
"""
:type a: str
:type b: str
:rtype: str
"""
a = [int(i) for i in a[:-1].split('+')]
b = [int(i) for i in b[:-1].split('+')]
c = a[0] * b[0] - a[1] * b[1]
d = a[0] * b[1] + a[1] * b[0]
return str(c) + '+' + str(d) + 'i'
文章作者: Monad Kai
文章链接: onlookerliu.github.io/2018/03/19/LeetCode-Notes-014/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Code@浮生记
支付宝打赏
微信打赏