Skip to content

Commit d891134

Browse files
author
lucifer
committed
feat: 模板增加 bisect_left 和 bisect_right
1 parent 9e87e42 commit d891134

File tree

8 files changed

+116
-44
lines changed

8 files changed

+116
-44
lines changed

‎leetcode-cheat.zip

-15.7 KB
Binary file not shown.

‎public/manifest.json

+13-15
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,15 @@
11
{
2-
"manifest_version": 2,
3-
"name": "leetcode cheatsheet",
4-
"description": "刷题小助手,made by 力扣加加",
5-
"version": "0.0.1",
6-
"browser_action": {
7-
"default_popup": "index.html",
8-
"default_title": "力扣加加"
9-
},
10-
"icons": {
11-
"128": "logo.png"
12-
},
13-
"permissions": [
14-
"tabs"
15-
],
16-
"content_security_policy": "script-src 'self' 'sha256-9HcBuUP35aPkU0991A4mASdsuifTkUlifJ7elThz6Ow=' 'sha256-0Jo/EYaXS11i7poc/P9fGcq/o6P0djny2JW6WivTVVw='; object-src 'self'"
2+
"manifest_version": 2,
3+
"name": "leetcode cheatsheet",
4+
"description": "刷题小助手,made by 力扣加加",
5+
"version": "0.2.1",
6+
"browser_action": {
7+
"default_popup": "index.html",
8+
"default_title": "力扣加加"
9+
},
10+
"icons": {
11+
"128": "logo.png"
12+
},
13+
"permissions": ["tabs"],
14+
"content_security_policy": "script-src 'self' 'sha256-9HcBuUP35aPkU0991A4mASdsuifTkUlifJ7elThz6Ow=' 'sha256-0Jo/EYaXS11i7poc/P9fGcq/o6P0djny2JW6WivTVVw='; object-src 'self'"
1715
}

