剑指offer第二版题解系列(二)


写在开头:正值毕业生求职期间,翻一翻剑指offer的书籍,将其中的题目进行分析和代码编写,并将代码整理到csdn博客上,如果有错误,敬请指出!(此系列不提供题目原文,请参考书籍《剑指offer第二版》即可。)


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)
    {
        if(head==null||p==null)
            return null;
        LNode ptr = head;
        if(p==head)
        {
            head = head.next;
            return head;
        }
        if(p.next == null)
        {
            while(ptr.next!=p)
            {
                ptr=ptr.next;
            }
            ptr.next = p.next;
            return head;
        }
        else
        {
            p.data = p.next.data;
            p.next=p.next.next;
        }
        return head;

    }
    public static void print(LNode head)
    {
        if(head==null)
            return;
        LNode ptr = head;
        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;
        LNode head = n1;
        print(deleteNode(head,n4));

    }
}
11. Test18_2.java
public class Test18_2{
    public static class LNode{
        int data;
        LNode next;
        public LNode(int data){
            this.data = data;
        }
    }
    public static LNode deletedulnode(LNode head)
    {
        if(head==null)
            return null;
        LNode first = new LNode(-1);
        first.next=head;
        LNode last =  first;
        LNode p = head;
        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;
    }
    public static void print(LNode head)
    {
        if(head==null)
            return;
        LNode ptr = head;
        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;
        LNode head = n1;
        print(deletedulnode(head));

    }

}
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));
    }
}
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下 4载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合;、下载 4使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
学校大创竞赛管理系统,学生上报项目内容,学院、教务处、评审专家评审。SpringBoot、SpringCloud、SpringSecurity、redis、Mysql、swagger、fastdfs、maven、vue、webpack.zip项目工程资源经过严格测试可直接运行成功且功能正常的情况才上传,可轻松复刻,拿到资料包后可轻松复现出一样的项目,本人系统开发经验充足(全领域),有任何使用问题欢迎随时与我联系,我会及时为您解惑,提供帮助。 【资源内容】:包含完整源码+工程文件+说明(如有)等。答辩评审平均分达到96分,放心下载使用!可轻松复现,设计报告也可借鉴此项目,该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的。 【提供帮助】:有任何使用问题欢迎随时与我联系,我会及时解答解惑,提供帮助 【附带帮助】:若还需要相关开发工具、学习资料等,我会提供帮助,提供资料,鼓励学习进步 【项目价值】:可用在相关项目设计中,皆可应用在项目、毕业设计、课程设计、期末/期中/大作业、工程实训、大创等学科竞赛比赛、初期项目立项、学习/练手等方面,可借鉴此优质项目实现复刻,设计报告也可借鉴此项目,也可基于此项目来扩展开发出更多功能 下载后请首先打开README文件(如有),项目工程可直接复现复刻,如果基础还行,也可在此程序基础上进行修改,以实现其它功能。供开源学习/技术交流/学习参考,勿用于商业用途。质量优质,放心下载使用。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值