# 剑指offer第二版题解系列（二）

Java 同时被 2 个专栏收录
7 篇文章 0 订阅
7 篇文章 0 订阅

### Test10 - Test19系列

##### 1. Test10.java
public class Test10{
public static int fibonaci(int n)
{
if(n<=0)
return 0;
if(n==1||n==2)
return 1;
if(n>2)
{
int i = 3;
int prepre = 1;
int pre = 1;
int current = 0;
while(i<=n)
{
current = prepre + pre;
prepre = pre;
pre = current;
i++;
}
return current;
}
return -1;
}
public static void main(String[] args)
{
System.out.println(fibonaci(6));

}
}
##### 2. Test11.java
public class  Test11{
public static int getMinNumber(int[] num){
if(num==null||num.length<1)
return -1;
int st = 0;
int ed = num.length-1;
int mid = 0;
if(num[st]<num[ed])
return num[st];
while(num[st]>=num[ed]) {
if (ed - st == 1)
return num[ed];
mid = (st + ed) / 2;
if(num[st]==num[mid]&&num[mid]==num[ed])
return getMinNumber2(num,0,num.length-1);
if(num[mid]>num[st])
st = mid;
else if(num[mid]<num[ed])
ed = mid;
}
return num[mid];

}
public static int getMinNumber2(int[] num,int a,int b)
{
if(num==null||num.length<1||a<0||b<0||a>b)
return -1;
int min = num[a];
for(int i = a+1;i<=b;++i)
{
if(min>=num[i])
min = num[i];
}
return min;
}
public static void main(String[] args)
{
//int[] a = {3,4,5,1,2};
int[] a = {1,0,1,1,1};
System.out.println(getMinNumber(a));
}
}
##### 3. Test12.java
public class Test12{
public static boolean getPath(char[] matrix,int rows,int cols,String st)
{
if(matrix==null||matrix.length<1||st==null||rows<1||cols<1)
return false;
boolean[] visit = new boolean[matrix.length];
for(int i=0;i<=matrix.length-1;i++)
visit[i] = false;
char[] cst = st.toCharArray();
int num = 0;
int stLength = cst.length-1;
//        int pos = 0;
// boolean hasPath = false;
for(int i = 0;i<rows;i++)
{
for(int j=0;j<cols;j++)

//                if(num==stLength)
//                    return true;
//                else
if(hasPath2(matrix,rows,cols,i,j,num,visit,cst))
return true;
}
return false;
}
public static boolean hasPath2(char[] matrix,int rows,int cols,int row,int col,int num,boolean[] visit,char[] cst) {
if (matrix == null || matrix.length < 1 || row<0 || col<0 || row >= rows || col >= cols || visit == null || visit.length < 1 || cst == null || cst.length < 1 || num > cst.length)
return false;
boolean ispath = false;
if(num == cst.length)
return true;
if (row >= 0 && col >= 0 && row < rows && col < cols && matrix[row * cols + col] == cst[num] && !visit[row * cols + col]) {
num++;
visit[row * cols + col] = true;
ispath = hasPath2(matrix, rows, cols, row + 1, col, num, visit, cst) ||
hasPath2(matrix, rows, cols, row, col + 1, num, visit, cst) ||
hasPath2(matrix, rows, cols, row, col - 1, num, visit, cst) ||
hasPath2(matrix, rows, cols, row - 1, col, num, visit, cst);

if (!ispath) {
num--;
visit[row * cols + col] = false;
}
}
return ispath;
}

public static void main(String[] args)
{
char[] matrix = {'a','b','t','g','c','f','c','s','j','d','e','h'};
String s = "bfce";
System.out.println(getPath(matrix,3,4,s));
}
}
##### 4. Test13.java
public class  Test13{
public static int numsum(int rows,int cols,int k)
{
if(k<0||rows<=0||cols<=0)
return 0;
boolean[] visit = new boolean[rows*cols];
for(int i=0;i<rows*cols-1;i++)
visit[i] = false;
return numsum1(rows,cols,0,0,visit,k);
}
public static int numsum1(int rows,int cols,int row,int col,boolean[] visit,int k)
{
if(k<0||row<0||row>=rows||col>=cols||col<0)
return 0;
int count = 0;
if(check(rows,cols,row,col,visit,k)) {
visit[row * cols + col] = true;
count = 1 +
numsum1(rows, cols, row - 1, col, visit, k) +
numsum1(rows, cols, row, col - 1, visit, k) +
numsum1(rows, cols, row + 1, col, visit, k) +
numsum1(rows, cols, row, col + 1, visit, k);
}
return count;
}
public static boolean check(int rows,int cols,int row,int col,boolean[] visit,int k)
{
if(k<0||row<0||row>=rows||col>=cols||col<0)
return false;
if((getsum(row)+getsum(col)<=k)&&!visit[row*cols+col]) {
return true;
}
return false;
}
public static int getsum(int num)
{
int sum = 0;
while(num>0) {
sum += (num % 10);
num = num/10;
}
return sum;

}
public static void main(String[] args)
{
System.out.println(numsum(5,5,5));
}
}
##### 5. Test14.java
public class Test14{
public static int maxChengji(int length)
{
if(length<=1)
return 0;
if(length==2)
return 1;
if(length==3)
return 2;
int[] f = new int[length+1];
f[0] = 0;
f[1] = 1;
f[2] = 2;
f[3] = 3;
int max = 0;
for(int i=4;i<=length;++i)
{
for(int j=1;j<=i/2;j++)
{
if(max < f[j]*f[i-j])
max=f[j]*f[i-j];
}
f[i] = max;
}
return f[length];
}
public static void main(String[] args)
{
System.out.println(maxChengji(10));
}
}
##### 6. Test14_2.java
public class Test14_2{
public static int maxChengji(int length)
{
if(length<=1)
return 0;
if(length==2)
return 1;
if(length==3)
return 2;
int[] f = new int[length+1];
f[0] = 0;
f[1] = 0;
f[2] = 1;
f[3] = 2;
f[4] = 4;
int sum = 1;
while(length>=5)
{
sum *= 3;
length = length-3;
}
return sum*f[length];
}
public static void main(String[] args)
{
System.out.println(maxChengji(10));
}
}
##### 7. Test15.java
/**
* 统计一个数字的二进制位中1的个数：
* 将该整数-1，然后与该整数相与，可以将整数二进制位中最右边的1去除
*/
public class Test15{
public static int count1(int num)
{
int count =0;
while(num)
{
count++;
num = (num-1)&num;
}
return count;
}
}