‎scripts/generateleetcode.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,13 @@ const genertateLeetcodeToJson = () => {
6666
const match = /\[(.+)\]\((.+)\)/.exec(preTagName);
6767
if (match) {
6868
// 形���1
69+
// 有可能有相对定位的文件(比如 ../thinkings),需要特别处理
6970
preKnowledge.push({
7071
text: match[1],
71-
link: match[2],
72+
link: match[2].replace(
73+
"../thinkings/",
74+
"https://github.com/azl397985856/leetcode/blob/master/thinkings/"
75+
),
7276
color: Utils.getColor(match[1]),
7377
});
7478
} else {

‎src/App.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@ import { Button, Table, Empty, Collapse, Tabs } from "antd";
44
import "highlight.js/styles/github.css";
55

66
import db from "./db/db";
7-
import { LEETCODE_CN_URL, LEETCODE_URL } from "./constant/index";
7+
import {
8+
LEETCODE_CN_URL,
9+
LEETCODE_URL,
10+
CONTRIBUTE_PROGRAMMING_LANGUAGE_URL,
11+
} from "./constant/index";
812
import ProblemDetail from "./Detail";
913
import TagOrLink from "./TagOrLink";
1014
import tempaltes from "./codeTemplates/index";
1115
import Codes from "./codes";
1216
import { bfs } from "./utils";
13-
import drawTree from "canvas-binary-tree";
17+
// import drawTree from "canvas-binary-tree";
1418
import "antd/dist/antd.css";
1519
import "./App.css";
16-
import { data as a } from "./db/binary-tree";
20+
// import { data as a } from "./db/binary-tree";
1721

1822
const { problems } = db;
1923
const { TabPane } = Tabs;
@@ -94,13 +98,14 @@ function App() {
9498

9599
// drawTree(canvas, bfs([1, 2, 2, 3, 1, 2, 2, 323213213232329]));
96100
// }, 1000);
97-
console.log(a);
101+
// console.log(a);
98102

99103
return (
100104
<div className="container">
101105
<div
102106
className="tree-vis"
103107
style={{
108+
display: "none",
104109
position: "fixed",
105110
zIndex: 99,
106111
top: 0,
@@ -110,9 +115,7 @@ function App() {
110115
backgroundColor: "rgba(0,0,0,.4)",
111116
}}
112117
>
113-
<div>
114-
<pre>{a}</pre>
115-
</div>
118+
<div>{/* <pre>{a}</pre> */}</div>
116119
<canvas width="1000" height="1000" id="canvas"></canvas>
117120
</div>
118121

@@ -175,6 +178,12 @@ function App() {
175178
</ul>
176179
</div>
177180
<Codes codes={codes} />
181+
<Button
182+
type="link"
183+
href={CONTRIBUTE_PROGRAMMING_LANGUAGE_URL}
184+
>
185+
纠错 or 贡献其他语言
186+
</Button>
178187
</Panel>
179188
</Collapse>
180189
))}

‎src/Detail.jsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ export default function Detail({ problemId }) {
107107
></TabPane>
108108

109109
<TabPane tab="我要反馈" key="feedback">
110-
<div>当前版本: V 0.2.0</div>
110+
<div>当前版本: V 0.2.1</div>
111111
<a
112112
href={ISSUES_URL}
113113
target="_blank"

‎src/codeTemplates/binarySearch.js

+58
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,64 @@ module.exports = {
216216
},
217217
],
218218
},
219+
{
220+
text: "寻找最左插入位置",
221+
codes: [
222+
{
223+
language: "py",
224+
text: `
225+
def bisect_left(nums, x):
226+
# 内置 api
227+
bisect.bisect_left(nums, x)
228+
# 手写
229+
l, r = 0, len(nums) - 1
230+
while l < r:
231+
mid = (l + r) // 2
232+
if nums[mid] < x:
233+
l = mid + 1
234+
else:
235+
r = mid
236+
# 由于 l 和 r 相等,因此返回谁都无所谓。
237+
return l
238+
`,
239+
},
240+
],
241+
problems: [
242+
{
243+
id: "random-pick-with-weight",
244+
title: "528. 按权重随机选择",
245+
},
246+
],
247+
},
248+
{
249+
text: "寻找最右插入位置",
250+
codes: [
251+
{
252+
language: "py",
253+
text: `
254+
def bisect_right(nums, x):
255+
# 内置 api
256+
bisect.bisect_right(nums, x)
257+
# 手写
258+
l, r = 0, len(nums) - 1
259+
while l < r:
260+
mid = (l + r) // 2
261+
if nums[mid] > x:
262+
r = mid
263+
else:
264+
l = mid + 1
265+
# 由于 l 和 r 相等,因此返回谁都无所谓。
266+
return l
267+
`,
268+
},
269+
],
270+
problems: [
271+
{
272+
id: "find-first-and-last-position-of-element-in-sorted-array",
273+
title: "34. 在排序数组中查找元素的第一个和最后一个位置",
274+
},
275+
],
276+
},
219277
],
220278
link:
221279
"https://github.com/azl397985856/leetcode/blob/master/91/binary-search.md",

‎src/constant/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ export const ISSUES_URL =
44
"https://github.com/azl397985856/leetcode-cheat/issues";
55
export const CONTRIBUTE_COMPANY_URL =
66
"https://github.com/azl397985856/leetcode-cheat/issues/1";
7+
8+
export const CONTRIBUTE_PROGRAMMING_LANGUAGE_URL =
9+
"https://github.com/azl397985856/leetcode-cheat/issues/4";

‎src/db/root.db.js

+20-20
Original file line numberDiff line numberDiff line change
@@ -1914,7 +1914,7 @@
19141914
"pre": [
19151915
{
19161916
"text": "贪心",
1917-
"link": "../thinkings/greedy.md",
1917+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/greedy.md",
19181918
"color": "purple"
19191919
}
19201920
],
@@ -2146,7 +2146,7 @@
21462146
},
21472147
{
21482148
"text": "动态规划",
2149-
"link": "../thinkings/dynamic-programming.md",
2149+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/dynamic-programming.md",
21502150
"color": "red"
21512151
}
21522152
],
@@ -5357,7 +5357,7 @@
53575357
"pre": [
53585358
{
53595359
"text": "前缀树",
5360-
"link": "../thinkings/trie.md",
5360+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md",
53615361
"color": "purple"
53625362
}
53635363
],
@@ -5405,7 +5405,7 @@
54055405
"pre": [
54065406
{
54075407
"text": "滑动窗口",
5408-
"link": "../thinkings/slide-window.md",
5408+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/slide-window.md",
54095409
"color": "purple"
54105410
}
54115411
],
@@ -5453,7 +5453,7 @@
54535453
"pre": [
54545454
{
54555455
"text": "前缀树",
5456-
"link": "../thinkings/trie.md",
5456+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md",
54575457
"color": "purple"
54585458
}
54595459
],
@@ -5499,17 +5499,17 @@
54995499
"pre": [
55005500
{
55015501
"text": "前缀树",
5502-
"link": "../thinkings/trie.md",
5502+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md",
55035503
"color": "purple"
55045504
},
55055505
{
55065506
"text": "深度优先遍历",
5507-
"link": "../thinkings/DFS.md",
5507+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/DFS.md",
55085508
"color": "geekblue"
55095509
},
55105510
{
55115511
"text": "小岛专题",
5512-
"link": "../thinkings/island.md",
5512+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/island.md",
55135513
"color": "purple"
55145514
},
55155515
{
@@ -6071,19 +6071,19 @@
60716071
"code": [
60726072
{
60736073
"language": "js",
6074-
"text": "\nvar maxSlidingWindow = function(nums, k) {\n // bad 时间复杂度O(n * k)\n if (nums.length === 0 || k === 0) return [];\n let slideWindow = [];\n const ret = [];\n for (let i = 0; i < nums.length - k + 1; i++) {\n for (let j = 0; j < k; j++) {\n slideWindow.push(nums[i + j]);\n }\n ret.push(Math.max(...slideWindow));\n slideWindow = [];\n }\n return ret;\n};\n"
6074+
"text": "\nvar maxSlidingWindow = function (nums, k) {\n // bad 时间复杂度O(n * k)\n if (nums.length === 0 || k === 0) return [];\n let slideWindow = [];\n const ret = [];\n for (let i = 0; i < nums.length - k + 1; i++) {\n for (let j = 0; j < k; j++) {\n slideWindow.push(nums[i + j]);\n }\n ret.push(Math.max(...slideWindow));\n slideWindow = [];\n }\n return ret;\n};\n"
60756075
},
60766076
{
60776077
"language": "js",
6078-
"text": "\nvar maxSlidingWindow = function(nums, k) {\n // 双端队列优化时间复杂度, 时间复杂度O(n)\n const deque = []; // 存放在接下来的滑动窗口可能成为最大值的数\n const ret = [];\n for (let i = 0; i < nums.length; i++) {\n // 清空失效元素\n while (deque[0] < i - k + 1) {\n deque.shift();\n }\n\n while (nums[deque[deque.length - 1]] < nums[i]) {\n deque.pop();\n }\n\n deque.push(i);\n\n if (i >= k - 1) {\n ret.push(nums[deque[0]]);\n }\n }\n return ret;\n};\n"
6078+
"text": "\nvar maxSlidingWindow = function (nums, k) {\n // 双端队列优化时间复杂度, 时间复杂度O(n)\n const deque = []; // 存放在接下来的滑动窗口可能成为最大值的数\n const ret = [];\n for (let i = 0; i < nums.length; i++) {\n // 清空失效元素\n while (deque[0] < i - k + 1) {\n deque.shift();\n }\n\n while (nums[deque[deque.length - 1]] < nums[i]) {\n deque.pop();\n }\n\n deque.push(i);\n\n if (i >= k - 1) {\n ret.push(nums[deque[0]]);\n }\n }\n return ret;\n};\n"
60796079
},
60806080
{
60816081
"language": "py",
60826082
"text": "\nclass Solution:\n def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:\n if k == 0: return []\n res = []\n for r in range(k - 1, len(nums)):\n res.append(max(nums[r - k + 1:r + 1]))\n return res\n"
60836083
},
60846084
{
60856085
"language": "py",
6086-
"text": "\nclass Solution:\n def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:\n deque, res, n = [], [], len(nums)\n for i in range(n):\n while deque and deque[0] < i - k + 1:\n deque.pop(0)\n while deque and nums[i] > nums[deque[-1]]:\n deque.pop(-1)\n deque.append(i)\n if i >= k - 1: res.append(nums[deque[0]])\n return res\n\n\n"
6086+
"text": "\nclass Solution:\n def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:\n deque, res, n = collections.deque(), [], len(nums)\n for i in range(n):\n # 移除前面实现的元素,整因为如此,才需要双端队列\n while deque and deque[0] < i - k + 1:\n deque.popleft()\n # 下面三行,类似单调递增栈\n while deque and nums[i] > nums[deque[-1]]:\n deque.pop()\n deque.append(i)\n if i >= k - 1:\n res.append(nums[deque[0]])\n return res\n"
60876087
}
60886088
]
60896089
},
@@ -6719,7 +6719,7 @@
67196719
"color": "blue"
67206720
},
67216721
{
6722-
"text": "对于这种$O(1)$空间复杂度有固定的套路。常见的有:1.直接修改原数组2.滑动窗口(当前状态并不是和之前所有状态有关,而是仅和某几个有关)。我们采用的是滑动窗口。但是难点就在于我们怎么知道当前状态和哪几个有关。对于这道题来说,画图或许可以帮助你打开思路。另外面试的时候说出$O(N)$的思路也不失为一个帮助你冷静分析问题的手段。",
6722+
"text": "对于这种$$O(1)$$空间复杂度有固定的套路。常见的有:1.直接修改原数组2.滑动窗口(当前状态并不是和之前所有状态有关,而是仅和某几个有关)。我们采用的是滑动窗口。但是难点就在于我们怎么知道当前状态和哪几个有关。对于这道题来说,画图或许可以帮助你打开思路。另外面试的时候说出$$O(N)$$的思路也不失为一个帮助你冷静分析问题的手段。",
67236723
"link": null,
67246724
"color": "blue"
67256725
}
@@ -7511,7 +7511,7 @@
75117511
"pre": [
75127512
{
75137513
"text": "前缀树",
7514-
"link": "../thinkings/trie.md",
7514+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md",
75157515
"color": "purple"
75167516
}
75177517
],
@@ -8443,7 +8443,7 @@
84438443
"pre": [
84448444
{
84458445
"text": "前缀树",
8446-
"link": "../thinkings/trie.md",
8446+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md",
84478447
"color": "purple"
84488448
}
84498449
],
@@ -8899,7 +8899,7 @@
88998899
"pre": [
89008900
{
89018901
"text": "滑动窗口",
8902-
"link": "../thinkings/slide-window.md",
8902+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/slide-window.md",
89038903
"color": "purple"
89048904
}
89058905
],
@@ -9284,7 +9284,7 @@
92849284
"pre": [
92859285
{
92869286
"text": "前缀树",
9287-
"link": "../thinkings/trie.md",
9287+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/trie.md",
92889288
"color": "purple"
92899289
}
92909290
],
@@ -9846,7 +9846,7 @@
98469846
"pre": [
98479847
{
98489848
"text": "前缀和",
9849-
"link": "../thinkings/prefix.md",
9849+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/prefix.md",
98509850
"color": "cyan"
98519851
}
98529852
],
@@ -9951,7 +9951,7 @@
99519951
"pre": [
99529952
{
99539953
"text": "前缀和",
9954-
"link": "../thinkings/prefix.md",
9954+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/prefix.md",
99559955
"color": "cyan"
99569956
},
99579957
{
@@ -10139,7 +10139,7 @@
1013910139
},
1014010140
{
1014110141
"text": "深度优先遍历",
10142-
"link": "../thinkings/DFS.md",
10142+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/DFS.md",
1014310143
"color": "geekblue"
1014410144
},
1014510145
{
@@ -10183,7 +10183,7 @@
1018310183
},
1018410184
{
1018510185
"text": "二叉树的遍历",
10186-
"link": "../thinkings/binary-tree-traversal.md",
10186+
"link": "https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md",
1018710187
"color": "red"
1018810188
}
1018910189
],

0 commit comments

Comments
 (0)