剑指offer的java实现(1)

xiaoxiao2021-02-28  105

1.

/**  * 在一个二维数组中,每一行都按照从左到右递增的顺序排序,  * 每一列都按照从上到下递增的顺序排序。  * 请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。  * @author shuaicenglou  *  */ public class Main {     public static void main(String[] args) {         int[][] num = {{1,2,8,9},{2,4,9,12},{4,7,10,13},{6,8,11,15}};         System.out.println(ifinthis(num,999));         System.out.println(ifinthis(num, 7));     }     public static boolean ifinthis(int[][] num,int point){         boolean result = false;         int h = 0,l = num[0].length-1; //h为行,l为列         while(h<=num.length-1&&l>=0){             if(num[h][l]==point){                 result = true;                 break;             }else if(num[h][l]>point) l-=1;             else h+=1;         }         return result;     } }

2.

/**  * 请实现一个函数,将一个字符串中的空格替换成“ ”。  * 例如,当字符串为We Are Happy.则经过替换之后的字符串为We Are Happy。  * @author shuaicenglou  *  */ public class Main2 {     public static void main(String[] args) {         System.out.println(replaceSpace1(new StringBuffer("   ")));     }     /**      * 新建内存替换      * @param str      * @return      */     public static String replaceSpace1(StringBuffer str) {         String s = str.toString(),result = "";                 byte[] chars = s.getBytes();         for(int i=0;i<chars.length;i++){             char c = (char)chars[i];             if(c==' '){                 result+=" ";             }else result+=c;         }         return result;     } } 第二题结束还有一道拓展题:

/**  * 有两个排序的数组A1和A2,内存在A1的末尾有足够多的空间容纳A2,  * 请实现一个函数,将A2中所有数字插入到A1并且所有数字有序  * 思路:从末尾开始比较插入,减少复制次数  * @author shuaicenglou  *  */ public class Main2_1 {     public static void main(String[] args) {         int[] A1 = new int[7],A2={2,3,4};         A1[0] = 1;         A1[1] = 5;         A1[2] = 6;         A1[3] = 7;         insert(A1,A2);         for(int i:A1) System.out.println(i);     }     public static void insert(int[] A1,int[] A2){         int p1=0,p2=0;         for(int i=0;i<A1.length;i++){             if(A1[i]==0){                 p1 = i-1;                 p2 = p1+A2.length;                 break;             }         }         if(p1<0){}//边界判断,视情况而定,此处不多做处理         //开始从尾到头复制,比较A1与A2的末尾最大值         int A2p = A2.length-1;         while(p1>=0&&A2p>=0){             if(A1[p1]>=A2[A2p]){                 A1[p2] = A1[p1];                 p1-=1;             }else{                 A1[p2] = A2[A2p];                 A2p-=1;             }             p2-=1;         } //            p1    p2     //        1 5 6 7 0 0 0 //          2 3 4 //              A2p     } }

3.

import java.util.ArrayList; /**  * 输入一个链表,从尾到头打印链表每个节点的值  * 思路:  * 1.通过栈输出  * 2.遍历时使用数组记录值,然后从尾到头输出  * 经测试,方法?的性能更好  * @author shuaicenglou  *  */ public class Main3 {     public static ArrayList<Integer> re = new ArrayList<Integer>();     public static void main(String[] args) {         ListNode head = new ListNode(5),n1 = new ListNode(81),n2 = new ListNode(61),n3 = new ListNode(95);         head.next = n1;         n1.next = n2;         n2.next = n3;         ArrayList<Integer> result = printListFromTailToHead(head);         for(int i:result)System.out.println(i);     }     /**      * 递归输出,链表过长时递归可能会导致调用栈溢出      * @param listNode      * @return      */     public static ArrayList<Integer> printListFromTailToHead(ListNode listNode) {         if(listNode!=null){             if(listNode.next!=null){                 printListFromTailToHead(listNode.next);             }             re.add(listNode.val);         }         return re;     }     /**      * 数组逆序输出      * @param listNode      * @return      */     public static ArrayList<Integer> printListFromTailToHead2(ListNode listNode) {         if(listNode==null) return new ArrayList<Integer>();         else{             ArrayList<Integer> middle = new ArrayList<Integer>();             ListNode mid = listNode;             while(mid!=null){                 middle.add(mid.val);                 mid = mid.next;             }             int size = middle.size();             ArrayList<Integer> result = new ArrayList<Integer>(size);             for(int i=size-1;i>=0;i--){                 result.add(middle.get(i));             }             return result;         }     } } class ListNode {     int val;     ListNode next = null;     ListNode(int val) {         this.val = val;         }     }

4.

/**  * 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。  * 假设输入的前序遍历和中序遍历的结果中都不含重复的数字。  * 例如输入前序遍历序列{1,2,4,7,3,5,6,8}  * 和中序遍历序列{4,7,2,1,5,3,8,6},  * 则重建二叉树并返回。  * @author shuaicenglou  *  */ public class Main4 { } /**  * 定义树的节点  * @author shuaicenglou  *  */ class TreeNode {     int val;     TreeNode left;     TreeNode right;     TreeNode(int x) { val = x; } }

转载请注明原文地址: https://www.6miu.com/read-42645.html

最新回复(0)