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

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

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


Test50 - Test59系列

1. Test50.java
import java.util.HashMap;
public class Test50{
    public static char getfirstone(String s)
    {
        if(s==null||s.length()<1)
            return '\0';
        HashMap<Character,Integer> hm = new HashMap<Character,Integer>();
        for(int i=0;i<s.length();i++){
            if(hm.get(s.charAt(i))==null)
            {
                hm.put(s.charAt(i),1);
            }
            else{
                int count = hm.remove(s.charAt(i));
                //System.out.println(count);
                hm.put(s.charAt(i),count+1);
            }
        }
        System.out.println(hm);
        char ch =s.charAt(0);
        for(int j=0;j<s.length();j++)
        {
            if(hm.get(s.charAt(j))==1) {
                ch = s.charAt(j);
                break;
            }

        }
        return ch;
    }
    public static void main(String[] args)
    {
        String s ="abaccdeff";
        System.out.println(getfirstone(s));
    }
}
2. Test50_2.java
public class Test50_2{
    static int[] count = new int[256];
    static int index = 0;
    public static class Getfirst {
        public Getfirst() {
            for (int i = 0; i < count.length; i++)
            { count[i] = -1;}
        }

        public static void insertdata(char ch) {
            if (ch > 255)
                return;
            if (count[ch] == -1)
            { count[ch] = index;}
            else
            { count[ch] = -2;}
            index++;
           // System.out.println(index);

        }
    }

