Offer to prove safety topics

1. Find (simple question) two-dimensional array

Ideas: The initial lookup at the lower left corner of the array, if a [i] [j]> target, it is possible in the above described row, i--; if a [i] [j] <target, it may be described the right column, j ++; if a [i] [j] == target, then the found

public  class Solution {
     public  Boolean the Find ( int target, int [] [] Array) {
         int I = be array.length-. 1, J = 0; // beginning in the lower left 
        the while (I> = 0 && J> = 0 && J <Array [0] && .length I < be array.length) {
             IF (Array [I] [J] == target) return  to true ;
             the else  IF (Array [I] [J]> target) i--; // greater than the target number, up to find 
            the else J ++; // less than the target number, find the right 
        }
         return  false ; 
    } 
}
View Code

2. Replace spaces (simple question)

Idea: using some methods StringBuffer, String, such as: toString (), append (), charAt ()

public class Solution {
    public String replaceSpace(StringBuffer str) {
        StringBuffer s1 = new StringBuffer();
        String s2 = str.toString();
        char c = ' ';
        for(int i = 0;i < s2.length();i++){
            if(c == s2.charAt(i)) s1.append("%20");
            else s1.append(s2.charAt(i));
        }
        return s1.toString();
    }
}
View Code

Guess you like

Origin www.cnblogs.com/Aiahtwo/p/12074317.html