Skip to content

Commit 5aecd2f

Browse files
add: 算法
1 parent a096892 commit 5aecd2f

12 files changed

+346
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* @lc app=leetcode.cn id=10 lang=javascript
3+
*
4+
* [10] 正则表达式匹配
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @param {string} p
11+
* @return {boolean}
12+
*/
13+
var isMatch = function(s, p) {
14+
let m = s.length, n = p.length;
15+
// 备忘录
16+
let memo = new Array(m + 1);
17+
for (let i = 0; i < memo.length; i++) {
18+
memo[i] = new Array(n + 1).fill(-1);
19+
}
20+
21+
return dp(0, 0);
22+
23+
/* 计算 p[j..] 是否匹配 s[i..] */
24+
function dp(i, j) {
25+
// 查备忘录,防止重复计算
26+
if (memo[i][j] !== -1) {
27+
return memo[i][j];
28+
}
29+
let res = false;
30+
// base case
31+
if (j === n) {
32+
res = i === m;
33+
} else {
34+
let firstMatch = i < m && (p[j] === s[i] || p[j] === '.');
35+
if (j + 1 < n && p[j + 1] === '*') {
36+
res = dp(i, j + 2) || (firstMatch && dp(i + 1, j));
37+
} else {
38+
res = firstMatch && dp(i + 1, j + 1);
39+
}
40+
}
41+
// 将当前结果记入备忘录
42+
memo[i][j] = res;
43+
return res;
44+
}
45+
};
46+
// @lc code=end
47+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode.cn id=11 lang=javascript
3+
*
4+
* [11] 盛最多水的容器
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} height
10+
* @return {number}
11+
*/
12+
var maxArea = function(height) {
13+
// 这个就是接雨水的套路
14+
let left = 0, right = height.length - 1
15+
let res = 0
16+
while(left < right) {
17+
// 和接雨水差不多,接雨水是需要对比左边heigh和右边height
18+
res = Math.max(res, Math.min(height[left], height[right]) * (right - left))
19+
if(height[left] < height[right]) {
20+
left++
21+
}else {
22+
right--
23+
}
24+
}
25+
return res
26+
};
27+
// @lc code=end
28+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @lc app=leetcode.cn id=12 lang=javascript
3+
*
4+
* [12] 整数转罗马数字
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number} num
10+
* @return {string}
11+
*/
12+
var intToRoman = function(num) {
13+
var Q = ["", "M", "MM", "MMM"];
14+
var B = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"];
15+
var S = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"];
16+
var G = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"];
17+
return Q[Math.floor(num/1000)] + B[Math.floor((num%1000)/100)] + S[Math.floor((num%100)/10)] + G[num%10];
18+
};
19+
// @lc code=end
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* @lc app=leetcode.cn id=13 lang=javascript
3+
*
4+
* [13] 罗马数字转整数
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @return {number}
11+
*/
12+
var romanToInt = function(s) {
13+
const map = {
14+
I: 1,
15+
IV: 4,
16+
V: 5,
17+
IX: 9,
18+
X: 10,
19+
XL: 40,
20+
L: 50,
21+
XC: 90,
22+
C: 100,
23+
CD: 400,
24+
D: 500,
25+
CM: 900,
26+
M: 1000
27+
}
28+
let ans = 0
29+
for (let i = 0; i < s.length;) {
30+
if((i + 1 < s.length) && map[s.substring(i, i+2)]) {
31+
ans += map[s.substring(i, i+2)]
32+
i += 2
33+
}else {
34+
ans += map[s.substring(i, i+1)]
35+
i++
36+
}
37+
}
38+
return ans
39+
};
40+
// @lc code=end
41+

