//java版classSolution{
publicintminArray(int[] nums){
int n = nums.length -1;if(n <0)return-1;while(n >0&& nums[n]== nums[0]) n --;if(nums[n]>= nums[0])return nums[0];int l =0, r = n;while(l < r){
int mid = l + r >>1;//[l, mid] [mid + 1, r]if(nums[mid]< nums[0]) r = mid;else l = mid +1;}return nums[l];}}
//C++版classSolution{
public:intminNumberInRotateArray(vector<int> nums){
int n = nums.size()-1;if(n <0)return0;while(nums[n]== nums[0]&& n >0) n --;if(nums[n]>= nums[0])return nums[0];int l =0, r = n;while(l < r){
int mid = l + r >>1;//[l, mid] [mid+1, r]if(nums[mid]< nums[0]) r = mid;else l = mid +1;}return nums[r];}};
12.矩阵中的路径
//java版classSolution{
//回溯法 dfspublicbooleanexist(char[][] board, String word){
for(int i =0; i < board.length; i ++)for(int j =0; j < board[0].length; j ++)if(dfs(board, word,0, i, j))returntrue;returnfalse;}privatebooleandfs(char[][] board, String word,int u,int x,int y){
//先判断边界 后比较相等if(x >= board.length || x <0|| y >= board[0].length || y <0|| board[x][y]!= word.charAt(u))returnfalse;if(u == word.length()-1)returntrue;char temp = board[x][y];
board[x][y]='*';//递归遍历boolean res =dfs(board, word, u +1, x -1, y)||dfs(board, word, u +1, x +1, y)||dfs(board, word, u +1,x, y -1)||dfs(board, word, u +1, x, y +1);
board[x][y]= temp;return res;}}
//C++版classSolution{
public:boolhasPath(char* matrix,int rows,int cols,char* str){
if(rows ==1&& cols ==1)if(matrix[0]== str[0])returntrue;elsereturnfalse;//matrix //str str \0for(int i =0; i < rows; i ++)for(int j =0; j < cols; j ++)if(dfs(matrix, rows, cols, str,0, i, j))returntrue;returnfalse;}booldfs(char* matrix,int rows,int cols,char* str,int u,int x,int y){
if(str[u]=='\0')returntrue;int dx[4]={
-1,0,1,0}, dy[4]={
0,1,0,-1};for(int i =0; i <4; i ++){
int a = x + dx[i], b = y + dy[i];if(a >=0&& a < rows && b >=0&& b < cols && matrix[a * cols + b]== str[u]){
char t = matrix[a * cols + b];
matrix[a * cols + b]='*';if(dfs(matrix, rows, cols, str, u +1, a, b))returntrue;
matrix[a * cols + b]= t;}}returnfalse;}};
13.机器人的运动范围
//java版classSolution{
int m, n, k;boolean[][] visited;publicintmovingCount(int m1,int n1,int k1){
//找规律//19 20 小8//39 40 小8//x9 x+1 0 小8//普通情况 12 13 +1
m = m1;
n = n1;
k = k1;
visited =newboolean[m][n];//falsereturndfs(0,0,0,0);}privateintdfs(int x,int y,int sx,int sy){
//sx sy对应 x y数位之和// x 16 y 20 sx 7 sy 2//边界优先 m- 1 n -1if(x >= m || y >= n || k < sx + sy || visited[x][y])return0;
visited[x][y]=true;//sx + sy <= kreturn1+dfs(x +1, y,(x +1)%10!=0 sx +1: sx -8, sy)+dfs(x, y +1, sx,(y +1)%10!=0 sy +1: sy -8);}}
//C++版classSolution{
public:intget_sum(pair<int,int> p){
int s =0;while(p.first){
s += p.first %10;
p.first /=10;}while(p.second){
s += p.second %10;
p.second /=10;}return s;}intmovingCount(int threshold,int rows,int cols){
int res =0;if(!cols ||!rows)return0;
queue<pair<int,int>> q;
vector<vector<bool>>st(rows, vector<bool>(cols,false));//bfsint dx[4]={
-1,0,1,0}, dy[4]={
0,1,0,-1};
q.push({
0,0});//bfswhile(q.size()){
pair<int,int> t = q.front();
q.pop();if(st[t.first][t.second]||get_sum(t)> threshold)continue;
res ++;
st[t.first][t.second]=true;for(int i =0; i <4; i ++){
int x = t.first + dx[i], y = t.second + dy[i];if(x >=0&& x< rows && y >=0&& y < cols) q.push({
x, y});}}return res;}};
//C++版classSolution{
public:intcutRope(int n){
//数学//子绳子// >=5//ni 3 * (ni - 3) = 3*ni - 9 > ni; 2*ni > 9// == 4 // 2 2 1 3// 3 > 1 *2// 2 > 1*1/*
if(n <= 3) return n - 1;
int res = 1;
if(n % 3 == 1) res *= 4, n -= 4;
else if(n % 3 == 2) res *= 2, n -= 2;
while(n)
{
res *= 3;
n -= 3;
}
return res;
*///动态规划if(n <=3)return n -1;int dp[n];
dp[1]=1;
dp[2]=2;
dp[3]=3;int res =0;//记录最大的乘积for(int i =4; i <= n ; i ++){
for(int j =1; j <= i/2; j ++)
res =max(res, dp[j]* dp[i - j]);
dp[i]= res;}return dp[n];}};
15.二进制中1的个数
//java版publicclassSolution{
// you need to treat n as an unsigned valuepublicinthammingWeight(int n){
//n & (n - 1)//每执行一次 最右边的1变成0//unsigned int int count =0;while(n !=0){
count ++;
n = n &(n -1);}return count;}}
//C++版classSolution{
public:intNumberOf1(int n){
int count =0;while(n){
count ++;
n = n &(n -1);}return count;}};
16.数值的整数次方
//java版classSolution{
publicdoublemyPow(double x,int n){
//快速幂//n用二进制表示if(x ==0)return0;long b = n;// 2147483648,2147483647double res =1.0;if(b <0){
b =-b;//用long防止溢出
x =1/ x;}//快速幂while(b >0){
if((b &1)==1) res *= x;//累乘
x *= x;//2的次方
b >>=1;}return res;}}
//C++版classSolution{
public:doublePower(double base,int e){
double res =1;for(int i =0; i <abs(e); i ++) res *= base;if(e <0) res =1/ res;return res;}};
17.打印从1到最大的n位数
//java版classSolution{
publicint[]printNumbers(int n){
if(n <=0)returnnewint[0];int result =1;int x =10;//该题考查快速幂;while(n !=0){
if((n &1)==1){
result *= x;}
n >>=1;
x *= x;}int len = result -1;int[]array =newint[len];for(int i =0;i < len;i++){
array[i]= i +1;}return array;}}
//C++版classSolution{
public:
vector<int>printNumbers(int n){
char*number =newchar[n+1];
vector<int> ans;memset(number,'0',n);
number[n]='\0';while(!checkNumber(number,n)){
ans.push_back(changeNumber(number,n));}return ans;}intchangeNumber(char*number,int n){
int res =0;for(int i =0;i < n;++i){
res = res*10+ number[i]-'0';}return res;}boolcheckNumber(char*number,int n){
int nLength = n;bool isOverFlow =0;int isTakeOver=1;for(int i = nLength -1;i >=0;--i){
number[i]= number[i]+ isTakeOver;if(number[i]>'9'){
if(i ==0){
isOverFlow =1;break;}
number[i]='0';
isTakeOver =1;}else{
isTakeOver =0;}}return isOverFlow;}};
18.删除链表的节点
//java版classSolution{
public ListNode deleteNode(ListNode head,int val){
//前一个结点 指向 当前结点的下一个//则当前结点被删除//删除第一个结点 单独讨论//if(head.val == val) return head.next;
ListNode dummy =newListNode(0);//虚拟头结点
dummy.next = head;
ListNode pre = dummy;
ListNode cur = dummy.next;//找到val结点while(cur.val != val && cur != null){
pre = cur;
cur = cur.next;}//找到val 或者走到头了if(cur != null){
pre.next = cur.next;}return dummy.next;}}
//C++版classSolution{
public:
ListNode*deleteDuplication(ListNode* head){
auto dummy =newListNode(-1);
dummy -> next = head;auto p = dummy;while(p -> next){
auto q = p -> next;while(q && p -> next -> val == q -> val) q = q -> next;if(p -> next -> next == q) p = p -> next;else p -> next = q;}return dummy -> next;}};
19.正则表达式匹配
//java版classSolution{
publicbooleanisMatch(String s, String p){
//dp思路//f[i][j] s的前i个字符 和 p 的前j个字符 匹配int n = s.length();int m = p.length();boolean[][] f =newboolean[n +1][m +1];for(int i =0; i <= n; i ++)for(int j =0; j <= m ; j ++){
if(j ==0)// i == j == 0
f[i][j]=(i ==0);else{
//* if(p.charAt(j -1)!='*'){
//没有碰到*if(i >0&&(s.charAt(i -1)== p.charAt(j -1)|| p.charAt(j -1)=='.'))
f[i][j]= f[i -1][j -1];}else{
//碰到*//* = 0if(j >=2)
f[i][j]|= f[i][j -2];//* != 0if(i >=1&& j >=2&&(s.charAt(i -1)== p.charAt(j -2)|| p.charAt(j -2)=='.'))
f[i][j]|= f[i -1][j];}}}return f[n][m];}}