Sword refers to offer (1) Find in a two-dimensional array + replace spaces + print the linked list from the end to the beginning

Record and review some question types of Jianzhi offer~


Lookup in a two-dimensional array

Title description

In a two-dimensional array (each one-dimensional array has the same length), each row is sorted in increasing order from left to right, and each column is sorted in increasing order from top to bottom. Please complete a function, input such a two-dimensional array and an integer to determine whether the array contains the integer.


It can be seen from the title that an integer is found in a two-dimensional array that increases from left to right and from top to bottom.
So we can have an idea, from the beginning of the bottom up over the calendar, and then from left to right traverse , you can get the required number.

public class Solution {
    
    
    public boolean Find(int target, int [][] array) {
    
    
        //二维数组长度为0或者内部一维数组长度为0直接返回false
        if(array.length == 0 || array[0].length == 0) return false;
        //从下开始往上遍历
        for(int i=array.length - 1; i >= 0; i--){
    
    
            //如果每个数组的第一个值就大于了目标值,直接进行下一轮循环
            if (array[i][0] > target){
    
    
                continue;
            }
            //然后再从左到右遍历数组,也就是从小到大
            for(int j=0;j<array[i].length; j++){
    
    
                //如果确认直接返回
                if(array[i][j] == target){
    
    
                    return true;
                }
                //如果大于则退出进入下一轮循环
                if(array[i][j] > target){
    
    
                    break;
                }
            }
        }
        //循环结束返回false;
        return false;
    }
}

Replace spaces

Title description
Please implement a function to replace each space in a string with "%20". For example, when the string is We Are Happy. The replaced string is We%20Are%20Happy.


Replace spaces, traverse the string from back to front, if a space is encountered, replace it with str.replace(index, index+1, targer)

public class Solution {
    
    
    public String replaceSpace(StringBuffer str) {
    
    
        //获取字符串最后一位的下标
    	int length = str.length() - 1;
    	//只要坐标没有到-1则继续循环
        while (length >= 0){
    
    
            //当遍历得到" "的时候
            if (" ".equals(str.charAt(length)+"")){
    
    
                //通过replace进行替换空格为%20
                str.replace(length,length+1,"%20");
            }
            //下标-1
            length--;
        }
        //变为String类型返回
        String s = str.toString();
        return s;
    }
}

Print the linked list from end to beginning

Title description
Enter a linked list and return an ArrayList in the order from the end to the beginning of the linked list.


The easiest way is to create an ArrayList, and then traverse an element to the position of subscript 0.

/**
*    public class ListNode {
*        int val;
*        ListNode next = null;
*
*        ListNode(int val) {
*            this.val = val;
*        }
*    }
*
*/
import java.util.ArrayList;
public class Solution {
    
    
    public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
    
    
        //创建ArrayList
        ArrayList<Integer> list = new ArrayList<>();
        //当前node不为null时
        while(listNode != null){
    
    
            //往头部插入
            list.add(0, listNode.val);
            listNode = listNode.next;
        }
        //返回list
        return list;
    }
}

Guess you like

Origin blog.csdn.net/qq_41762594/article/details/105980743