编程1

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

public class Solution {
    public boolean Find(int target, int [][] array) {
       if((array==null||array.length==0)||(array.length==1&&array[0].length==0))
       {
           return false;
       }
        int row = array.length;
        int col = array[0].length;
        
        if(target>array[row-1][col-1]){
            return false;
        }
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++)
            {
                if(target>array[i][col-1]||target<array[i][0])
                {
                    break;
                }
                else
                {
                    if(target==array[i][j])
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

思路: 双循环判断,如果目标值大于每行最大或者小于每行最小,直接进入下一循环。

优化:因为二维数组是有序的,从左下角开始判断,如果小于左下角,直接i--,如果大于,则j++;

class Solution {
public:
    bool Find(vector<vector<int> > array,int target) {
        int rowCount = array.size();
        int colCount = array[0].size();
        int i,j;
        for(i=rowCount-1,j=0;i>=0&&j<colCount;)
        {
            if(target == array[i][j])
                return true;
            if(target < array[i][j])
            {
                i--;
                continue;
            }
            if(target > array[i][j])
            {
                j++;
                continue;
            }
        }
        return false;
    }
};

题目2:

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
public class Solution {
    public String replaceSpace(StringBuffer str) {
        String s1 = str.toString();
        String s2 = s1.replace(" ","%20");
        return s2;
    }
}

题目3:输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
import java.util.Stack;
public class Solution {
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
        ArrayList<Integer> list = new ArrayList<>();
        if(listNode==null){
            return list;
        }
        
        Stack<Integer> stack = new Stack<>();
        while(listNode!=null){
            stack.push(listNode.val);
            listNode=listNode.next;
        }
        while(!stack.empty()){
            list.add(stack.pop());
        }
        return list;
    }
}

思路:;利用栈的先进后出,将链表倒序输出。

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

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.Arrays;
public class Solution {
    public TreeNode reConstructBinaryTree(int [] pre,int [] in) {
         if(pre.length == 0||in.length == 0){
             return null;
         }
         TreeNode node = new TreeNode(pre[0]);
         for(int i = 0; i < in.length; i++){
             if(pre[0] == in[i]){
                 node.left = reConstructBinaryTree(Arrays.copyOfRange(pre, 1, i+1), Arrays.copyOfRange(in, 0, i));
                 node.right = reConstructBinaryTree(Arrays.copyOfRange(pre, i+1, pre.length), Arrays.copyOfRange(in, i+1,in.length));
             }
         }
         return node;

    }
}

思路:使用递归,利用前序和中序的顺序,重建二叉树。

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

import java.util.Stack;

public class Solution {
    Stack<Integer> stack1 = new Stack<Integer>();
    Stack<Integer> stack2 = new Stack<Integer>();
    
    public void push(int node) {
        
        stack1.push(node);
       
    }
    
    public int pop() {
        if(!stack2.empty()){
            return stack2.pop();
        }else{
            while(!stack1.empty()){
                stack2.push(stack1.pop());
            }
        }
        return stack2.pop();
    }
}

思路:将值全部放入栈1,输出的时候,先判断栈2中是否为空,如果不为空则先输出栈2的值,否则将栈1中的所有都放入栈2中,然后输出栈2,。

猜你喜欢

转载自www.cnblogs.com/guoyunhao/p/9693878.html