Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
leetcode
提交
b159f16f
L
leetcode
项目概览
爱吃血肠
/
leetcode
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
leetcode
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
b159f16f
编写于
4月 24, 2019
作者:
L
luzhipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat: 修改#209 #279, 增加todo
上级
3f8598ef
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
217 addition
and
15 deletion
+217
-15
README.md
README.md
+4
-4
assets/drawio/209.minimum-size-subarray-sum.drawio
assets/drawio/209.minimum-size-subarray-sum.drawio
+1
-0
assets/problems/209.minimum-size-subarray-sum.png
assets/problems/209.minimum-size-subarray-sum.png
+0
-0
problems/209.minimum-size-subarray-sum.md
problems/209.minimum-size-subarray-sum.md
+3
-1
problems/279.perfect-squares.md
problems/279.perfect-squares.md
+69
-10
todo/slideWindow/239.sliding-window-maximum.js
todo/slideWindow/239.sliding-window-maximum.js
+62
-0
todo/str/214.shortest-palindrome.js
todo/str/214.shortest-palindrome.js
+38
-0
todo/str/5.longest-palindromic-substring.js
todo/str/5.longest-palindromic-substring.js
+40
-0
未找到文件。
README.md
浏览文件 @
b159f16f
...
...
@@ -118,9 +118,9 @@ leetcode 题解,记录自己的 leetcode 解题之路。
-
[
199.binary-tree-right-side-view
](
./problems/199.binary-tree-right-side-view.md
)
-
[
201.bitwise-and-of-numbers-range
](
./problems/201.bitwise-and-of-numbers-range.md
)
-
🆕
[
208.implement-trie-prefix-tree
](
./problems/208.implement-trie-prefix-tree.md
)
-
[
209.minimum-size-subarray-sum
](
./problems/209.minimum-size-subarray-sum.md
)
-
🖊
[
209.minimum-size-subarray-sum
](
./problems/209.minimum-size-subarray-sum.md
)
-
[
240.search-a-2-d-matrix-ii
](
./problems/240.search-a-2-d-matrix-ii.md
)
-
[
279.perfect-squares
](
./problems/279.perfect-squares.md
)
-
🖊
[
279.perfect-squares
](
./problems/279.perfect-squares.md
)
-
[
322.coin-change
](
./problems/322.coin-change.md
)
-
[
328.odd-even-linked-list
](
./problems/328.odd-even-linked-list.md
)
-
[
416.partition-equal-subset-sum
](
./problems/416.partition-equal-subset-sum.md
)
...
...
@@ -169,9 +169,9 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki集合”,然后
-
[
365.water-and-jug-problem
](
./todo/365.water-and-jug-problem.js
)
-
anki 卡片 完善
-
[
anki 卡片 完善
](
./assets/anki/
)
-
字符串类问题汇总
-
[
字符串类问题汇总
](
./todo/str/
)
## 交流群
...
...
assets/drawio/209.minimum-size-subarray-sum.drawio
0 → 100644
浏览文件 @
b159f16f
<mxfile modified="2019-04-24T03:55:11.597Z" host="www.draw.io" agent="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36" etag="RuhMS0eC7MZ-eP7ukQ4K" version="10.6.3" type="device"><diagram id="2aZ_QtbOp8G18H4-aLN5" name="第 1 页">7ZpBb5swGIZ/DcdF2AYDxzZttsMqTcqh2mlywAFUwBScJemvnymGBOyknZYQouWU8NkY87yfzWuMgabp5mtB8uiJBTQxoBlsDPRgQAiQ54qfKrKtIw4GdSAs4kBW2gXm8RuVQVNGV3FAy05FzljC47wb9FmWUZ93YqQo2LpbbcmS7lVzElIlMPdJokaf44BHddSFzi7+jcZh1FwZYK8uSUlTWd5JGZGArfdC6NFA04IxXv9LN1OaVPAaLvV5swOlbccKmvHPnOA7L7Nis9g+/bRfnk3ivr3O11/kbfwmyUresOws3zYECrbKAlo1Agx0v45iTuc58avStdBcxCKeJrK45AV7oVOWsOL9bOQ5D6bjiBJ5IVpwujl4B6DlIhKKspTyYiuqyBNcSVKmkicP1ztdAJaxaE8TR8aITIWwbXhHS/yRwP4CHlDgHaNnfkzvBIy8LiNgqpBsDSP7XIygwghdnBGwRgYJKZDA5SGNLZOsEY42OLZMshVI1uUhjS2T8AjnJDS2THIVSI4CSdwuP/b8z1gmat4v4yTphUgSh5k49AUeKuL3FbxY2K47WZDGQVBdRou+K86SZVwaR2jL4z0HMpthE+LTqGSbPZWArahkaVSC51LJU1Ryj6TyPxm4gJRR28oJWPbcHHBVlO0qYBg7Zyow8bXABMjp0Gzp7ptjGw1JU3XHDeDrw4kcTXKag+JUjbQ6H4+UJuzNmpajSU7dtHk+mqpP8q6GZjOMJU0bq7k58DpYNVRXk5sIdHMTY01uDvsYUt/JJDQLq87cqWZ+HBaso8oHfsw076czeBr1cFe89glzKTsGVNfcaqeuMf5v7UBv5CHzwl4aqGb6NvA+KZ6lWa4OKh5Uzftt5B0Sr3GzjXjehUceVNcKN/EO2T/kTrry2ZqNkGHlU9cmrXzwJt/Rl6FY89pjWPHUzQdFsnbrskPO/MCzL1zbsqtKCVnQ5AcrYx4zrX7fexUWjHOWagTmLNflgehfXnU13YTVLvQkLX1CJwHNC+oTToNJzkpR89f7hnAvt0RHl65PfV+TB0EsGpB9Ktmq0ucUOWD13vViB040azhPkwaWfa40ULdXDIgTLqkY1Q57Awy/rqpNa4ECEeyaC3c/hMPqN42z5nTRnbqFuuQ2H3TfKKNeLthnm8zF4e5rg/eyvW820OMf</diagram></mxfile>
\ No newline at end of file
assets/problems/209.minimum-size-subarray-sum.png
0 → 100644
浏览文件 @
b159f16f
55.4 KB
problems/209.minimum-size-subarray-sum.md
浏览文件 @
b159f16f
...
...
@@ -21,11 +21,13 @@ If you have figured out the O(n) solution, try coding another solution of which
用滑动窗口来记录序列, 每当滑动窗口中的 sum 超过 s, 就去更新最小值,并根据先进先出的原则更新滑动窗口,直至 sum 刚好小于 s
![
209.minimum-size-subarray-sum
](
../assets/problems/209.minimum-size-subarray-sum.png
)
> 这道题目和 leetcode 3 号题目有点像,都可以用滑动窗口的思路来解决
## 关键点
-
滑动窗口简化操作
-
滑动窗口简化操作
(滑窗口适合用于求解这种要求
`连续`
的题目)
## 代码
...
...
problems/279.perfect-squares.md
浏览文件 @
b159f16f
## 题目地址
https://leetcode.com/problems/perfect-squares/description/
## 题目描述
```
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n.
Example 1:
Input: n = 12
Output: 3
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
...
...
@@ -23,19 +23,66 @@ Explanation: 13 = 4 + 9.
## 思路
直接递归处理即可,但是这种暴力的解法很容易超时。如果你把递归的过程化成一棵树的话(其实就是递归树),
可以看出中间有很多重复的计算。
可以看出中间有很多重复的计算。
如果能将重复的计算缓存下来,说不定能够解决时间复杂度太高的问题。
> 递归对内存的要求也很高, 如果数字非常大,也会面临爆栈的风险,将递归转化为循环可以解决。
如果使用DP,其实本质上和递归 + 缓存 差不多。
递归 + 缓存的方式代码如下:
```
js
const
mapper
=
{};
function
d
(
n
,
level
)
{
if
(
n
===
0
)
return
level
;
let
i
=
1
;
const
arr
=
[];
while
(
n
-
i
*
i
>=
0
)
{
const
hit
=
mapper
[
n
-
i
*
i
];
if
(
hit
)
{
arr
.
push
(
hit
+
level
);
}
else
{
const
depth
=
d
(
n
-
i
*
i
,
level
+
1
)
-
level
;
mapper
[
n
-
i
*
i
]
=
depth
;
arr
.
push
(
depth
+
level
);
}
i
++
;
}
return
Math
.
min
(...
arr
);
}
/**
* @param {number} n
* @return {number}
*/
var
numSquares
=
function
(
n
)
{
return
d
(
n
,
0
);
};
```
如果使用 DP,其实本质上和递归 + 缓存 差不多。
DP 的代码见代码区。
## 关键点解析
-
如果用递归 + 缓存, 缓存的设计很重要
我的做法是key就是n,value是以n为起点,到达底端的深度。
下次取出缓存的时候用当前的level + 存的深度 就是我们想要的level.
我的做法是 key 就是 n,value 是以 n 为起点,到达底端的深度。
下次取出缓存的时候用当前的 level + 存的深度 就是我们想要的 level.
-
使用递归的核心点还是选和不选的问题
```
js
for
(
let
i
=
1
;
i
<=
n
;
i
++
)
{
for
(
let
j
=
1
;
j
*
j
<=
i
;
j
++
)
{
// 选(dp[i]) 还是 不选(dp[i - j * j])
dp
[
i
]
=
Math
.
min
(
dp
[
i
],
dp
[
i
-
j
*
j
]
+
1
);
}
}
```
## 代码
...
...
@@ -74,13 +121,12 @@ Explanation: 13 = 4 + 9.
const
mapper
=
{};
function
d
(
n
,
level
)
{
if
(
n
===
0
)
return
level
;
if
(
n
===
0
)
return
level
;
let
i
=
1
;
const
arr
=
[];
while
(
n
-
i
*
i
>=
0
)
{
while
(
n
-
i
*
i
>=
0
)
{
const
hit
=
mapper
[
n
-
i
*
i
];
if
(
hit
)
{
arr
.
push
(
hit
+
level
);
...
...
@@ -99,6 +145,19 @@ function d(n, level) {
* @return {number}
*/
var
numSquares
=
function
(
n
)
{
return
d
(
n
,
0
);
if
(
n
<=
0
)
{
return
0
;
}
const
dp
=
Array
(
n
+
1
).
fill
(
Number
.
MAX_VALUE
);
dp
[
0
]
=
0
;
for
(
let
i
=
1
;
i
<=
n
;
i
++
)
{
for
(
let
j
=
1
;
j
*
j
<=
i
;
j
++
)
{
// 选(dp[i]) 还是 不选(dp[i - j * j])
dp
[
i
]
=
Math
.
min
(
dp
[
i
],
dp
[
i
-
j
*
j
]
+
1
);
}
}
return
dp
[
n
];
};
```
todo/slideWindow/239.sliding-window-maximum.js
0 → 100644
浏览文件 @
b159f16f
/*
* @lc app=leetcode id=239 lang=javascript
*
* [239] Sliding Window Maximum
*
* https://leetcode.com/problems/sliding-window-maximum/description/
*
* algorithms
* Hard (37.22%)
* Total Accepted: 150.8K
* Total Submissions: 399.5K
* Testcase Example: '[1,3,-1,-3,5,3,6,7]\n3'
*
* Given an array nums, there is a sliding window of size k which is moving
* from the very left of the array to the very right. You can only see the k
* numbers in the window. Each time the sliding window moves right by one
* position. Return the max sliding window.
*
* Example:
*
*
* Input: nums = [1,3,-1,-3,5,3,6,7], and k = 3
* Output: [3,3,5,5,6,7]
* Explanation:
*
* Window position Max
* --------------- -----
* [1 3 -1] -3 5 3 6 7 3
* 1 [3 -1 -3] 5 3 6 7 3
* 1 3 [-1 -3 5] 3 6 7 5
* 1 3 -1 [-3 5 3] 6 7 5
* 1 3 -1 -3 [5 3 6] 7 6
* 1 3 -1 -3 5 [3 6 7] 7
*
*
* Note:
* You may assume k is always valid, 1 ≤ k ≤ input array's size for non-empty
* array.
*
* Follow up:
* Could you solve it in linear time?
*/
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
var
maxSlidingWindow
=
function
(
nums
,
k
)
{
// bad 时间复杂度O(n^2)
if
(
nums
.
length
===
0
||
k
===
0
)
return
[];
let
slideWindow
=
[];
const
ret
=
[];
for
(
let
i
=
0
;
i
<
nums
.
length
-
k
+
1
;
i
++
)
{
for
(
let
j
=
0
;
j
<
k
;
j
++
)
{
slideWindow
.
push
(
nums
[
i
+
j
]);
}
ret
.
push
(
Math
.
max
(...
slideWindow
));
slideWindow
=
[];
}
return
ret
;
// 双端队列优化时间复杂度
};
todo/str/214.shortest-palindrome.js
0 → 100644
浏览文件 @
b159f16f
/*
* @lc app=leetcode id=214 lang=javascript
*
* [214] Shortest Palindrome
*
* https://leetcode.com/problems/shortest-palindrome/description/
*
* algorithms
* Hard (27.15%)
* Total Accepted: 73.1K
* Total Submissions: 266.3K
* Testcase Example: '"aacecaaa"'
*
* Given a string s, you are allowed to convert it to a palindrome by adding
* characters in front of it. Find and return the shortest palindrome you can
* find by performing this transformation.
*
* Example 1:
*
*
* Input: "aacecaaa"
* Output: "aaacecaaa"
*
*
* Example 2:
*
*
* Input: "abcd"
* Output: "dcbabcd"
*/
/**
* @param {string} s
* @return {string}
*/
var
shortestPalindrome
=
function
(
s
)
{
};
todo/str/5.longest-palindromic-substring.js
0 → 100644
浏览文件 @
b159f16f
/*
* @lc app=leetcode id=5 lang=javascript
*
* [5] Longest Palindromic Substring
*
* https://leetcode.com/problems/longest-palindromic-substring/description/
*
* algorithms
* Medium (26.70%)
* Total Accepted: 525K
* Total Submissions: 1.9M
* Testcase Example: '"babad"'
*
* Given a string s, find the longest palindromic substring in s. You may
* assume that the maximum length of s is 1000.
*
* Example 1:
*
*
* Input: "babad"
* Output: "bab"
* Note: "aba" is also a valid answer.
*
*
* Example 2:
*
*
* Input: "cbbd"
* Output: "bb"
*
*
*/
/**
* @param {string} s
* @return {string}
*/
var
longestPalindrome
=
function
(
s
)
{
};
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录