##### 8. Test16.java
public class Test16{
public static double getcount(double num,int exp)
{
if(num==0.0)
return -1;
if(num==1.0)
return 1.0;
if(exp==0)
return 1.0;
int n = exp>0?exp:-exp;
double result = 1.0;
if(n!=0)
{
result = getcount(num,n>>1);
result *= result;
}
if((n&1)==1)
result *= num;
result=exp>0?result:1/result;
return result;
}
public static void main(String[] args)
{
System.out.println(getcount(5,-2));
}
}
##### 9. Test17.java
public class Test17{
public static void printnum(int n)
{
if(n<=0)
return;
int[] num = new int[n];
for(int i=0;i<n;i++)
{
num[i] = 0;
}
while(!increasenum(num))
printnum2(num);
return;
}
public static boolean increasenum(int[] num)
{
if(num==null||num.length<1)
return false;
boolean isflow = false;
int isjw = 0;
int numlength = num.length;
int sum = 0;
for(int i=numlength-1;i>=0;--i) {
sum = num[i] + isjw;
if (i == numlength - 1) {
sum++;
}
if (sum >= 10) {
if (i == 0)
isflow = true;
else {
sum -= 10;
isjw = 1;
num[i] = sum;
}
}
num[i]= sum;
}
return isflow;
}
public static void printnum2(int[] num)
{
if(num==null||num.length<=0)
return;
boolean isBeginning0 = true;
for(int i=0;i<num.length;i++)
{
if(isBeginning0&&num[i]!=0)
isBeginning0 =false;
if(!isBeginning0)
System.out.print(num[i]);
}
System.out.println();

}
public static void main(String[] args)
{
printnum(2);
}

}
##### 10. Test18.java
public class Test18{
public static class LNode{
int data;
LNode next;
public LNode(int data)
{
this.data = data;
}
}
public static LNode deleteNode(LNode head,LNode p)
{
return null;
{
}
if(p.next == null)
{
while(ptr.next!=p)
{
ptr=ptr.next;
}
ptr.next = p.next;
}
else
{
p.data = p.next.data;
p.next=p.next.next;
}

}
{
return;
while(ptr!=null)
{
System.out.println(ptr.data);
ptr = ptr.next;
}
}
public static void main(String[] args)
{
LNode n1 = new LNode(1);
LNode n2 = new LNode(2);
LNode n3 = new LNode(3);
LNode n4 = new LNode(4);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = null;

}
}
##### 11. Test18_2.java
public class Test18_2{
public static class LNode{
int data;
LNode next;
public LNode(int data){
this.data = data;
}
}
{
return null;
LNode first = new LNode(-1);
LNode last =  first;
while(p.next!=null)
{
if(p.data==p.next.data)
{
int val =p.data;
while(p!=null&&p.data==val)
{
p=p.next;
last.next = p;
}
}
else{
last = p;
p=p.next;
}
}
return first.next;
}
{
return;
while(ptr!=null)
{
System.out.println(ptr.data);
ptr = ptr.next;
}
}
public static void main(String[] args)
{
LNode n1 = new LNode(1);
LNode n2 = new LNode(2);
LNode n3 = new LNode(2);
LNode n4 = new LNode(4);
n1.next = n2;
n2.next = n3;
n3.next = n4;
n4.next = null;

}

}
##### 12. Test19.java
public class Test19{
public static boolean match(char[] str,char[] zzchar){
if(str==null||str.length<1||zzchar==null|| zzchar.length<1)
return false;
int i=0;
int j=0;
return match1(str,i,zzchar,j);
}
public static boolean match1(char[] str,int st,char[] zzchar,int zst)
{

if(st==str.length&&zst==zzchar.length)
return true;
if(st<str.length&&zst==zzchar.length)
return false;
if(zst<zzchar.length-1&&zzchar[zst+1]=='*')
{
if(str[st]==zzchar[zst]&&st<str.length||zzchar[zst]=='.'&&st<str.length)
return match1(str,st,zzchar,zst+2)|| match1(str,st+1,zzchar,zst+2)|| match1(str,st+1,zzchar,zst);
else
return match1(str,st,zzchar,zst+2);
}
else {
if(str[st]==zzchar[zst]&&st<str.length||zzchar[zst]=='.'&&st<str.length)
{
return match1(str,st+1,zzchar,zst+1);
}
else
return  false;
}
}
public static void main(String[] args)
{
char[] str = {'a','a','a'};
char[] zzchar = {'a','a','a','*'};
System.out.println(match(str,zzchar));
}
}
• 0
点赞
• 0
评论
• 0
收藏
• 一键三连
• 扫一扫，分享海报

08-22 677

08-16 5万+
08-22 99
08-22 57
07-15 1614
08-22 75
08-22 71
12-13 5245
02-02 4521
01-05 4633
08-22 95
08-16 491

sfiayui123

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。