Skip to content

Commit

Permalink
feat: #1023
Browse files Browse the repository at this point in the history
  • Loading branch information
lucifer committed Jan 1, 2020
1 parent a89493e commit b9bb197
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 39 deletions.
68 changes: 29 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
![leetcode.jpeg](./assets/leetcode.jpeg)

这个是我写的[纪念项目 Star 突破 1W 的一个短文](./thanksGiving.md), 记录了项目的"兴起"之路, 大家有兴趣可以看一下,
如果对这个项目感兴趣,请**点击一下Star**, 项目会**持续更新**,感谢大家的支持。
如果对这个项目感兴趣,请**点击一下 Star**, 项目会**持续更新**,感谢大家的支持。

## 介绍

Expand All @@ -30,34 +30,34 @@ leetcode 题解,记录自己的 leetcode 解题之路。

- 第三部分是 anki 卡片, 将 leetcode 题目按照一定的方式记录在 anki 中,方便大家记忆。

- 第四部分是每日一题,每日一题是在交流群(包括微信和qq)里进行的一种活动,大家一起 解一道题,这样讨论问题更加集中,会得到更多的反馈。而且 这些题目可以被记录下来,日后会进行筛选添加到仓库的题解模块。
- 第四部分是每日一题,每日一题是在交流群(包括微信和 qq)里进行的一种活动,大家一起 解一道题,这样讨论问题更加集中,会得到更多的反馈。而且 这些题目可以被记录下来,日后会进行筛选添加到仓库的题解模块。

- 第五部分是计划, 这里会记录将来要加入到以上三个部分内容

> 只有熟练掌握基础的数据结构与算法,才能对复杂问题迎刃有余。
## 关于我

擅长前端工程化,前端性能优化,前端标准化等,做过.net, 搞过Java,现在是一名前端工程师,我的个人博客:https://lucifer.ren/blog/
擅长前端工程化,前端性能优化,前端标准化等,做过.net, 搞过 Java,现在是一名前端工程师,我的个人博客:https://lucifer.ren/blog/

