Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4a758f0

Browse files
committed
docs: add No13、53、191、384题解
1 parent 1fae0b0 commit 4a758f0

File tree

5 files changed

+241
-1
lines changed

5 files changed

+241
-1
lines changed

‎leetcode刷题/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
- [No.1 两数之和](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No1_two-sum.md)
44
- [No.7 整数反转](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No7_reverse.md)
55
- [No.8 字符串转换整数 (atoi)](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No8_my-atoi.md)
6+
- [No.13 罗马数字转整数](note/No13_roman-to-int.md)
67
- [No.14 最长公共前缀](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No14_longest-common-prefix.md)
78
- [No.19 删除链表倒数第 n 个节点](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No19_remove-nth-from-end.md)
89
- [No.20 有效的括号](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No20_is-valid.md)
@@ -11,6 +12,7 @@
1112
- [No.36 有效的数独](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No36_isvalid-sudoku.md)
1213
- [No.38 报数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No38_count-and-say.md)
1314
- [No.48 旋转图像](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No48_rotate.md)
15+
- [No.53 最大子序和](note/No53_max-sub-array.md)
1416
- [No.66 加一](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No66_plus-one.md)
1517
- [No.70 爬楼梯](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No70_climb-stairs.md)
1618
- [No.88 合并两个有序数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No88_merge.md)
@@ -25,6 +27,7 @@
2527
- [No.160 相交链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No160_get-intersection-node.md)
2628
- [No.169 多数元素](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No169_majority-element.md)
2729
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
30+
- [No.191 位1的个数](note/No191_hamming-weight.md)
2831
- [No.198 打家劫舍](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No198_rob.md)
2932
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
3033
- [No.206 反转链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No206_reverse-list.md)
@@ -34,6 +37,7 @@
3437
- [No.283 移动零](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No283_move-zeroes.md)
3538
- [No.326 3的幂](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No326_is-power-of-three.md)
3639
- [No.350 两个数组的交集 II](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No350_intersect.md)
40+
- [No.384 打乱数组](note/No384_shuffle.md)
3741
- [No.387 字符串中的第一个唯一字符](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No387_first-uniq-char.md)
3842
- [No.412 Fizz Buzz](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No412_fizz-buzz.md)
3943
- [No.448 找到所有数组中消失的数字](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No448_find-disappeared-numbers.md)
@@ -89,17 +93,20 @@
8993

9094
#### 数学初级
9195

96+
- [No.13 罗马数字转整数](note/No13_roman-to-int.md)
9297
- [No.326 3的幂](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No326_is-power-of-three.md)
9398
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
9499
- [No.492 构造矩形](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No492_construct-rectangle.md)
95100

96101

97102
### 动态规划
98103

104+
- [No.53 最大子序和](note/No53_max-sub-array.md)
99105
- [No.70 爬楼梯](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No70_climb-stairs.md)
100106
- [No.121 买卖股票的最佳时机](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No121_max-profit.md)
101107
- [No.198 打家劫舍](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No198_rob.md)
102108

109+
103110
### 排序和搜索
104111

105112
- [No.88 合并两个有序数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No88_merge.md)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# No.13 罗马数字转整数
2+
3+
难度:`easy`
4+
5+
罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。
6+
7+
字符 数值
8+
I 1
9+
V 5
10+
X 10
11+
L 50
12+
C 100
13+
D 500
14+
M 1000
15+
例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。
16+
17+
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX。这个特殊的规则只适用于以下六种情况:
18+
```
19+
I 可以放在 V (5) 和 X (10) 的左边,来表示 4 和 9。
20+
X 可以放在 L (50) 和 C (100) 的左边,来表示 40 和 90。
21+
C 可以放在 D (500) 和 M (1000) 的左边,来表示 400 和 900。
22+
给定一个罗马数字,将其转换成整数。输入确保在 1 到 3999 的范围内。
23+
```
24+
25+
## 示例
26+
27+
示例 1:
28+
```
29+
输入: "III"
30+
输出: 3
31+
```
32+
示例 2:
33+
```
34+
输入: "IV"
35+
输出: 4
36+
```
37+
示例 3:
38+
```
39+
输入: "IX"
40+
输出: 9
41+
```
42+
示例 4:
43+
```
44+
输入: "LVIII"
45+
输出: 58
46+
解释: L = 50, V= 5, III = 3.
47+
```
48+
示例 5:
49+
```
50+
输入: "MCMXCIV"
51+
输出: 1994
52+
解释: M = 1000, CM = 900, XC = 90, IV = 4.
53+
```
54+
55+
## 解题思路
56+
57+
先建立一个HashMap来映射符号和值,然后对字符串从左到右来,如果当前字符代表的值不小于其右边,就加上该值;否则就减去该值。以此类推到最左边的数,最终得到的结果即是答案。
58+
59+
代码如下:
60+
61+
```javascript
62+
/**
63+
* @param {string} s
64+
* @return {number}
65+
*/
66+
var romanToInt = function(s) {
67+
let map = {
68+
'I': 1,
69+
'V': 5,
70+
'X': 10,
71+
'L': 50,
72+
'C': 100,
73+
'D': 500,
74+
'M': 1000,
75+
76+
}
77+
let num = 0;
78+
let n = s.length;
79+
for (let i = 0; i < n; i++) {
80+
81+
if (map[s[i]]<map[s[i+1]] && i < n-1) {
82+
num -= map[s[i]];
83+
} else {
84+
num += map[s[i]];
85+
}
86+
console.log(num);
87+
}
88+
return num;
89+
};
90+
```
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# No.191 位1的个数
2+
3+
编写一个函数,输入是一个无符号整数,返回其二进制表达式中数字位数为 ‘1’ 的个数(也被称为汉明重量)。
4+
5+
6+
## 示例
7+
8+
示例 1:
9+
10+
```
11+
输入:00000000000000000000000000001011
12+
输出:3
13+
解释:输入的二进制串 00000000000000000000000000001011 中,共有三位为 '1'。
14+
```
15+
示例 2:
16+
```
17+
输入:00000000000000000000000010000000
18+
输出:1
19+
解释:输入的二进制串 00000000000000000000000010000000 中,共有一位为 '1'。
20+
```
21+
示例 3:
22+
```
23+
输入:11111111111111111111111111111101
24+
输出:31
25+
解释:输入的二进制串 11111111111111111111111111111101 中,共有 31 位为 '1'。
26+
```
27+
28+
## 解题思路
29+
30+
代码如下:
31+
32+
```javascript
33+
/**
34+
* @param {number} n - a positive integer
35+
* @return {number}
36+
*/
37+
var hammingWeight = function(n) {
38+
let count = 0;
39+
let mask = 1;
40+
for(let i = 0;i < 32;i++){
41+
if((n & mask) != 0){
42+
count++;
43+
}
44+
mask <<= 1;
45+
}
46+
return count;
47+
};
48+
```