labuladuo/2.两数相加.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* @lc app=leetcode.cn id=2 lang=javascript
3+
*
4+
* [2] 两数相加
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* Definition for singly-linked list.
10+
* function ListNode(val, next) {
11+
* this.val = (val===undefined ? 0 : val)
12+
* this.next = (next===undefined ? null : next)
13+
* }
14+
*/
15+
/**
16+
* @param {ListNode} l1
17+
* @param {ListNode} l2
18+
* @return {ListNode}
19+
*/
20+
var addTwoNumbers = function(l1, l2) {
21+
let p1 = l1, p2 = l2;
22+
let dummy = new ListNode(-1)
23+
let p = dummy
24+
let carry = 0
25+
while(p1 != null || p2 != null || carry > 0) {
26+
// 处理推进和处理
27+
let val = carry
28+
if(p1 != null) {
29+
val += p1.val
30+
p1 = p1.next
31+
}
32+
if(p2 != null) {
33+
val += p2.val
34+
p2 = p2.next
35+
}
36+
// 处理进位情况
37+
carry = Math.floor(val / 10)
38+
val = val % 10
39+
// 构建新的节点
40+
p.next = new ListNode(val)
41+
p = p.next
42+
}
43+
return dummy.next
44+
};
45+
// @lc code=end
46+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
* @lc app=leetcode.cn id=4 lang=javascript
3+
*
4+
* [4] 寻找两个正序数组的中位数
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} nums1
10+
* @param {number[]} nums2
11+
* @return {number}
12+
*/
13+
var findMedianSortedArrays = function(nums1, nums2) {
14+
15+
};
16+
// @lc code=end
17+

labuladuo/46.全排列.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* @lc app=leetcode.cn id=46 lang=javascript
3+
*
4+
* [46] 全排列
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number[]} nums
10+
* @return {number[][]}
11+
*/
12+
var permute = function(nums) {
13+
14+
};
15+
// @lc code=end
16+

labuladuo/6.n-字形变换.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=6 lang=javascript
3+
*
4+
* [6] N 字形变换
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @param {number} numRows
11+
* @return {string}
12+
*/
13+
var convert = function(s, numRows) {
14+
if(numRows == 1) return s
15+
16+
const len = Math.min(s.length, numRows)
17+
const rows = new Array(len).fill('')
18+
let loc = 0, down = false
19+
// 记得那个动画就好,上下轮动,其他的都是为这个做准备
20+
for (const c of s) {
21+
rows[loc] += c
22+
if(loc == 0 || loc == numRows - 1) {
23+
down = !down
24+
}
25+
loc += down ? 1 : -1
26+
}
27+
return rows.join('')
28+
};
29+
// @lc code=end
30+

labuladuo/7.整数反转.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* @lc app=leetcode.cn id=7 lang=javascript
3+
*
4+
* [7] 整数反转
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {number} x
10+
* @return {number}
11+
*/
12+
var reverse = function(x) {
13+
let isPos = true
14+
let y = x
15+
// 处理负值
16+
if(x < 0) {
17+
y = Math.abs(x)
18+
isPos = false
19+
}
20+
// 核心计算,我不会位运算
21+
let numRes = Number(y.toString().split('').reverse().join(''))
22+
// 数字大小限制
23+
let res = isPos === false ? -1 * numRes : numRes
24+
if(res > Math.pow(2, 31) - 1 || res < Math.pow(-2, 31)) return 0;
25+
return res
26+
};
27+
// @lc code=end
28+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* @lc app=leetcode.cn id=8 lang=javascript
3+
*
4+
* [8] 字符串转换整数 (atoi)
5+
*/
6+
7+
// @lc code=start
8+
/**
9+
* @param {string} s
10+
* @return {number}
11+
*/
12+
var myAtoi = function(str) {
13+
const number = parseInt(str, 10);
14+
15+
if(isNaN(number)) {
16+
return 0;
17+
} else if (number < -(2**31) || number > 2**31-1) {
18+
return number < -(2**31) ? -(2**31) : 2**31-1;
19+
} else {
20+
return number;
21+
}
22+
};
23+
// @lc code=end
24+
25+
// 另外的思路
26+
/*
27+
let res = str.trim().match(/^(\-|\+)?\d+/g)
28+
return res ? Math.max(Math.min(Number(res[0]),2**31-1),-(2**31)) : 0
29+
*/
30+

0 commit comments

Comments
 (0)