lc 61~70
单链表
class Solution {
public:
ListNode* rotateRight(ListNode* head, int k) {
if(!head) return head;
int n = 0;
ListNode *tail;
for(auto p = head; p; p = p->next){
tail = p;
n++;
}
k %= n;
if(!k) return head;
auto p = head;
for(int i = 0; i < n - k - 1; i++) p = p->next;
tail->next = head;
head = p->next;
p->next = nullptr;
return head;
}
};
DP
class Solution {
public:
int uniquePaths(int m, int n) {
vector<vector<int>> dp(m, vector<int>(n));
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++){
if(!i && !j) dp[i][j] = 1;
else{
if(i) dp[i][j] += dp[i - 1][j];
if(j) dp[i][j] += dp[i][j - 1];
}
}
return dp[m - 1][n - 1];
}
};
组合数公式(个人模板,三个)
class Solution {
public:
int uniquePaths(int m, int n) {
int x = m - 1 + n - 1, y = min(m - 1, n - 1);
return C(x, y);
}
int C(int x, int y){
double res = 1;
for(long long i = x, u = 1; u <= y; i--, u++)
res = res * i / u;
return (int)(res + 0.5);
}
};
DP
class Solution {
public:
int uniquePathsWithObstacles(vector<vector<int>>& o) {
int n = o.size(), m = o[0].size();
vector<vector<int>> dp(n, vector<int>(m));
for(int i = 0; i < n; i++)
for(int j = 0; j < m; j++){
if(!i && !j) dp[0][0] = 1 - o[0][0];
else{
if(o[i][j]) dp[i][j] = 0;
else{
if(i) dp[i][j] += dp[i - 1][j];
if(j) dp[i][j] += dp[i][j - 1];
}
}
}
return dp[n - 1][m - 1];
}
};
DP
class Solution {
public:
int minPathSum(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
vector<vector<int>> dp(m, vector<int>(n, INT_MAX));
for(int i = 0; i < m; i++)
for(int j = 0; j < n; j++){
if(!i && !j) dp[i][j] = grid[i][j];
else{
if(i) dp[i][j] = min(dp[i][j], dp[i - 1][j] + grid[i][j]);
if(j) dp[i][j] = min(dp[i][j], dp[i][j - 1] + grid[i][j]);
}
}
return dp[m - 1][n - 1];
}
};
模拟(很麻烦)
class Solution {
public:
bool isNumber(string s) {
int l = 0, r = s.size() - 1;
while(l <= r && s[l] == ' ') l ++;
while(l <= r && s[r] == ' ') r --;
if(l > r) return false;
s = s.substr(l, r - l + 1);
if(s[0] == '+' || s[0] == '-') s = s.substr(1);
if(s.empty()) return false;
if(s[0] == '.' && (1 == s.size() || s[1] == 'e' || s[1] == 'E')) return false;
int dot = 0, e = 0;
for(int i = 0; i < s.size(); i++){
if(s[i] == '.'){
dot++;
if(dot > 1 || e) return false;
}else if(s[i] == 'e' || s[i] == 'E'){
e++;
if(!i || e > 1 || i + 1 == s.size()) return false;
if(s[i + 1] == '+' || s[i + 1] == '-'){
if(i + 2 == s.size()) return false;
else i++;
}
}else if(s[i] > '9' || s[i] < '0') return false;
}
return true;
}
};
简单,注意最后有进位用insert插入
class Solution {
public:
vector<int> plusOne(vector<int>& digits) {
int t = 1;
for(int i = digits.size() - 1; i >= 0; i--){
t += digits[i];
digits[i] = t % 10;
t /= 10;
}
if(t) digits.insert(digits.begin(), t);
return digits;
}
};
先翻转,求出结果再翻转
class Solution {
public:
string addBinary(string a, string b) {
string ans;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int t = 0;
for(int i = 0; i < a.size() || i < b.size(); i++){
if(i < a.size()) t += a[i] - '0';
if(i < b.size()) t += b[i] - '0';
ans += to_string(t % 2);
t /= 2;
}
if(t) ans += to_string(t);
reverse(ans.begin(), ans.end());
return ans;
}
};
模拟,关键是把题意弄清楚
class Solution {
public:
vector<string> fullJustify(vector<string>& words, int maxWidth) {
vector<string> ans;
int size = words.size();
for(int i = 0; i < size; ){
string line = words[i];
int k = i + 1, len = line.size();
while(k < size && len + 1 + words[k].size() <= maxWidth)
len += 1 + words[k++].size();
if(k >= size || k == i + 1){
for(int j = i + 1; j < k; j++)
line += ' ' + words[j];
while(line.size() < maxWidth) line += ' ';
}else{
int cnt = k - i - 1, r = maxWidth - len + cnt;
int ii = 0;
for(; ii < r % cnt; ii++)
line += string(r / cnt + 1, ' ') + words[i + ii + 1];
while(ii < cnt) line += string(r / cnt, ' ') + words[i + ii + 1], ii++;
}
ans.push_back(line);
i = k;
}
return ans;
}
};
简单小数二分
class Solution {
public:
int mySqrt(int x) {
double l = 0, r = x;
while(r - l > 1e-10){
double mid = (l + r) / 2;
if(mid * mid >= x) r = mid;
else l = mid;
}
return (int)r;
}
};
巧用二分模板
class Solution {
public:
int mySqrt(int x) {
int l = 0, r = x;
while(l < r){
int mid = l + 1ll + r >> 1;
if(mid <= x / mid) l = mid;
else r = mid - 1;
}
return l;
}
};
DP
class Solution {
public:
int climbStairs(int n) {
vector<int> dp(n + 1);
dp[0] = 1, dp[1] = 1;
for(int i = 2; i <= n; i++)
dp[i] = dp[i - 1] + dp[i - 2];
return dp[n];
}
};
滚动数组
class Solution {
public:
int climbStairs(int n) {
int a = 1, b = 1;
while(--n){
int c = a + b;
a = b, b = c;
}
return b;
}
};
|