Skip to main content
 首页 » 编程设计

Java面试剑指offer系列一

2022年07月19日152yxwkf

1.在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。

a)        常规程序,直接遍历二维数组

public class Solution { 
 
    public boolean Find(int [][] array,int target) { 
 
        int flag = 0; 
 
        for(int i = 0; i < array.length; i++){ 
 
            int[] arr2 = array[i]; 
 
            for(int j = 0; j < arr2.length; j++){ 
 
                if(array[i][j] == target){ 
 
                    flag = 1; 
 
                } 
 
            } 
 
        } 
 
        if(flag == 1){ 
 
            return true; 
 
        } else{ 
 
            return false; 
 
        } 
 
    } 
 
    
 
    public static void main(String[] args){ 
 
        int[][] array = new int[][] { { 1 }, { 2, 3 }, { 4, 5, 6 } }; 
 
        Solution s = new Solution(); 
 
        System.out.print(s.Find(array, 7)); 
 
    } 
 
}

b)        最佳解法,从左下角开始比较,当key值小于数组中的值时向上移,当key值大于数组中的值时向右移。

public class Solution { 
 
       public boolean find(int[][] array, int key){ 
 
              int len = array.length - 1; 
 
              int i = 0; 
 
              while((len > 0) && (i < array[0].length)){ 
 
                     if(key < array[len][i]){ 
 
                            len--; 
 
                     }else if(key > array[len][i]){ 
 
                            i++; 
 
                     }else{ 
 
                            return true; 
 
                     } 
 
              } 
 
              return false; 
 
       } 
 
       
 
       public static void main(String[] args) { 
 
              int[][] array = new int[][]{{1,2,3},{4,5,6},{7,8,9}}; 
 
              Solution solution = new Solution(); 
 
              System.out.println(solution.find(array, 5)); 
 
       } 
 
}

c)        代码最简洁

public boolean Find(int [][] array,int target) { 
 
        for (int[] is : array) { 
 
            for (int i = 0; i < is.length; i++) { 
 
                if (is[i] == target) { 
 
                    return true; 
 
                } 
 
            } 
 
        } 
 
        return false; 
 
}

2.请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

a)        我的实现方法,先把StringBuffer转化成一个String字符串,然后遍历字符串,如果存在空格,就替换成“%20”

public class Solution { 
 
       public String replaceSpace(StringBuffer str) { 
 
              String[] s = str.toString().split(""); 
 
              String string = ""; 
 
              for(int i = 0; i < s.length; i++){ 
 
                     if(s[i].equals(" ")){ 
 
                            s[i] = "%20"; 
 
                     } 
 
                     string = string + s[i]; 
 
              } 
 
              return string; 
 
       } 
 
       
 
       public static void main(String[] args) { 
 
              Solution solution = new Solution(); 
 
              StringBuffer str = new StringBuffer("We Are Happy"); 
 
              System.out.println(solution.replaceSpace(str)); 
 
       } 
 
  
 
}

3.输入一个链表,从尾到头打印链表每个节点的值。

输入描述:

输入为链表的表头

输出描述:

输出为需要打印的“新链表”的表头

import java.util.ArrayList; 
 
public class Solution { 
 
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { 
 
        ArrayList<Integer> a = new ArrayList<Integer>(); 
 
        ListNode node = listNode; 
 
        while(node != null){ 
 
            a.add(new Integer(node.val)); 
 
            node = node.next; 
 
        } 
 
        Integer b; 
 
        for(int i = 0; i < a.size()/2; i++){//使用for循环语句把链表的值前后调//换过来 
 
            b=a.get(i); 
 
            a.set(i, a.get(a.size()-i-1));  
 
            a.set(a.size()-i-1,b); 
 
        } 
 
        return a; 
 
    } 
 
}

4.输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不含重复的数字。例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7,2,1,5,3,8,6},则重建二叉树并返回。

public class Solution { 
 
//根节点类 
 
class TreeNode { 
 
        int val; 
 
        TreeNode left; 
 
        TreeNode right; 
 
  
 
        TreeNode(int x) { 
 
               val = x; 
 
        } 
 
} 
 
  
 
public TreeNode reConstructBinaryTree(int[] pre, int[] in) { 
 
        // 根节点是前序遍历中的第一个元素 
 
        TreeNode root = new TreeNode(pre[0]); 
 
        // 当只有一个数据时 
 
        int len = pre.length; 
 
        if (len == 1) { 
 
               root.left = null; 
 
               root.right = null; 
 
        } 
 
        // 找到根节点在中序遍历中的位置 
 
        int rootval = root.val; 
 
        int i; 
 
        for (i = 0; i < in.length; i++) { 
 
               if (rootval == in[i]) { 
 
                      break; 
 
               } 
 
        } 
 
  
 
        // 创建左子树 
 
        if (i > 0) { 
 
               int[] leftPre = new int[i]; 
 
               int[] leftIn = new int[i]; 
 
               for (int j = 0; j < i; j++) { 
 
                      leftPre[j] = pre[j+1]; 
 
                      leftIn[j] = in[j]; 
 
               } 
 
               root.left = reConstructBinaryTree(leftPre, leftIn); 
 
        } else { 
 
               root.left = null; 
 
        } 
 
        // 创建右子树 
 
        if (len - i - 1 > 0) { 
 
               int[] rightPre = new int[len - i - 1]; 
 
               int[] rightIn = new int[len - i - 1]; 
 
               for (int j = i + 1; j < len; j++) { 
 
                      rightPre[j - i - 1] = pre[j]; 
 
                      rightIn[j - i - 1] = in[j]; 
 
               } 
 
               root.right = reConstructBinaryTree(rightPre, rightIn); 
 
        } else { 
 
               root.right = null; 
 
        } 
 
        return root; 
 
} 
 
}

5.用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

       

public class Solution { 
 
              Stack<Integer> stack1 = new Stack<Integer>(); 
 
              Stack<Integer> stack2 = new Stack<Integer>(); 
 
  
 
              public void push(int node) { 
 
                     stack1.push(node);//这里要进队列就直接进stack1 
 
              } 
 
  
 
              public int pop() { 
 
                     int val= 0; 
 
               //把stack1中的内容安倒序放入stack2中 
 
                     while (!stack1.empty()) { 
 
                            stack2.push(stack1.pop()); 
 
                     } 
 
                     if (!stack2.empty()) { 
 
                            val = stack2.pop(); 
 
                     } else { 
 
                            System.out.println("队列为空,返回默认值0"); 
 
                     } 
 
              //实现了出队列 后还要把stack2中的内容还给stack1 
 
                     while (!stack2.empty()) { 
 
                            stack1.push(stack2.pop()); 
 
                     } 
 
                     return val; 
 
              } 
 
  
 
              public static void main(String[] args) { 
 
                     Solution s = new Solution(); 
 
                     s.push(1); 
 
                     s.push(2); 
 
                     s.push(4); 
 
                     System.out.println(s.pop()); 
 
                     System.out.println(s.pop()); 
 
                     System.out.println(s.pop()); 
 
                     System.out.println(s.pop()); 
 
              } 
 
}

本文参考链接:https://www.cnblogs.com/wgl1995/p/5768416.html