我经常会在开源社区进行一些输出和分享,比较受欢迎的有[宇宙最强的前端面试指南](https://github.com/azl397985856/fe-interview)
[我的第一本小书](https://github.com/azl397985856/automate-everything)。目前本人正在写一本关于《leetcode题解》的实体书,因此可能更新会比较慢,如果有人想要做些贡献或者合作的也可以直接用下面的邮箱联系我。
[我的第一本小书](https://github.com/azl397985856/automate-everything)。目前本人正在写一本关于《leetcode 题解》的实体书,因此可能更新会比较慢,如果有人想要做些贡献或者合作的也可以直接用下面的邮箱联系我。

另外如果大家需要内推的可以找我,我这里有包括阿里,腾讯,头条,网易等很多公司的朋友。有需要可以直接群里联系我,或者发送到我的个人邮箱 [[email protected]]

## 食用指南

- 对于最近添加的部分, 后面会有 🆕 标注
- 对于最近更新的部分, 后面会有 🖊 标注
- 将来会在这里更新anki卡片
- 这里有一份leetcode官方账号在知乎上给出的一个《互联网公司最常见的面试算法题有哪些?》的答案,我这里尽量去覆盖回答中的题目和知识点
原文地址: https://www.zhihu.com/question/24964987/answer/586425979
- 将来会在这里更新 anki 卡片
- 这里有一份 leetcode 官方账号在知乎上给出的一个《互联网公司最常见的面试算法题有哪些?》的答案,我这里尽量去覆盖回答中的题目和知识点
原文地址: https://www.zhihu.com/question/24964987/answer/586425979

- 这里有一张互联网公司面试中经常考察的问题类型总结的思维导图,我们可以结合图片中的信息分析一下。

![leetcode-zhihu](./assets//leetcode-zhihu.jpg)

(图片来自leetcode)
(图片来自 leetcode)

其中算法,主要是以下几种:

Expand All @@ -76,39 +76,29 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- 树与图:最近公共祖先、并查集
- 字符串:前缀树(字典树) / 后缀树




## 精彩预告


[0042.trapping-rain-water](./problems/42.trapping-rain-water.md):

![0042.trapping-rain-water](./assets/problems/42.trapping-rain-water-1.png)


[0547.friend-circles](./problems/547.friend-circles-en.md):

<img width="600" src="./assets/problems/547.friend-circle-bfs.png">



[backtrack problems](./problems/90.subsets-ii.md):

<img width="600" src="./assets/problems/backtrack.png">



[0198.house-robber](./problems/198.house-robber.md):

<img width="600" src="./assets/problems/198.house-robber.png">


[0454.4-sum-ii](./problems/454.4-sum-ii.md):

<img width="600" src="./assets/problems/454.4-sum-ii.png">

## Top题目进度
## Top 题目进度

- [Top 100 Liked Questions](https://leetcode.com/problemset/top-100-liked-questions/) (79 / 100)

Expand Down Expand Up @@ -151,7 +141,6 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0371.sum-of-two-integers](./problems/371.sum-of-two-integers.md)
- [0575.distribute-candies](./problems/575.distribute-candies.md)


#### 中等难度

- [0002. Add Two Numbers](./problems/2.addTwoNumbers.md)
Expand All @@ -163,17 +152,17 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0024. Swap Nodes In Pairs](./problems/24.swapNodesInPairs.md)
- [0029.divide-two-integers](./problems/29.divide-two-integers.md)
- [0031.next-permutation](./problems/31.next-permutation.md)
- [0033.search-in-rotated-sorted-array](./problems/33.search-in-rotated-sorted-array.md)
- [0033.search-in-rotated-sorted-array](./problems/33.search-in-rotated-sorted-array.md)
- [0039.combination-sum](./problems/39.combination-sum.md)
- [0040.combination-sum-ii](./problems/40.combination-sum-ii.md)
- [0046.permutations](./problems/46.permutations.md)
- [0047.permutations-ii](./problems/47.permutations-ii.md)
- [0048.rotate-image](./problems/48.rotate-image.md)
- [0049.group-anagrams](./problems/49.group-anagrams.md)
- [0049.group-anagrams](./problems/49.group-anagrams.md)
- [0055.jump-game](./problems/55.jump-game.md)
- [0056.merge-intervals](./problems/56.merge-intervals.md)
- [0062.unique-paths](./problems/62.unique-paths.md )
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md )
- [0062.unique-paths](./problems/62.unique-paths.md)
- [0073.set-matrix-zeroes](./problems/73.set-matrix-zeroes.md)
- [0075.sort-colors](./problems/75.sort-colors.md)
- [0078.subsets](./problems/78.subsets.md)
- [0079.word-search](./problems/79.word-search-en.md)
Expand All @@ -182,36 +171,36 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0091.decode-ways](./problems/91.decode-ways.md)
- [0092.reverse-linked-list-ii](./problems/92.reverse-linked-list-ii.md)
- [0094.binary-tree-inorder-traversal](./problems/94.binary-tree-inorder-traversal.md)
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md)
- [0098.validate-binary-search-tree](./problems/98.validate-binary-search-tree.md)
- [0102.binary-tree-level-order-traversal](./problems/102.binary-tree-level-order-traversal.md)
- [0103.binary-tree-zigzag-level-order-traversal](./problems/103.binary-tree-zigzag-level-order-traversal.md)
- [0113.path-sum-ii](./problems/113.path-sum-ii.md)
- [0129.sum-root-to-leaf-numbers](./problems/129.sum-root-to-leaf-numbers.md)
- [0130.surrounded-regions](./problems/130.surrounded-regions.md)
- [0129.sum-root-to-leaf-numbers](./problems/129.sum-root-to-leaf-numbers.md)
- [0130.surrounded-regions](./problems/130.surrounded-regions.md)
- [0131.palindrome-partitioning](./problems/131.palindrome-partitioning.md)
- [0139.word-break](./problems/139.word-break.md)
- [0144.binary-tree-preorder-traversal](./problems/144.binary-tree-preorder-traversal.md)
- [0150.evaluate-reverse-polish-notation](./problems/150.evaluate-reverse-polish-notation.md)
- [0152.maximum-product-subarray](./problems/152.maximum-product-subarray.md)
- [0199.binary-tree-right-side-view](./problems/199.binary-tree-right-side-view.md)
- [0200.number-of-islands](./problems/200.number-of-islands.md) 🆕
- [0200.number-of-islands](./problems/200.number-of-islands.md) 🆕
- [0201.bitwise-and-of-numbers-range](./problems/201.bitwise-and-of-numbers-range.md)
- [0208.implement-trie-prefix-tree](./problems/208.implement-trie-prefix-tree.md)
- [0209.minimum-size-subarray-sum](./problems/209.minimum-size-subarray-sum.md)
- [0215.kth-largest-element-in-an-array](./problems/215.kth-largest-element-in-an-array.md) 🆕
- [0221.maximal-square](./problems/221.maximal-square.md)
- [0229.majority-element-ii](./problems/229.majority-element-ii.md) 🆕
- [0215.kth-largest-element-in-an-array](./problems/215.kth-largest-element-in-an-array.md) 🆕
- [0221.maximal-square](./problems/221.maximal-square.md)
- [0229.majority-element-ii](./problems/229.majority-element-ii.md) 🆕
- [0230.kth-smallest-element-in-a-bst](./problems/230.kth-smallest-element-in-a-bst.md)
- [0236.lowest-common-ancestor-of-a-binary-tree](./problems/236.lowest-common-ancestor-of-a-binary-tree.md)
- [0238.product-of-array-except-self](./problems/238.product-of-array-except-self.md)
- [0240.search-a-2-d-matrix-ii](./problems/240.search-a-2-d-matrix-ii.md)
- [0279.perfect-squares](./problems/279.perfect-squares.md)
- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md)
- [0309.best-time-to-buy-and-sell-stock-with-cooldown](./problems/309.best-time-to-buy-and-sell-stock-with-cooldown.md)
- [0322.coin-change](./problems/322.coin-change.md)
- [0328.odd-even-linked-list](./problems/328.odd-even-linked-list.md)
- [0334.increasing-triplet-subsequence](./problems/334.increasing-triplet-subsequence.md)
- [0365.water-and-jug-problem](./problems/365.water-and-jug-problem.md)
- [0378.kth-smallest-element-in-a-sorted-matrix](./problems/378.kth-smallest-element-in-a-sorted-matrix.md)
- [0378.kth-smallest-element-in-a-sorted-matrix](./problems/378.kth-smallest-element-in-a-sorted-matrix.md)
- [0416.partition-equal-subset-sum](./problems/416.partition-equal-subset-sum.md)
- [0445.add-two-numbers-ii](./problems/445.add-two-numbers-ii.md)
- [0454.4-sum-ii](./problems/454.4-sum-ii.md)
Expand All @@ -226,6 +215,7 @@ leetcode 题解,记录自己的 leetcode 解题之路。
- [0887.super-egg-drop](./problems/887.super-egg-drop.md)
- [0900.rle-iterator](./problems/900.rle-iterator.md)
- [0912.sort-an-array](./problems/912.sort-an-array.md) 🆕
- [1023.camelcase-matching](./problems/1023.camelcase-matching.md) 🆕
- [1031.maximum-sum-of-two-non-overlapping-subarrays](./problems/1031.maximum-sum-of-two-non-overlapping-subarrays.md)
- [1186.maximum-subarray-sum-with-one-deletion](./problems/1186.maximum-subarray-sum-with-one-deletion.md) 🆕
- [1218.longest-arithmetic-subsequence-of-given-difference.md](./problems/1218.longest-arithmetic-subsequence-of-given-difference.md) 🆕
Expand Down Expand Up @@ -259,15 +249,15 @@ leetcode 题解,记录自己的 leetcode 解题之路。