    public static char getfirstchar(String s)
    {
        Getfirst gt = new Getfirst();
        if(s==null||s.length()<1)
            return '\0';
        for(int i=0;i<s.length();i++)
        {
            gt.insertdata(s.charAt(i));
        }
        int min =Integer.MAX_VALUE;
        char ch ='\0';
        //System.out.println(ch);
        for(int i=0;i<count.length;i++)
        {
            System.out.println(count[i]);
            if (count[i]>=0&&count[i]<min) {
                ch =(char)i;
                min = count[i];
                //System.out.println(min);
            }
        }
        return ch;
    }
     public static void main(String[] args)
     {
         String s = "bbaaabed";
         System.out.println(getfirstchar(s));
     }
}
3. Test51.java
public class Test51{
    public static int inversecount(int[] data)
    {
        if(data==null||data.length<1)
            return -1;
        int[] copy = new int[data.length];
        for(int i=0;i<data.length;i++)
            copy[i]=data[i];
        int count = inversecount(data,copy,0,data.length-1);
        return count;
    }
    public static int inversecount(int[] data,int[] copy,int start,int end)
    {
        if(start<0||end>data.length||start>end)
            return -1;
        if(start==end) {
            //copy[start]=data[start];
            data[start]=copy[start];
            return 0;
        }
        int middle = (end-start)/2;
        int left = inversecount(data,copy,start,start+middle);
        int right = inversecount(data,copy,start+middle+1,end);
        int copyindex = end;
        int i = start+middle;
        int j = end;
        int count = 0;
        while(i>=0&&j>=start+middle+1)
        {
            if(data[i]>data[j])
            {
                System.out.println(data[i]+".....");
                System.out.println(data[i]+"....");
                copy[copyindex--] = data[i--];
                count += j-start-middle;
            }
            else {
                copy[copyindex--] = data[j--];
            }
        }
        while(i>=start) {
            copy[copyindex--] = data[i--];
        }
        while(j>=start+middle+1) {
            copy[copyindex--] = data[j--];
        }
        return left+right+count;
    }
    public static void main(String[] args)
    {
        int[] data = {7,5,3,4};
        int[] data1 = {1,3,2,4};
        System.out.println(inversecount(data1));
    }
}
4. Test52.java
public class Test52{
    public static class LinkedList{
        int data;
        LinkedList next;
        public LinkedList(int data)
        {
            this.data = data;
        }
    }
    public static int lengthlink(LinkedList head)
    {
        if(head==null)
            return -1;
        int head_length = 1;
        LinkedList p = head;
        while(p.next!=null)
        {
            head_length++;
            p = p.next;
        }
        return head_length;
    }
    public static LinkedList commonNode(LinkedList head1,LinkedList head2)
    {
        if(head1==null||head2==null)
            return null;
        if(head1==head2)
            return head1;
        LinkedList p1 = head1;
        LinkedList p2 = head2;
        int head1_length = lengthlink(head1);
        int head2_length = lengthlink(head2);
        if(head1_length>=head2_length) {
            int len = (head1_length - head2_length);
            int i = 1;
            while (i <= len && p1.next != null) {
                p1 = p1.next;
                i++;
            }
            if (p1.next == null)
                return null;
//            if(p2.next!=null&&p1.next==p2.next)
//                return p1.next;
            while (p1.next != null && p2.next!=null)
            {
                if(p1.next==p2.next) {
                    break;
                }
                p1 =p1.next;
                p2=p2.next;

            }
        }
        else if(head2_length>head1_length)
        {
            int len = (head2_length - head1_length);
            int i = 1;
            while (i <= len && p2.next != null) {
                p2 = p2.next;
                i++;
            }
            if (p2.next == null)
                return null;
//            if(p2.next!=null&&p1.next==p2.next)
//                return p1.next;
            while (p1.next != null && p2.next!=null)
            {
                if(p1.next==p2.next) {
                    break;
                }
                p1 =p1.next;
                p2=p2.next;

            }
        }
        return p1.next;
    }
    public static void main(String[] args)
    {
        LinkedList n1 = new LinkedList(1);
        LinkedList n2 = new LinkedList(2);
        LinkedList n3 = new LinkedList(3);
        LinkedList n4 = new LinkedList(4);
        LinkedList n5 = new LinkedList(5);
        LinkedList n6 = new LinkedList(6);
        LinkedList n7 = new LinkedList(7);
        n1.next =n2;
        n2.next =n3;
        n3.next=n6;
        n6.next=n7;
        n7.next=null;
        n4.next=n5;
        n5.next=n6;
        LinkedList head1 =n1;
        LinkedList head2 =n4;
        System.out.println(commonNode(head1,head2).data);



    }
}
5. Test53.java
public class Test53{
    public static int getFirstK(int[] data,int start,int end,int k){
        if(data==null||data.length<1)
            return -1;
        int middle = (start+end)/2;
        if(data[middle]==k)
        {
            if(middle==0||middle>1&&data[middle-1]!=k)
                return middle;
            else if(middle>1&&data[middle-1]==k)
            {
                end = middle-1;
            }
        }
        else if(data[middle]>k)
           end = middle-1;
        else
            start = middle+1;
        return getFirstK(data,start,end,k);
    }
    public static int getLastK(int[] data,int start,int end,int k)
    {
        if(data==null||data.length<1)
            return -1;
        int middle = (start+end)/2;
        if(data[middle]==k)
        {
            if(middle==end||data[middle+1]!=k)
                return middle;
            else if(data[middle-1]==k)
            {
                start=middle+1;
            }
        }
        else if(data[middle]>k)
            end = middle-1;
        else
            start = middle+1;
        return getLastK(data,start,end,k);
    }
    public static void main(String[] args)
    {
        int[] data = {1,2,3,3,3,3,4,5};
        System.out.println(getLastK(data,0,7,3)-getFirstK(data,0,7,3)+1);

    }
}
6. Test53_2.java
public class Test53_2{
    public static int getnonum(int[] data,int start,int end)
    {
        if(data==null||data.length<1)
            return -1;
        int middle = (start+end)/2;
        if(data[middle]==middle)
        {
           start = middle+1;
        }
        else if(data[middle]!=middle)
        {
            if(data[middle-1]==middle-1)
                return middle;
            else
                end = middle-1;
        }
        return getnonum(data,start,end);
    }
    public static void main(String[] args)
    {
        int[] data = {0,1,2,4,5};
        System.out.println(getnonum(data,0,5));
    }
}
7. Test53_3.java
public class Test53_3{
    public static int geteqnum(int[] data,int start,int end)
    {
        if(data==null||data.length<1)
            return -1;
        int middle = (start+end)/2;
        if(data[middle]==middle)
        {
            return middle;
        }
        else if(data[middle]>middle)
        {
            end = middle-1;
        }
        else
        {
            start = middle+1;
        }
        return  geteqnum(data,start,end);

    }
    public static void main(String[] args)
    {
        int[] data ={-1,-2,2,3,5,6};
        System.out.println(geteqnum(data,0,5));
    }
}
8. Test54.java
public class Test54{
    public static class BinaryTree{
        int data;
        BinaryTree left;
        BinaryTree right;
        public BinaryTree(int data)
        {
            this.data =data;
        }
    }
    public static BinaryTree KthNode(BinaryTree root,int k)
    {
        if(root==null||k<1)
            return null;
        BinaryTree result = null;
        if(root.left!=null)
        {
            result = KthNode(root.left,k);
        }
        if(result==null)
        {
            if(k==1)
            {
                result = root;
            }
            else
            {
                k--;
            }
        }
        if(result==null&&root.right!=null)
        {
            result = KthNode(root.right,k);
        }
        return result;
    }
}
9. Test55_1.java
public class Test55_1{
    public static class BinaryTree{
        int data;
        BinaryTree left;
        BinaryTree right;
        public BinaryTree(int data)
        {
            this.data = data;
        }
    }
    public static int treedepth(BinaryTree root)
    {
        if(root==null)
            return 0;
        int left = 0;
        int right = 0;
        if(root.left!=null)
             left = treedepth(root.left);
        if(root.right!=null)
            right = treedepth(root.right);
        return left>right?(left+1):(right+1);
    }
}
10. Test55_2.java
public class Test55_2{
    public static class BinaryTree{
        int data;
        BinaryTree left;
        BinaryTree right;
        public BinaryTree(int data)
        {
            this.data = data;
        }
    }
    public static boolean balanceTree(BinaryTree root)
    {
        int[] depth = new int[1];
        return balanceTree(root,depth);
    }
    public static boolean balanceTree(BinaryTree root,int[] depth)
    {
        if(root==null)
        {
            depth[0]=0;
            return true;
        }
        int[] depthl= new int[1];
        int[] depthr = new int[1];
        if(balanceTree(root.left,depthl)&&balanceTree(root.right,depthr)) {
            int dep = depthl - depthr;
            if (dep <= 1 && dep >= -1)
            {
                depth=1+(depthl>depthr?depthl:depthr);
                return true;
            }
        }
        return false;
    }
}
11. Test56.java
public class Test56{
    public static int[] findoncenum(int[] data)
    {
        int[] result = {0,0};
        if(data==null||data.length<2)
            return result;
        int xor = 0;
        for(int i:data)
        {
            xor ^=i;
        }
        int indexof1 = find1(xor);
        for(int i:data)
        {
            if(isbit1(i,indexof1))
            {
                result[0] ^= i;
            }
            else
                result[1] ^= i;
        }
        return result;
    }
    public static int find1(int xor)
    {
        int index = 0;
        while((xor&1==0)&&index<32)
        {
            index++;
            xor = xor>>1;
        }
        return  index;
    }
    public static boolean isbit1(int i,int indexof1)
    {
        i = i>>indexof1;
        return (i&&1);
    }
}
12. Test56_2.java
public class Test56_2{
    public static int[] findoncenum(int[] data)
    {
        int[] result = new int[32];
//        boolean invalidflag = false;
        if(data==null||data.length<1) {
            return result;
        }
        for(int i:data)
        {
            for(int index=31;index>=0;index--)
            {
                result[index] +=(i&1);
                i=i>>1;
            }
        }
        for(int i=0;i<32;i++)
        {
            result[i] = result[i]%3;
        }
        return result;
    }
    public static int findnum(int[] result)
    {
        int sum =0;
        for(int i=result.length-1;i>=0;i--)
        {

            int count = result[i];
            int bit = result[i]&1;
            if(bit==0)
            {
                continue;
            }
            else
            {
                int j = result.length-1-i;
                while(j>=1)
                {
                    count *=2;
                    j--;
                }
            }
            sum +=count;
        }
        return sum;
    }
    public static void main(String[] args)
    {
        int[] data ={2,3,3,3,2,2,5};
        System.out.println(findnum(findoncenum(data)));
    }
}
13. Test57.java
public class Test57{
    public static int[] sumsnum(int[] data,int start,int end,int s)
    {
        int[] result = new int[2];
        if(data==null|| data.length<2)
            return result;
        if(data[start]+data[end]==s)
        {
            result[0] = data[start];
            result[1] = data[end];
        }
        else if(data[start]+data[end]<s)
        {
            start=start+1;
            result = sumsnum(data,start,end,s);
        }
        else
        {
            end=end-1;
            result = sumsnum(data,start,end,s);
        }
        return result;
    }
    public static void main(String[] args)
    {
        int[] data = {1,2,4,7,11,15};
        int[] result= sumsnum(data,0,5,15);
        for(int i:result)
            System.out.println(i);
    }
}
14. Test57_2.java
public class Test57_2{
    public static void sequencesum(int s,int small,int big)
    {
        if(s<3)
            return ;
//        int small = 1;
//        int big =2;
        int middle = (1+s)/2;
        int cursum = small+big;
        while(small<middle)
        {
            if(cursum ==s)
            {
                printsequence(small,big);
            }
            while(cursum>sum&&small<middle)
            {
                cursum -=small;
                small++;
                if(cursum==sum)
                    printsequence(small,big);
            }
            big++;
            cursum +=big;
        }
        return;
    }
}
15. Test58_1.java
public class Test58_1{
    public static void reverseString(String s,int start,int end)
    {
        if(s==null)
            return;
        char[] arr = s.toCharArray();
//        int left =start;
//        int right = end;
        reverseString(arr,start,end);
        int left =0;
        int right =0;
       // int mid =0;
        while(left<=end)
        {
            if(arr[left]==' ') {
                left++;
                right++;
            }
            else if(right==end||arr[right]==' ')
            {
                reverseString(arr,left,right-1);
                right++;
                left = right;
            }
            else
            {
                right++;
            }
        }
        return;
    }
    public static void reverseString(char[] arr,int start,int end)
    {
        char temp  ='\0';
        while(start!=end)
        {
            temp = arr[start];
            arr[start] = arr[end];
            arr[end] = temp;
            start++;
            end--;
        }
    }
}
16. Test59.java
import java.util.ArrayDeque;
import java.util.Deque;
public class Test59{
    public static int[] maxInWindows(int[] data,final int size){
        if(data==null ||data.length==0||data.length<size)
            return new int[0];
        int[] result = new int[data.length-size+1];
        Deque<Integer> deque = new ArrayDeque<>();

        for(int i=0;i<size-1;i++){
            while (!deque.isEmpty()&&data[i]>=data[deque.getLast()])
                deque.removeLast();
            deque.addLast(i);
        }
        for(int i=size-1;i<data.length;i++){
            while (!deque.isEmpty()&&i-deque.getFirst()+1>size)
                deque.removeFirst();
            while (!deque.isEmpty()&&data[deque.getLast()]<=data[i])
                deque.removeLast();
            deque.addLast(i);
            result[i-(size-1)] = data[deque.getFirst()]; //只要往队列里面进一个数,无论前面删了多少个数,
            // 都要将当前队列头元素存入result数组中。因为刚开始就先让进入队列里面2个数,
            // 下面的循环进入一个,就应该将当前的最大值存入result
        }
        return result;
    }
    public static void main(String[] args){
        int[] data = new int[]{2,3,4,2,6,2,5,1};
        int[] result = maxInWindows(data,3);
        for(int i=0;i<result.length;i++){
            System.out.print(result[i]);
            System.out.print("\t");
        }
    }
}



