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

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

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


Test40 - Test49系列

1. Test40.java
两种解法
【解法一】 利用39给出的方法,使用partition函数计算出索引值为K-1的点,其左边的数包括本身即为所求
【解法二】利用大顶堆
2. Test42.java
public class Test42{
    static  boolean  InvaildInput = false;
    public static int maxsubarray(int[] arr)
    {
        if(arr==null||arr.length<1){
            InvaildInput = true;
            return 0;
        }
        int cursum = 0;
        int presum = 0x80000000;
        for(int i=0;i<arr.length;i++)
        {   if(cursum<=0)
             {
               cursum = arr[i];
            }
            else
             {
                cursum +=arr[i];
             }
            if(cursum>presum)
                presum = cursum;
        }
        return presum;
    }
    public static void main(String[] args)
    {
        int[] arr = {1,-2,3,10,-4,7,2,-5};
        System.out.println(maxsubarray(arr));
    }
}
3. Test43.java
public class Test43{
    public static int count1(int n)
    {
        if(n<0)
            return 0;
        String s  = n+"";
        char[] num = new char[s.length()];
        for(int i=0;i<s.length();i++)
        {
            num[i] = s.charAt(i);
        }
        return count1(num,0);
    }
    public static int count1(char[] num,int start)
    {
        if(num==null||num.length<1||start>num.length-1||num[start]>'9'||num[start]<'0')
            return 0;
        int first = num[start]-'0';
        int numfirst = 0;
        int numnext = 0;
        int length = num.length-start;
        if(length==1&&num[start]=='0')
            return 0;
        if(length==1&&num[start]<='9')
            return 1;
        if(first>1)
        {
            numfirst = count10(num,length-1);
            System.out.println(numfirst);
        }
        else if(first==1)
        {
            numfirst = countfirst(num,start+1)+1;
            System.out.println(countfirst(num,start+1));
        }
        numnext = first*(length-1)*count10(num,length-2);
        int count = count1(num,start+1);
        return numfirst+numnext+count;
    }
    public static int count10(char[] num,int length)
    {
        int sum = 1;
        for(int i=0;i<length;i++)
        {
            sum *= 10;
        }
        return sum;
    }
    public static int countfirst(char[] num,int start)
    {
        int sum = 0;
        for(int i=start;i<num.length;i++)
        {
            sum = sum*10+(num[i]-'0');
        }
        return sum;
    }
//    public static int countnext(char[] num,int start)
//    {
//        int count = num.length-1-start;
//        int sum  = 1;
//        for(int i=start+1;i<=count-1;i++)
//        {
//            sum *= 10;
//        }
//        return sum;
//    }
    public static void main(String[] args)
    {
        int n = 202;
        System.out.println(count1(n));
    }
}
4. Test44.java
public class Test44{
    public static int numnnum(int n){
        if(n<0)
            return -1;
        if(n>1&&n<10)
            return n;
        int presum = 10;
        int prepresum =0;
        int pos = 2;
        while(presum<n)
        {
            prepresum = presum;
            presum +=pos*countpossum(pos);
            pos++;
        }
        int posnum = n-prepresum;
        System.out.println(pos);
        return numnnum1(pos-1,posnum);
    }
    public static int countpossum(int pos)
    {
        int k =9;
        for(int i=1;i<pos;i++)
            k *=10;
        return k;
    }
    public static int numnnum1(int pos,int posnum)
    {
        int num10=1;
        for(int i=1;i<pos;i++)
            num10 *=10;
        int num = posnum/pos;
        int y = posnum-(num*pos);
        int nums = num+num10;
        int ws =0;
        for(int i=0;i<=y;i++)
        {
            if(num10!=0) {
                ws = nums / num10;
                nums = nums - ws * num10;
                num10 = num10 / 10;
            }

        }
        return ws;
    }
    public static void main(String[] args)
    {
        int n=1001;
        System.out.println(numnnum(n));
    }
}
5. Test45.java
import java.util.Comparator;
import java.lang.*;
public class Test45{
    public static class mycomparator implements Comparator<String>{
        public  int  compare(String o1,String o2)
        {
            if(o1==null||o2==null)
                return -1;
            String s1 = o1+o2;
            String s2 = o2+o1;
            return s1.compareTo(s2);
        }
    }
    public static void quicksort(String[] str,int start,int end)
    {
        if(str==null||str.length<1||start<0||end>str.length||start>end)
            return;
        String temp = str[start];
        System.out.println(temp);
        int left = start;
        int right = end;
        mycomparator mc = new mycomparator();
        if(start<end) {
            while (start < end) {
                while (start < end) {
                    if (mc.compare(temp, str[end])<0)
                        end--;
                }
                System.out.println(temp);
                str[start] = str[end];
                start++;
                while (start < end) {
                    if (mc.compare(str[start], temp)<0)
                        start++;
                }
                str[end] = str[start];
                end--;
            }
        }
            str[start] = temp;
        System.out.println(temp);
            quicksort(str, left, start - 1);
            quicksort(str, start + 1, right);
    }
    public static String print(String[] str)
    {
        if(str==null||str.length<1)
            return null;
        StringBuilder sb = new StringBuilder(256);
        quicksort(str,0,str.length-1);
        for(int i=0;i<str.length;i++)
        {
            sb.append(str[i]);
        }
        return sb.toString();
    }
    public static void main(String[] args)
    {
        String[] str = {"3","1","4"};
        System.out.println(print(str));
    }
}
6. Test46.java
public class Test46{
    public static int translatechar(int num)
    {
        if(num<0)
            return 0;
//        if(num>=0&&num<=9)
//            return 1;
//        if(num>=10&&num<=25)
//            return 2;
        String s = num+"";
        System.out.println(s);
        char[] arr = s.toCharArray();
        System.out.println(arr);
        int count = translatechar(arr,arr.length-1);
        System.out.println(count);
        return count;

    }
    public static int translatechar(char[] arr,int end) {
        int count =0;
        if (arr == null || arr.length < 1 || end < 0 || end > arr.length)
            return 0;
        if (end == 0 && arr[end] >= '0' && arr[end] <= '9')
            return 1;
//        else if (end == 0 && arr[end]< '0' && arr[end]>'9')
//            return 0;
        if (end - 1 >= 0) {
            if (arr[end] >= '0' && arr[end] <= '9' && arr[end - 1] >= '0' && arr[end - 1] <= '9') {
                int n = arr[end - 1] - '0';
                int m = arr[end] - '0';
                int nm = n * 10 + m;
                if (nm >= 10 && nm <= 25) {
                    if(end-2>=0) {
                        count += translatechar(arr, end - 1) + translatechar(arr, end - 2);
                    }
                    else if(end-2==-1)
                    {
                        count +=translatechar(arr,0)+1;
                        System.out.println(count+"---");
                    }

                }
                else {
                    count += translatechar(arr, end - 1);
                     System.out.println(count+"+++++");
                }
            }
        }
        System.out.println(count+"aaaaa");
        return count;
    }
    public static void main(String[] args)
    {
        int num = 12225;
        int count = translatechar(num);
        System.out.println(count);
    }
}
7. Test47.java
public class Test47{
    public static int  maxprice(int[][] matrix,int rows,int cols)
    {
        if(matrix==null||matrix.length<1||rows<0||cols<0)
            return -1;
        int[][] price = new int[rows][cols];
        int max =0;
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<cols;j++)
            {
                if(i-1>=0&&j-1>=0)
                {
                    max = ((price[i-1][j]>price[i][j-1])?price[i-1][j]:price[i][j-1]);
                }
                else if(i-1>=0) {
                    max = price[i-1][j];
                }
                else if(j-1>=0)
                {
                    max = price[i][j-1];
                }
                price[i][j] = max+matrix[i][j];
            }
        }
        max = price[0][0];
        for(int i=0;i<rows;i++)
        {
            for(int j=0;j<cols;j++)
            {
                System.out.print(price[i][j]+" ");
                if(max<price[i][j])
                    max = price[i][j];
            }
        }
        return max;
    }
    public static void main(String[] args)
    {
        int[][] matrix = {{1,10,3,8},{12,2,9,6},{5,7,4,11},{3,7,16,5}};
        System.out.println(maxprice(matrix,4,4));
    }
}
8. Test48.java
public class Test48{
    public static int subString(String s)
    {
        if(s==null||s.length()<1)
            return -1;
        int slength = s.length()-1;
        String sub = s.subString(0,1);
        for(int i=1;i<=slength;i++)
        {
            if(sub.indexOf(s.charAt(i))==-1)
            {
                sub = sub+s.charAt(i).toString();
            }
            else
            {
                int index  = sub.indexOf(s.charAt(i));
                if(index==sub.length()-1)
                    sub = s.charAt(i).toString();
                else {
                    sub = sub.subString(index + 1,sub.length())+s.charAt(i).toString();
                }
            }
        }

        return sub.length();
    }
    public static void main(String[] args)
    {
        String s = "arabcacfr";
        int length = subString(s);
        System.out.println(length);
    }
}
9. Test48_2.java
public class Test48_2{
    public static int maxsubString(String s)
    {
        if(s==null)
            return -1;
        int curLength = 0;
        int maxLength = 0;
        int[] position = new int[26];
        for(int i=0;i<position.length;i++)
            position[i]=-1;
        for(int i=0;i<s.length();i++)
        {
            int value = position[s.charAt(i)-'a'];
            if(value<0||i-value>curLength)
                curLength++;
            else
            {
                if(curLength>maxLength)
                    maxLength = curLength;
                curLength = i-value;
            }
            position[s.charAt(i)-'a'] = i;
        //    System.out.println(s.charAt(i));
        }
        if(curLength>maxLength)
            maxLength = curLength;
        return maxLength;
    }
    public static void main(String[] args)
    {
        String s ="arabcacfr";
        System.out.println(maxsubString(s));
    }

}
10. Test49.java
public class Test49{
    public static int getuglynum(int index){
        if(index<1)
            return -1;
        int[] uglynum = new int[index];
        uglynum[0] = 1;
        int pos = 1;
        int p2 =0;
        int p3=0;
        int p5=0;
        while(pos<index)
        {
            int um = min(uglynum[p2]*2,uglynum[p3]*3,uglynum[p5]*5);
            uglynum[pos] = um;
            pos++;
            if(uglynum[p2]*2==um)
                p2++;
            else if(uglynum[p3]*3==um)
                p3++;
            if(uglynum[p5]*5==um)
                p5++;
        }
        return uglynum[pos-1];
    }
    public static int min(int x,int y,int z)
    {
        int m = x>y?y:x;
        int n =m>z?z:m;
        return n;
    }
    public static void main(String[] args)
    {
        int index = 10;
        System.out.println(getuglynum(index));
    }
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值