### anki 卡片

Anki主要分为两个部分:一部分是关键点到题目的映射,另一部分是题目到思路,关键点,代码的映射。
Anki 主要分为两个部分:一部分是关键点到题目的映射,另一部分是题目到思路,关键点,代码的映射。

全部卡片都在[anki-card](./assets/anki/leetcode.apkg)

使用方法:

anki - 文件 - 导入 - 下拉格式选择“打包的 anki集合”,然后选中你下载好的文件,确定即可。
anki - 文件 - 导入 - 下拉格式选择“打包的 anki 集合”,然后选中你下载好的文件,确定即可。

更多关于anki使用方法的请查看[anki官网](https://apps.ankiweb.net/)
更多关于 anki 使用方法的请查看[anki 官网](https://apps.ankiweb.net/)

目前已更新卡片一览(仅列举正面):

Expand All @@ -283,7 +273,7 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki集合”,然后
### 每日一题

每日一题是在交流群(包括微信和 qq)里通过issues来进行的一种活动,大家一起 解一道题,这样讨论问题更加集中,会得到更多的反馈。而且 这些题目可以被记录下来,日后会进行筛选添加到仓库的题解模块。
每日一题是在交流群(包括微信和 qq)里通过 issues 来进行的一种活动,大家一起 解一道题,这样讨论问题更加集中,会得到更多的反馈。而且 这些题目可以被记录下来,日后会进行筛选添加到仓库的题解模块。

- [每日一题汇总](./daily/)

Expand All @@ -309,19 +299,19 @@ anki - 文件 - 导入 - 下拉格式选择“打包的 anki集合”,然后

另外你可以回复大前端进大前端微信交流群, 回复 leetcode 拉你进 leetcode 微信群,如果想加入 qq 群,请回复 qq。


<img width="300" src="./assets/gongzhonghao.jpeg">

## 捐赠

[点击查看完整的捐赠列表](./donation.md)

### 微信

<img width="300" src="./assets/donate-weixin.jpg">

### 支付宝
<img width="300" src="./assets/donate-zfb.jpg">

<img width="300" src="./assets/donate-zfb.jpg">

## 贡献

Expand Down
132 changes: 132 additions & 0 deletions problems/1023.camelcase-matching.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
## 题目地址

https://leetcode-cn.com/problems/camelcase-matching/

## 题目描述

```
如果我们可以将小写字母插入模式串 pattern 得到待查询项 query,那么待查询项与给定模式串匹配。(我们可以在任何位置插入每个字符,也可以插入 0 个字符。)
给定待查询列表 queries,和模式串 pattern,返回由布尔值组成的答案列表 answer。只有在待查项 queries[i] 与模式串 pattern 匹配时, answer[i] 才为 true,否则为 false。
 
示例 1:
输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
输出:[true,false,true,true,false]
示例:
"FooBar" 可以这样生成:"F" + "oo" + "B" + "ar"。
"FootBall" 可以这样生成:"F" + "oot" + "B" + "all".
"FrameBuffer" 可以这样生成:"F" + "rame" + "B" + "uffer".
示例 2:
输入:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
输出:[true,false,true,false,false]
解释:
"FooBar" 可以这样生成:"Fo" + "o" + "Ba" + "r".
"FootBall" 可以这样生成:"Fo" + "ot" + "Ba" + "ll".
示例 3:
输出:queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
输入:[false,true,false,false,false]
解释:
"FooBarTest" 可以这样生成:"Fo" + "o" + "Ba" + "r" + "T" + "est".
 
提示:
1 <= queries.length <= 100
1 <= queries[i].length <= 100
1 <= pattern.length <= 100
所有字符串都仅由大写和小写英文字母组成。
```

## 思路

这道题是一道典型的双指针题目。不过这里的双指针并不是指向同一个数组或者字符串,而是指向多个,这道题是指向两个,分别是 query 和 pattern,这种题目非常常见,能够识别和掌握这种题目的解题模板非常重要。对 queries 的每一项我们的逻辑是一样的,这里就以其中一项为例进行讲解。

以 query 为 FooBar,pattern 为 FB 为例。

首先我们来简化一下问题,假如我们没有`可以在任何位置插入每个字符,也可以插入 0 个字符。`这个规则。我们的问题会比较简单,这个时候我们的算法是什么样的呢?一起来看下:

1. 首先我们建立两个指针 i 和 j 分别指向 query 和 pattern 的首字母。
2. 当 i 和 j 指向的字母相同的时候,我们同时向后移动两个指针一个单位。
3. 当 i 和 j 指向的字母不同的时候,我们直接返回 False

假如我们要找到的不是子串,而是子序列怎么办?我们不妨假设判断 pattern 是否是 query 的子序列。 其实 LeetCode 实际上也有这样的题目,我们来看下:

1. 首先我们建立两个指针 i 和 j 分别指向 query 和 pattern 的首字母。
2. 当 i 和 j 指向的字母相同的时候,我们同时向后移动两个指针一个单位。
3. 当 i 和 j 指向的字母不同的时候,我们移动 i 指针。
4. 当 i 超出 query 范围的时候,我们只需要判断 pattern 是否达到了终点即可。当然我们也可以提前退出。

我们直接参考下 LeetCode [392. 判断子序列](https://leetcode-cn.com/problems/is-subsequence/)

代码:

> 给定字符串 s 和 t ,判断 s 是否为 t 的子序列
```python
class Solution:
def isSubsequence(self, s: str, t: str) -> bool:
i = 0
j = 0
while j < len(t):
if i < len(s) and s[i] == t[j]:
i += 1
j += 1
else:
j += 1
if i >= len (s):
return True
return i == len(s)
```

然后我们加上`可以在任何位置插入每个字符,也可以插入 0 个字符。`这个规则。来看下有什么不同:

1. 首先我们建立两个指针 i 和 j 分别指向 query 和 pattern 的首字母。
2. 当 i 和 j 指向的字母相同的时候,我们同时向后移动两个指针一个单位。
3. 当 i 和 j 指向的字母不同的时候,我们继续判断 i 指向的元素是否是小写。
4. 如果是小写我们只把 i 向后移动一个单位。
5. 如果不是小写我们直接返回 False

## 关键点解析

- 双指针
- 字符串匹配
- 子序列
- 子串

## 代码

```python
class Solution:
def camelMatch(self, queries: List[str], pattern: str) -> List[bool]:
res = []
for query in queries:
i = 0
j = 0
while i < len(query):
if j < len(pattern) and query[i] == pattern[j]:
i += 1
j += 1
elif query[i].islower():
i += 1
else:
break
if i == len(query) and j == len(pattern):
res.append(True)
else:
res.append(False)
return res
```

## 扩展

这是一个符合直觉的解法,但是却不是一个很优秀的解法,那么你有想到什么优秀的解法么?

## 参考

- [392. 判断子序列](https://leetcode-cn.com/problems/is-subsequence/)

0 comments on commit b9bb197

Please sign in to comment.