-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
260 changed files
with
13,185 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# CodeBook2# CodeBook |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# C++STL:代码简洁之道一 | ||
|
||
## 1:C++-STL中lower_bound与upper_bound的用法 | ||
|
||
### 函数lower_bound() | ||
|
||
- **头文件:#include<algorithm>** | ||
- **函数模板: 如 二分查找:binary_search()** | ||
- **函数功能: 函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置** | ||
|
||
### 函数upper_bound() | ||
|
||
- **头文件:#include<algorithm>** | ||
- **函数模板: 如二分查找binary_search()** | ||
- **函数功能:函数upper_bound()返回的在前闭后开区间查找的关键字的上界,返回大于val的第一个元素位置** | ||
|
||
### 总结: | ||
|
||
- **lower_bound(val):返回容器中第一个值【大于或等于】val的元素的iterator位置。** | ||
- **upper_bound(val): 返回容器中第一个值【大于】val的元素的iterator位置。** | ||
|
||
![image-20210910113648424](C:\Users\西安交通大学2193613091sxm\AppData\Roaming\Typora\typora-user-images\image-20210910113648424.png) | ||
|
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
# [6. Z 字形变换]:【代码优化】 | ||
|
||
## 题目描述: | ||
|
||
![image-20210404220719228](C:\Users\西安交通大学2193613091sxm\AppData\Roaming\Typora\typora-user-images\image-20210404220719228.png) | ||
|
||
![image-20210404220831676](C:\Users\西安交通大学2193613091sxm\AppData\Roaming\Typora\typora-user-images\image-20210404220831676.png) | ||
|
||
## 题目分析 | ||
|
||
#### 1:首先肯定是读懂题意 | ||
|
||
#### 2:根据给出的示例考虑各种特殊的情况(分析的严谨性)--此处代码显得冗长 | ||
|
||
```C++ | ||
class Solution { | ||
public: | ||
string convert(string s, int numRows) { | ||
//特殊情况处理 | ||
if(numRows==1)return s; | ||
vector<string>res(numRows,""); | ||
string ans; | ||
//特殊情况处理 | ||
if(numRows==2){ | ||
for(int i=0;i<s.size();i++){ | ||
if(i%2==0)res[0].push_back(s[i]); | ||
else res[1].push_back(s[i]); | ||
} | ||
ans+=res[0]; | ||
ans+=res[1]; | ||
return ans; | ||
} | ||
int flag=0,cout1=0,cout2=numRows-2; | ||
for(int i=0;i<s.size();i++){ | ||
if(!flag){ | ||
res[cout1++].push_back(s[i]); | ||
if(cout1==numRows){ | ||
flag=1; | ||
cout1=0; | ||
} | ||
}else if(flag){ | ||
res[cout2--].push_back(s[i]); | ||
if(!cout2){ | ||
flag=0; | ||
cout2=numRows-2; | ||
} | ||
} | ||
} | ||
|
||
//string ans; | ||
for(int i=0;i<numRows;i++){ | ||
ans+=res[i]; | ||
} | ||
|
||
return ans; | ||
} | ||
}; | ||
``` | ||
|
||
```c++ | ||
class Solution { | ||
public: | ||
string convert(string s, int numRows) { | ||
|
||
if (numRows == 1) return s; | ||
|
||
vector<string> rows(min(numRows, int(s.size())));//长度的考虑 | ||
int curRow = 0; | ||
bool goingDown = false; | ||
|
||
//for循环的推荐表达 | ||
for (char c : s) { | ||
rows[curRow] += c; | ||
if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown; | ||
//两种情况下问好表达式的妙用。(问好表达式的用法主要式再一种判断两种结果的情况下来简化代码) | ||
curRow += goingDown ? 1 : -1; | ||
} | ||
|
||
string ret; | ||
for (string row : rows) ret += row; | ||
return ret; | ||
} | ||
}; | ||
|
||
作者:LeetCode | ||
链接:https://leetcode-cn.com/problems/zigzag-conversion/solution/z-zi-xing-bian-huan-by-leetcode/ | ||
来源:力扣(LeetCode) | ||
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 | ||
``` | ||
|
||
## 感悟:简单的题目要学会用精炼的代码给写出来 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
# 高级代码学习 | ||
|
||
## 2021/4/9:自定义sort算法 | ||
|
||
![image-20210409232835739](C:\Users\西安交通大学2193613091sxm\AppData\Roaming\Typora\typora-user-images\image-20210409232835739.png) | ||
|
||
1. 设置一个bool类型的函数:该函数用来确定两个数的比较方式 | ||
2. 当为一维数组时,a<b表示升序排列;a>b表示降序排列; | ||
3. 当为二维数组时,可以根据实际的需要确定时根据第几列的数据进行排序;以及综合几列数据的情况进行排序; | ||
4. 如下举例: | ||
|
||
```c++ | ||
static bool cmp1(const vector<int> &a, const vector<int> &b){ | ||
if(a[1]==b[1])return a[0]<b[0];//第二列相同的情况下,按照第一列的数据从小到大进行排序 | ||
return a[1] < b[1];//按照第二列从小到大排序 | ||
} | ||
static bool cmp2(const vector<int> &a, const vector<int> &b){ | ||
return a[0] < b[0];//按照第一列从小到大排序 | ||
} | ||
``` | ||
```c++ | ||
//字符串处理大整数的加法 | ||
string add(string& a,string& b){ | ||
int n1=a.size()-1; | ||
int n2=b.size()-1; | ||
int carry=0; | ||
string ans; | ||
while(n1>=0||n2>=0||carry>0){ | ||
int t1=n1>=0?a[n1--]-'0':0; | ||
int t2=n2>=0?b[n2--]-'0':0; | ||
ans+=(t1+t2+carry)%10+'0'; | ||
carry=(t1+t2+carry)>=10?1:0; | ||
} | ||
reverse(ans.begin(),ans.end()); | ||
return ans; | ||
} | ||
}; | ||
作者:Over-Lord | ||
链接:https://leetcode-cn.com/problems/additive-number/solution/xia-biao-zuo-wei-fen-duan-dian-dfs-by-over-lord/ | ||
来源:力扣(LeetCode) | ||
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 | ||
``` | ||
|
||
## 优先队列的自定义sort算法 | ||
|
||
```c++ | ||
struct cmp2{ | ||
bool operator()(vector<int>&a,vector<int>&b) | ||
{ | ||
if(a[0]==b[0])return a[1]>b[1];//小的优先级高,所以是从小到大排序 | ||
else return a[0]>b[0]; | ||
} | ||
}; | ||
struct cmp3{ | ||
bool operator()(vector<int>&a,vector<int>&b) | ||
{ | ||
if(a[1]==b[1])return a[2]>b[2];//小的优先级高,所以是从小到大排序 | ||
else return a[1]>b[1]; | ||
} | ||
}; | ||
|
||
``` | ||
## LONG_MAX, LONG_MIN 分别表示长整型的最大值和最小值 | ||
## 8个方向:数组+加法运算表示方向——使代码更加简洁 | ||
```C++ | ||
int dir_x[8] = {0, 1, 0, -1, 1, 1, -1, -1}; | ||
int dir_y[8] = {1, 0, -1, 0, 1, -1, 1, -1}; | ||
for (int i = 0; i < 8; ++i) { | ||
int tx = x + dir_x[i]; | ||
int ty = y + dir_y[i]; | ||
if (tx < 0 || tx >= board.size() || ty < 0 || ty >= board[0].size()) { | ||
continue; | ||
} | ||
// 不用判断 M,因为如果有 M 的话游戏已经结束了 | ||
cnt += board[tx][ty] == 'M'; | ||
} | ||
作者:LeetCode-Solution | ||
链接:https://leetcode-cn.com/problems/minesweeper/solution/sao-lei-you-xi-by-leetcode-solution/ | ||
来源:力扣(LeetCode) | ||
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。 | ||
``` | ||
|
||
## lambda表达式的使用对运行速度的影响: | ||
|
||
```C++ | ||
sort(pairs.begin(),pairs.end(),[](vector<int>&a,vector<int>&b){return a[1]<b[1];});//运行快 | ||
sort(pairs.begin(),pairs.end(),[&](vector<int>a,vector<int>b){return a[1]<b[1];});//运行慢 | ||
``` | ||
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 困难题:不含连续1的非负整数 | ||
|
||
## 题目描述: | ||
|
||
![image-20210912105157961](C:\Users\西安交通大学2193613091sxm\AppData\Roaming\Typora\typora-user-images\image-20210912105157961.png) | ||
|
||
## 题目分析: | ||
|
||
- **从我的角度来讲:这道题得用动态规划;动态规划的角度大概可以相当,一个变量就是二进制的每一个位,一个变量就是二进制的每一个位上的取值0或者1;** | ||
- **但是这道题有所限制:一是不能保证在任何条件下,每一位都能取到0和1,是由条件限制的;而这样的条件限制体现在我的编码上和思考上就有点困难;** | ||
- **新思路:对于一个角度变量只有两种取值的情况下,可以借助二叉树来分析一下;** | ||
|
||
## 我的代码: | ||
|
||
```C++ | ||
class Solution { | ||
public: | ||
int findIntegers(int n) { | ||
int num=ceil(log(n+1)/log(2)); | ||
vector<vector<int>>dp(num,vector<int>(2,0)); | ||
dp[0][0]=dp[0][1]=1; | ||
for(int i=1;i<num;i++){ | ||
dp[i][0]=dp[i-1][0]+dp[i-1][1]; | ||
dp[i][1]=dp[i-1][0]; | ||
} | ||
int res=0; | ||
for(int i=num-1;i>=0;i--){ | ||
if(n>>i&1==1)res+=dp[i][0]; | ||
} | ||
return res+1; | ||
} | ||
}; | ||
``` | ||
## 二叉树+动态规划+位运算(位运算DP) | ||
dp[t]=dp[t-1]+dp[t-2]的动态转移分析: | ||
https://leetcode-cn.com/problems/non-negative-integers-without-consecutive-ones/solution/bu-han-lian-xu-1de-fei-fu-zheng-shu-by-l-9l86/ | ||
```C++ | ||
class Solution { | ||
public: | ||
int findIntegers(int n) { | ||
//动态规划 | ||
//dp[t]表示高度为t-1,根节点为0的完全二叉树中符合条件的数量 | ||
vector<int>dp(31); | ||
dp[0]=dp[1]=1; | ||
for(int i=2;i<31;i++)dp[i]=dp[i-1]+dp[i-2]; | ||
//逐一数位分析 | ||
int res=0; | ||
int pre=0;//防止出现连续的两个1 | ||
for(int i=29;i>=0;i--){ | ||
int val=1<<i; | ||
if((n&val)!=0){ | ||
//说明i的二进制位的第i位为1 | ||
res+=dp[i+1]; | ||
if(pre==1)break;//当出现连续两个一,终止分析 | ||
pre=1; | ||
}else pre=0; | ||
if(i==0)res+=1; | ||
} | ||
return res; | ||
} | ||
}; | ||
``` | ||
|
Binary file not shown.
Oops, something went wrong.