Skip to content

Commit

Permalink
更新代码块
Browse files Browse the repository at this point in the history
  • Loading branch information
youngyangyang04 committed Aug 10, 2021
1 parent c7c34dd commit 8a2d420
Show file tree
Hide file tree
Showing 192 changed files with 552 additions and 552 deletions.
12 changes: 6 additions & 6 deletions problems/0005.最长回文子串.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@

以上三种情况分析完了,那么递归公式如下:

```C++
```CPP
if (s[i] == s[j]) {
if (j - i <= 1) { // 情况一 和 情况二
dp[i][j] = true;
Expand All @@ -81,7 +81,7 @@ if (s[i] == s[j]) {

在得到[i,j]区间是否是回文子串的时候,直接保存最长回文子串的左边界和右边界,代码如下:

```C++
```CPP
if (s[i] == s[j]) {
if (j - i <= 1) { // 情况一 和 情况二
dp[i][j] = true;
Expand Down Expand Up @@ -120,7 +120,7 @@ dp[i + 1][j - 1] 在 dp[i][j]的左下角,如图:

代码如下:

```C++
```CPP
for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序
for (int j = i; j < s.size(); j++) {
if (s[i] == s[j]) {
Expand Down Expand Up @@ -150,7 +150,7 @@ for (int i = s.size() - 1; i >= 0; i--) { // 注意遍历顺序

以上分析完毕,C++代码如下:

```C++
```CPP
class Solution {
public:
string longestPalindrome(string s) {
Expand Down Expand Up @@ -181,7 +181,7 @@ public:
```
以上代码是为了凸显情况一二三,当然是可以简洁一下的,如下:

```C++
```CPP
class Solution {
public:
string longestPalindrome(string s) {
Expand Down Expand Up @@ -226,7 +226,7 @@ public:
**这两种情况可以放在一起计算,但分别计算思路更清晰,我倾向于分别计算**,代码如下:
```C++
```CPP
class Solution {
public:
int left = 0;
Expand Down
4 changes: 2 additions & 2 deletions problems/0015.三数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ https://leetcode-cn.com/problems/3sum/
大家可以尝试使用哈希法写一写,就知道其困难的程度了。

哈希法C++代码:
```C++
```CPP
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
Expand Down Expand Up @@ -107,7 +107,7 @@ public:
C++代码代码如下:
```C++
```CPP
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0018.四数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ https://leetcode-cn.com/problems/4sum/

C++代码

```C++
```CPP
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0019.删除链表的倒数第N个节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@

此时不难写出如下C++代码:

```C++
```CPP
class Solution {
public:
ListNode* removeNthFromEnd(ListNode* head, int n) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0020.有效的括号.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ cd a/b/c/../../
实现C++代码如下:


```C++
```CPP
class Solution {
public:
bool isValid(string s) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0024.两两交换链表中的节点.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ https://leetcode-cn.com/problems/swap-nodes-in-pairs/

对应的C++代码实现如下: (注释中详细和如上图中的三步做对应)

```C++
```CPP
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
Expand Down
4 changes: 2 additions & 2 deletions problems/0027.移除元素.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

代码如下:

```C++
```CPP
// 时间复杂度:O(n^2)
// 空间复杂度:O(1)
class Solution {
Expand Down Expand Up @@ -85,7 +85,7 @@ public:

后序都会一一介绍到,本题代码如下:

```C++
```CPP
// 时间复杂度:O(n)
// 空间复杂度:O(1)
class Solution {
Expand Down
12 changes: 6 additions & 6 deletions problems/0028.实现strStr.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ next[i] = j;

最后整体构建next数组的函数代码如下:

```C++
```CPP
void getNext(int* next, const string& s){
    int j = -1;
    next[0] = j;
Expand Down Expand Up @@ -386,7 +386,7 @@ if (j == (t.size() - 1) ) {

那么使用next数组,用模式串匹配文本串的整体代码如下:

```C++
```CPP
int j = -1; // 因为next数组里记录的起始位置为-1
for (int i = 0; i < s.size(); i++) { // 注意i就从0开始
    while(j >= 0 && s[i] != t[j + 1]) { // 不匹配
Expand All @@ -405,7 +405,7 @@ for (int i = 0; i < s.size(); i++) { // 注意i就从0开始

# 前缀表统一减一 C++代码实现

```C++
```CPP
class Solution {
public:
    void getNext(int* next, const string& s) {
Expand Down Expand Up @@ -457,7 +457,7 @@ public:
我给出的getNext的实现为:(前缀表统一减一)
```C++
```CPP
void getNext(int* next, const string& s) {
    int j = -1;
    next[0] = j;
Expand All @@ -479,7 +479,7 @@ void getNext(int* next, const string& s) {

那么前缀表不减一来构建next数组,代码如下:

```C++
```CPP
void getNext(int* next, const string& s) {
int j = 0;
next[0] = 0;
Expand All @@ -502,7 +502,7 @@ void getNext(int* next, const string& s) {
实现代码如下:
```C++
```CPP
class Solution {
public:
void getNext(int* next, const string& s) {
Expand Down
2 changes: 1 addition & 1 deletion problems/0031.下一个排列.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@

对应的C++代码如下:

```C++
```CPP
class Solution {
public:
void nextPermutation(vector<int>& nums) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

可以写出如下代码

```C++
```CPP
// 二分查找,寻找target的右边界(不包括target)
// 如果rightBorder为没有被赋值(即target在数组范围的左边,例如数组[3,3],target为2),为了处理情况一
int getRightBorder(vector<int>& nums, int target) {
Expand All @@ -86,7 +86,7 @@ int getRightBorder(vector<int>& nums, int target) {
## 寻找左边界
```C++
```CPP
// 二分查找,寻找target的左边界leftBorder(不包括target)
// 如果leftBorder没有被赋值(即target在数组范围的右边,例如数组[3,3],target为4),为了处理情况一
int getLeftBorder(vector<int>& nums, int target) {
Expand All @@ -110,7 +110,7 @@ int getLeftBorder(vector<int>& nums, int target) {

左右边界计算完之后,看一下主体代码,这里把上面讨论的三种情况,都覆盖了

```C++
```CPP
class Solution {
public:
vector<int> searchRange(vector<int>& nums, int target) {
Expand Down
4 changes: 2 additions & 2 deletions problems/0035.搜索插入位置.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public:

**大家要仔细看注释,思考为什么要写while(left <= right), 为什么要写right = middle - 1**

```C++
```CPP
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
Expand Down Expand Up @@ -158,7 +158,7 @@ public:
**大家要仔细看注释,思考为什么要写while (left < right), 为什么要写right = middle**。
```C++
```CPP
class Solution {
public:
int searchInsert(vector<int>& nums, int target) {
Expand Down
6 changes: 3 additions & 3 deletions problems/0037.解数独.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ bool backtracking(vector<vector<char>>& board)

代码如下:(**详细看注释**

```C++
```CPP
bool backtracking(vector<vector<char>>& board) {
for (int i = 0; i < board.size(); i++) { // 遍历行
for (int j = 0; j < board[0].size(); j++) { // 遍历列
Expand Down Expand Up @@ -125,7 +125,7 @@ bool backtracking(vector<vector<char>>& board) {
代码如下:
```C++
```CPP
bool isValid(int row, int col, char val, vector<vector<char>>& board) {
for (int i = 0; i < 9; i++) { // 判断行里是否重复
if (board[row][i] == val) {
Expand Down Expand Up @@ -154,7 +154,7 @@ bool isValid(int row, int col, char val, vector<vector<char>>& board) {

## C++代码

```C++
```CPP
class Solution {
private:
bool backtracking(vector<vector<char>>& board) {
Expand Down
10 changes: 5 additions & 5 deletions problems/0039.组合总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ candidates 中的数字可以无限制重复被选取。

代码如下:

```C++
```CPP
vector<vector<int>> result;
vector<int> path;
void backtracking(vector<int>& candidates, int target, int sum, int startIndex)
Expand All @@ -89,7 +89,7 @@ void backtracking(vector<int>& candidates, int target, int sum, int startIndex)
sum等于target的时候,需要收集结果,代码如下:
```C++
```CPP
if (sum > target) {
return;
}
Expand All @@ -107,7 +107,7 @@ if (sum == target) {

如何重复选取呢,看代码,注释部分:

```C++
```CPP
for (int i = startIndex; i < candidates.size(); i++) {
sum += candidates[i];
path.push_back(candidates[i]);
Expand All @@ -119,7 +119,7 @@ for (int i = startIndex; i < candidates.size(); i++) {

按照[关于回溯算法,你该了解这些!](https://mp.weixin.qq.com/s/gjSgJbNbd1eAA5WkA-HeWw)中给出的模板,不难写出如下C++完整代码:

```C++
```CPP
// 版本一
class Solution {
private:
Expand Down Expand Up @@ -179,7 +179,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;
整体代码如下:(注意注释的部分)
```C++
```CPP
class Solution {
private:
vector<vector<int>> result;
Expand Down
10 changes: 5 additions & 5 deletions problems/0040.组合总和II.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ candidates 中的每个数字在每个组合中只能使用一次。

代码如下:

```C++
```CPP
vector<vector<int>> result; // 存放组合集合
vector<int> path; // 符合条件的组合
void backtracking(vector<int>& candidates, int target, int sum, int startIndex, vector<bool>& used) {
Expand All @@ -102,7 +102,7 @@ void backtracking(vector<int>& candidates, int target, int sum, int startIndex,
代码如下:
```C++
```CPP
if (sum > target) { // 这个条件其实可以省略
return;
}
Expand Down Expand Up @@ -137,7 +137,7 @@ if (sum == target) {

那么单层搜索的逻辑代码如下:

```C++
```CPP
for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target; i++) {
// used[i - 1] == true,说明同一树支candidates[i - 1]使用过
// used[i - 1] == false,说明同一树层candidates[i - 1]使用过
Expand All @@ -161,7 +161,7 @@ for (int i = startIndex; i < candidates.size() && sum + candidates[i] <= target;

回溯三部曲分析完了,整体C++代码如下:

```C++
```CPP
class Solution {
private:
vector<vector<int>> result;
Expand Down Expand Up @@ -206,7 +206,7 @@ public:
这里直接用startIndex来去重也是可以的, 就不用used数组了。
```C++
```CPP
class Solution {
private:
vector<vector<int>> result;
Expand Down
Loading

0 comments on commit 8a2d420

Please sign in to comment.