17. Test59_2.java
import java.util.LinkedList;
import java.util.comparable;
import java.util.queue;
public class Test59_2{
    public static class Internal<M extends comparable>
    {
        public M data;
        public int index;
        public Internal(M data,int index)
        {
            this.data = data;
            this.index = index;
        }
    }
    public static class QueueMax<T extends comparable>
    {
        private  queue<Internal<T>> queueData;
        private  queue<Internal<T>> queueMax;
        private  int currentIndex
        public QueueMax()
        {
            this.queueData = new LinkedList<Internal<T>>;
            this.queueMax = new LinkedList<Internal<T>>;
            this.currentIndex =0;
        }
        public M max()
        {
            if(queueMax.isEmpty())
                return null;
            else
            {
                M max = queueMax.getFirst();
                return max.data;
            }
        }
        public void push(M value)
        {
            while(!queueMax.isEmpty()&&value.compareTo(queueMax.getLast().value)>=0)
                queueMax.removeLast();
            Internal<M> data = new Internal<M>(value,currentIndex);
            queueMax.addLast(data);
            queueData.addLast(data);
            currentIndex++;
        }
        public T pop()
        {
            if(!queueData.isEmpty()) {
                T value = queueData.getFirst();
                if(value.data.compareTo(queueMax.getFirst().data)==0)
                {
                    queueMax.removeFirst();
                }
            }
            else
                return null;
            return queueData.remove().data;

        }
    }
}
展开阅读全文
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

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

抵扣说明:

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

余额充值