‎leetcode刷题/note/No384_shuffle.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# No.384 打乱数组
2+
3+
难度:`middle`
4+
5+
打乱一个没有重复元素的数组。
6+
7+
## 示例
8+
9+
示例:
10+
```
11+
// 以数字集合 1, 2 和 3 初始化数组。
12+
int[] nums = {1,2,3};
13+
Solution solution = new Solution(nums);
14+
15+
// 打乱数组 [1,2,3] 并返回结果。任何 [1,2,3]的排列返回的概率应该相同。
16+
solution.shuffle();
17+
18+
// 重设数组到它的初始状态[1,2,3]。
19+
solution.reset();
20+
21+
// 随机返回数组[1,2,3]打乱后的结果。
22+
solution.shuffle();
23+
```
24+
25+
## 解题思路
26+
27+
主要是乱序到方法,可以使用洗牌算法,一边遍历数组,遍历的同时随机生成一个下标,将当前遍历的下标与随机生成的下标元素进行交换。
28+
29+
代码如下:
30+
31+
```javascript
32+
/**
33+
* @param {number[]} nums
34+
*/
35+
var Solution = function(nums) {
36+
this.nums = nums;
37+
this.original = [...nums];
38+
};
39+
40+
/**
41+
* Resets the array to its original configuration and return it.
42+
* @return {number[]}
43+
*/
44+
Solution.prototype.reset = function() {
45+
return this.original;
46+
};
47+
48+
/**
49+
* Returns a random shuffling of the array.
50+
* @return {number[]}
51+
*/
52+
Solution.prototype.shuffle = function() {
53+
// 洗牌算法,一边遍历数组,遍历的同时随机生成一个下标,将当前遍历的下标与随机生成的下标元素进行交换
54+
for (let i = 0, n = this.nums.length; i < n; i++) {
55+
let randomIdx = Math.floor(Math.random()*n);
56+
let temp = this.nums[i];
57+
this.nums[i] = this.nums[randomIdx];
58+
this.nums[randomIdx] = temp;
59+
}
60+
return this.nums;
61+
};
62+
63+
/**
64+
* Your Solution object will be instantiated and called as such:
65+
* var obj = new Solution(nums)
66+
* var param_1 = obj.reset()
67+
* var param_2 = obj.shuffle()
68+
*/
69+
```
70+

‎leetcode刷题/note/No53_max-sub-array.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,29 @@ var maxSubArray = function(nums) {
4949

5050
**思路二:**
5151

52-
动态规划,一遍遍历,每次遍历计算出。类似寻找最大最小值的题目,初始值一定要定义成理论上的最小最大值
52+
动态规划,一遍遍历,每次遍历计算出。类似寻找最大最小值的题目,初始值一定要定义成理论上的最小最大值。
53+
54+
遍历到当前数时,看是否有增益效果,并计算出当前到最大值。
55+
56+
代码如下:
57+
58+
```javascript
59+
/**
60+
* @param {number[]} nums
61+
* @return {number}
62+
*/
63+
var maxSubArray = function(nums) {
64+
let ans = nums[0];
65+
let sum = 0;
66+
for(let num of nums) {
67+
if(sum > 0) {
68+
sum += num;
69+
} else {
70+
sum = num;
71+
}
72+
ans = Math.max(ans, sum);
73+
}
74+
return ans;
75+
76+
};
77+
```

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /