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


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


Test1 - Test9系列

1. Test3_1.java
public class Test3{
    public static int  dulplicate(int[] number)
    {
        if(number==null|| number.length<=0)
            return -1;
        for(int i : number)
        {
            if(i<0||i>=number.length)
                return -1;
        }
        int i = 0;
        for(;i<number.length;++i)
        {
            while(number[i]!=i) {
                if (number[i] == number[number[i]])
                    return number[i];
                else
                    swap(number, i, number[i]);
            }

        }
        return -1;
    }
    public static void swap(int[] number,int i,int num)
    {
        int temp = number[i];
        number[i] = number[num];
        number[num] = temp;
    }
    public static void main(String[] args)
    {
        int[] number = {5,4,1,2,3,5};
        int temp = dulplicate(number);
        System.out.println(temp);
    }
}
2. Test3_2.java
public class Test3_2{
    public static int dulplicate(int[] num)
    {
        if(num==null || num.length<1)
            return -1;
        for(int i : num) {
            if (i < 1 || i > num.length - 1)
                return -1;
        }
        int[] copy = new int[num.length];
        for(int i = 0;i<num.length;i++)
            copy[i]=0;
        int i = 0;
        for(;i<num.length;i++)
        {
            if(num[i]!=i+1)
                if(copy[num[i]]==0)

                    copy[num[i]]=num[i];
                else
                    return num[i];
        }
        return -1;
    }
    public static void main(String[] args)
    {
        int[] num = {2,5,4,5,3,2,6,7};
        System.out.println(dulplicate(num));
    }

}
3. Test4.java
public class Test4{
    public static boolean containNum(int[][] number,int num )
    {
        if(number==null|| number.length<1||number[0].length<1)
            return false;
        int rows = number.length;
        int cols = number[0].length;
        int row = 0;
        int col = cols - 1;
        while(row<rows&&row>=0&&col<=cols&&col>=0)
        {
            if(number[row][col]==num)
                return true;
            else if(number[row][col]>num)
                col--;
            else
                row++;
        }
        return false;
    }
    public static void main(String[] args)
    {
        int[][] matrix ={{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};
        System.out.println(containNum(matrix,7));
    }
}
4. Test5.java
public class Test5{
    public static String replaceBlank(char[] array,int usedLength)
    {
        if(array==null||array.length<usedLength)
            return null;
        int count = 0;
        for(int i=0;i<usedLength;i++) {
            if (array[i] == ' ')
                count++;
        }
        if(count==0)
            return new String(array,0,usedLength);
        int targetLength=usedLength+count*2;
        if(targetLength>array.length)
            return null;
        int end = targetLength-1;
        int point = usedLength-1;
        while(point>=0&& end>point)
        {
            if(array[point]==' ')
            {
                array[end--]='0';
                array[end--]='2';
                array[end--]='%';
            }
            else
                array[end--]=array[point];
            point--;
        }
        return new String(array,0,targetLength);
    }
    public static void main(String[]  args)
    {
        char[] array = new char[50];
        array[0] = ' ';
        array[1] = 'e';
        array[2] = 'r';
        array[3] = ' ';
        System.out.println(replaceBlank(array,4));
    }
}
5. Test5_2.java
public class Test5_2{
    public static int[] mergetTwoArray(int[] a,int[] b)
    {
        if(a==null||b==null) {
            System.out.println("输入数组异常");
            System.exit(0);
        }
        if(b.length==0)
            return a;
        int count=0;
        while(a[count]!=0||a[count+1] !=0){
            count++;
        }
        int i = count-1;
        int j = b.length-1;
        int k = count+b.length-1;
        while(i>=0&&j>=0&&k>i)
        {
            if(a[i]==b[j])
            {
                a[k--]=b[j--];
                a[k--]=a[i--];
            }
            else if(a[i]>b[j])
                a[k--]=a[i--];
            else
                a[k--]=b[j--];
        }
        while(i>=0&&k>i)
        {a[k--]=a[i--];}
        while(j>=0&&k>i)
        {a[k--]=b[j--];}
        return a;
    }
    public static void main(String[] args) {
        int[] array1 = new int[]{5,6,7,8,9,0,0,0,0,0,0,0};
        int[] array2 = new int[]{1,2,3,4};

        int[] array = mergetTwoArray(array1, array2);
        for(int i = 0; i < array.length;i++){
            System.out.println(array[i]);
        }


    }
}
6. Test6.java
import java.util.Stack;

public class Test6{
    public static class LinkedList{
        int val;
        LinkedList next;
        public LinkedList(){
            this.val = val;
        }

    }
    public static void printLink(LinkedList root)
    {
        Stack<LinkedList> link = new Stack<LinkedList>();
        LinkedList list = root;
        while(list!=null)
        {
            link.push(list);
            list = list.next;
        }
        LinkedList tmp;
        while(!link.isEmpty()) {
            tmp = link.pop();
            System.out.println(tmp.val);
        }

    }
    public static void main(String[] args)
    {
        LinkedList root = new LinkedList();
        root.val = 1;
        root.next = new LinkedList();
        root.next.val = 2;
        root.next.next = new LinkedList();
        root.next.next.val = 3;
        printLink(root);
    }
}

/**
 *单链表的操作
 */
class LNode {
    private int data;
    private LNode next;

    public void setData(int data) {
        this.data = data;
    }

    public int getData() {
        return data;
    }

    public void setNext(LNode next) {
        this.next = next;
    }

    public LNode getNext() {
        return next;
    }
}
public class LinkedList{
    public LNode head;
    /**
     * 尾插法
     */
    public void createLink(int[] arr)
    {

        LNode tail = new LNode();
        head = tail;
        for(int i=0,i<arr.length;i++)
        {
            LNode newnode = new LNode();
            newnode.setData(arr[i]);
            tail.setNext(newnode);
            newnode.setNext(null);
            tail = newnode;
        }

    }
    /**
     * 判断链表中元素是否存在的方法
     */
    public static boolean find(int value)
    {
        LNode ptr;
        ptr = head.getNext();
        while (ptr != null) {
            if(ptr.getData()==value)
                return true;
            else
                ptr = ptr.getNext();

        }
        return false;
    }

    /**
     * 定义删除链表中元素的方法
     */
    public static void delete(int value) {
        LNode ptr;
        LNode pnt;
        ptr = head;
        pnt = head.getNext();
        while (pnt != null) {
            if (value == pnt.getData())
                ptr.setNext(ptr.getNext());
            else {
                ptr = pnt;
                pnt = pnt.getNext();
            }
        }
        if (pnt == null)
            System.out.println("没有找到"0);
    }
    /**
     * 定义插入节点的方法
     * @param pos 插入的位置
     * @param value 插入的值
     */
    public static void insert(int pos,int vlaue)
    {
        LNode ptr;
        LNode pnt;
        ptr = head
        pnt = ptr.getNext();
        while(pnt!=null)
        {
            if(pnt.getData()==pos) {
                LNode node = new LNode();
                node.setData(value);
                node.setNext(pnt);
                ptr.setNext(node);
            }
            else
            {
                ptr = pnt;
                pnt = pnt.getNext();
            }
        }
        if(pnt==null)
            System.out.println("输入的位置有误");

    }
    /**
     * 定义一个输出链表内容的方法
     */
    public static void print()
    {
        LNode ptr;
        ptr = head;
        if(ptr!=null)
            ptr=ptr.getNext();
        else
            System.out.println("null");
        while(ptr!=null)
        {
            System.out.println(ptr.getData());
            ptr=ptr.getNext();
        }

    }
}
7. Test7.java
public class Test7{
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        public TreeNode(int val){
            this.val = val;
        }
    }
    public static TreeNode construct(int[] a,int[] b)
    {
        if(a==null&&b==null&&a.length!=b.length&&a.length<1)
            return null;
        return construct(a,0,a.length-1,b,0,b.length-1);
    }
    public static TreeNode construct(int[] a,int sa,int ea,int[] b,int sb,int eb)
    {
        if(sa>ea&&sb>eb)
            return null;
        int value = a[sa];
        int  pos= sb;
        while(sb<=eb&&b[pos]!=value) {
            pos++;
        }
        if(sb>eb)
            return null;
        TreeNode root = new TreeNode(value);
        root.left = construct(a,sa+1,sa+pos-sb,b,sb,pos-1);
        root.right = construct(a,sa+pos-sb+1,ea,b,pos+1,eb);
        return root;
    }
    public static void print(TreeNode root)
    {
        if(root!=null)
        {
            print(root.left);
            System.out.println(root.val+"");
            print(root.right);
        }
    }
    public static void main(String[] args)
    {
        int[] a = {1,2,4,7,3,5,6,8};
        int[] b = {4,7,2,1,5,3,8,6};
        TreeNode root = construct(a,b);
        print(root);
    }
}
8. Test8.java
public class Test8{
    public static  class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode next;
        public TreeNode(int val)
        {
            this.val = val;
        }
    }
    public static TreeNode nextNode(TreeNode node) {
        if (node == null)
            return null;
        TreeNode pNode = node;
        if (pNode.right != null)
        {
            pNode = pNode.right;
        while (pNode != null && pNode.left != null) {
            pNode = pNode.left;
        }
        return pNode;
    }
        while(pNode.next!=null)
        {
            if(pNode.next.left==pNode)
            {
                return pNode.next;
            }
            pNode = pNode.next;
        }
        return null;
    }
    public static void main(String[] args)
    {
        TreeNode n1 = new TreeNode(1);
        TreeNode n2 = new TreeNode(2);
        TreeNode n3 = new TreeNode(3);
        TreeNode n4 = new TreeNode(4);
        TreeNode n5 = new TreeNode(5);
        TreeNode n6 = new TreeNode(6);
        TreeNode n7 = new TreeNode(7);
        n1.left = n2;
        n1.right = n3;
        n1.next = null;
        n2.next = n1;
        n2.left = n4;
        n2.right = n5;
        n3.next = n1;
        n3.left = n6;
        n3.right = n7;
        n4.next = n2;
        n4.left = null;
        n4.right = null;
        n5.next = n2;
        n5.right = null;
        n5.left = null;
        n6.next = n3;
        n6.left = null;
        n6.right = null;
        n7.next = n3;
        n7.left = null;
        n7.right = null;
        TreeNode tn  = nextNode(n5);
        System.out.println(tn.val+"");
    }
}
9. Test9.java
import java.util.Stack;
public class Test9 {
    public static class towStackimplDuilie {
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();
        public  towStackimplDuilie() {}
        public  void appendTail(Integer t)
        {
            if(s1.isEmpty()&&s2.isEmpty())
                s1.add(t);
            else if(s1.isEmpty())
                s2.add(t);
            else
                s1.add(t);
        }
        public  Integer deleteHead()
        {
            if(s1.isEmpty()&&s2.isEmpty())
                return null;
            if(s1.isEmpty()){
                while(!s2.isEmpty())
                    s1.add(s2.pop());
                return s1.pop();
            }
            if(s2.isEmpty()){
                while(!s1.isEmpty())
                    s2.add(s1.pop());
                return s2.pop();
            }
           return -1;
        }
    }
    public static void main(String[] args)
    {
        towStackimplDuilie duilie = new towStackimplDuilie();
        duilie.appendTail(1);
        duilie.appendTail(2);
        duilie.appendTail(3);
        duilie.appendTail(4);
        Integer t = duilie.deleteHead();
        System.out.println(t);

    }
}


10. Test9_2.java
import java.util.Queue;
import java.util.LinkedList;
public class Test9_2 {
    public static class towQueueImplStack {
        private Queue<Integer> q1 = new LinkedList<Integer>();
        private Queue<Integer> q2 = new LinkedList<Integer>();

        public towQueueImplStack() {
        }

        public void add(Integer t) {
            if (q1.isEmpty() && q2.isEmpty()) {
                q1.offer(t);    //默认offerLast
                return;
            } else if (q1.isEmpty()) {
                q2.offer(t);
                return;
            } else {
                q1.offer(t);
                return;
            }
        }

        public Integer pop() {
            if (q1.isEmpty() && q2.isEmpty())
                return -1;
            else if (q1.isEmpty()) {
                while (q2.size() > 1)
                    q1.offer(q2.poll());
                return q2.poll();  //默认popFirst
            } else {
                while (q1.size() > 1)
                    q2.offer(q1.poll());
                return q1.poll();
            }

        }
    }
    public static void main(String[] args)
    {
        towQueueImplStack stack = new towQueueImplStack();
        stack.add(1);
        stack.add(2);
        stack.add(3);
        stack.add(4);
        stack.add(5);
        Integer t = stack.pop();
        System.out.println(t);
        stack.add(6);
        Integer t1 = stack.pop();
        System.out.println(t1);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值