/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode*rotateRight(ListNode* head,int k){if(!head)return head;
ListNode *now = head,*last = head,*pre = head;int len =0;while(now) len ++, last = now, now = now->next;
k %= len;
k = len - k;if(!k || k == len)return head;
now = head;while(k --) pre = now, now = now->next;
last->next = head;
pre->next = nullptr;return now;}};
62. 不同路径
class Solution {
public:intuniquePaths(int m,int n){
vector<vector<int>>f(m, vector<int>(n,0));for(int i =0; i < m;++ i) f[i][0]=1;for(int i =0; i < n;++ i) f[0][i]=1;for(int i =1; i < m;++ i)for(int j =1; j < n;++ j)
f[i][j]+= f[i -1][j]+ f[i][j -1];return f[m -1][n -1];}};
63. 不同路径 II
class Solution {
public:intuniquePathsWithObstacles(vector<vector<int>>& o){int n = o.size(), m = o.back().size();
vector<vector<int>>f(n, vector<int>(m,0));for(int i =0; i < n &&(o[i][0]==0);++ i) f[i][0]=1;for(int i =0; i < m &&(o[0][i]==0);++ i) f[0][i]=1;for(int i =1; i < n;++ i)for(int j =1; j < m;++ j){if(o[i][j])continue;if(!o[i -1][j]) f[i][j]+= f[i -1][j];if(!o[i][j -1]) f[i][j]+= f[i][j -1];}return f[n -1][m -1];}};
64. 最小路径和
class Solution {
public:intminPathSum(vector<vector<int>>& grid){int n = grid.size(), m = grid.back().size();
vector<vector<int>>f(n, vector<int>(m,0));
f[0][0]= grid[0][0];for(int i =1; i < n;++ i) f[i][0]= f[i -1][0]+ grid[i][0];for(int i =1; i < m;++ i) f[0][i]= f[0][i -1]+ grid[0][i];for(int i =1; i < n;++ i)for(int j =1; j < m;++ j)
f[i][j]=min(f[i -1][j], f[i][j -1])+ grid[i][j];return f[n -1][m -1];}};
65. 有效数字
class Solution {
public:
bool isNumber(string s){auto isInt =[=](string s)-> bool {int st =0;if(s[st]=='+'|| s[st]=='-') st ++;for(; st < s.size();++ st)if(s[st]<'0'|| s[st]>'9')return false;return true;};auto check =[](string b)-> bool{if(b.size()!=1)return true;if(b.back()=='+'|| b.back()=='-')return false;return true;};auto isFol =[=](string s)-> bool {int st = s.find('.');if(st == s.npos)return false;if(s.size()==1)return false;
string a = s.substr(0, st);if(st &&!isInt(a))return false;if(st +1!= s.size()){
string tmp = s.substr(st +1);
tmp ="+"+ tmp;returnisInt(tmp);}int flag =0;for(int i =0; i < s.size();++ i)
flag +=(s[i]>='0'&& s[i]<='9');return flag !=0;};transform(s.begin(),s.end(),s.begin(),::tolower);int st = s.find('e');if(st == s.npos)returnisInt(s)||isFol(s);else{if(st ==0|| st == s.size()-1)return false;
string a = s.substr(0, st);
string b = s.substr(st +1);return(isFol(a)||isInt(a))&&isInt(b)&&check(b)&&check(a);}}};
66. 加一
class Solution {
public:
vector<int>plusOne(vector<int>& digits){reverse(digits.begin(), digits.end());int t =1;for(int i =0; i < digits.size();++ i){
t += digits[i];
digits[i]= t %10;
t /=10;}if(t) digits.push_back(t);reverse(digits.begin(), digits.end());return digits;}};
67. 二进制求和
class Solution {
public:
string addBinary(string a, string b){
string ans;reverse(begin(a),end(a));reverse(begin(b),end(b));int t =0;for(int i =0; i <max(a.size(), b.size());++ i){if(i < a.size()) t +=(a[i]=='1');if(i < b.size()) t +=(b[i]=='1');
ans.push_back((t %2)+'0');
t /=2;}if(t) ans.push_back('1');reverse(begin(ans),end(ans));return ans;}};
68. 文本左右对齐
class Solution {
public:
vector<string>fullJustify(vector<string>& words,int maxWidth){
vector<string> ans;
vector<string> temp;int cur =0;for(int i =0; i < words.size();++ i){if(maxWidth >= cur + temp.size()+ words[i].size()){
temp.push_back(words[i]);
cur += words[i].size();}else{
string s;if(temp.size()>1){int space = maxWidth -(cur + temp.size()-1);int siz = space /(temp.size()-1);int to = space %(temp.size()-1);for(int i =0; i < temp.size();++ i){if(i){
s = s +string(1+ siz +(!!to),' ')+ temp[i];if(to) to --;}else s = temp[i];}}else s = temp[0];if(s.size()< maxWidth) s +=string(maxWidth - s.size(),' ');
ans.push_back(s);
vector<string>().swap(temp);
temp.push_back(words[i]);
cur = words[i].size();}}
string s;for(int i =0; i < temp.size();++ i)if(i) s +=" "+ temp[i];else s += temp[i];if(s.size()< maxWidth) s +=string(maxWidth - s.size(),' ');
ans.push_back(s);return ans;}};
69. x 的平方根
class Solution {
public:intmySqrt(int x){longlong l =0, r = x;while(l < r){longlong mid = l + r +1>>1;if(mid * mid <= x) l = mid;else r = mid -1;}return l;}};
70. 爬楼梯
class Solution {
public:intclimbStairs(int n){
vector f(n +1,0);
f[0]=1;f[1]=1;for(int i =2; i <= n;++ i) f[i]= f[i -1]+ f[i -2];return f[n];}};