LEETCODE [61], the idea of leetcode & array of classification, moderate level, Title: 162,73

These days have been thinking about this problem really necessary to brush it, this simple question brush brush to get the end of it? ? ?

The title of this topic is unlimited casual Baidu, how many question how many questions, so that where I always brush significance? ? ?

It has recently been hard to think, to know why the question brush brush feeling is more mechanized operations.

Taking the time to read before Jobs' speech a little feeling, after a few days of thinking I finally figured out.

Here is a brush problem refine their own way of thinking, which wrote much of his own way of thinking has improved the coding is invisible.

Secondly, I do not need to brush such questions, because the subject is limited, time is limited, so it should do is to taste each question, thinking, I should have to think from a perspective of what if later encountered similar problems, understand how to achieve.

So to sum up the moment, can not question the sea, but also quality, only the quality and quantity go up, in order to experience the power of being, and quality alone is not enough, the degree to hold their own up! ! !

Refuel

Today, parts of leetcode

Package y2019.Algorithm.array.medium; 

/ ** 
 * @ProjectName: Cutter-Point 
 * @package: y2019.Algorithm.array.medium 
 * @ClassName: FindPeakElement 
 * @author: xiaof 
 * @Description: the Find the TODO 162. the Element Peak 
 * A Peak Element IS AN Element that IS Greater Within last ITS neighbors. 
 * the Given AN INPUT Array the nums, WHERE the nums [I] ≠ the nums [I +. 1], Find A Peak Element and return ITS index. 
 * of The Array On May Contain Multiple Peaks , that in Case One return the index to the any of peaks the iS Fine. 
 * On May by You Imagine that the nums [-1] = the nums [n-] = -∞. 
 * 
 * peak refers to the element whose value is greater than the value of the element adjacent the left and right . 
 Given an input array * nums, wherein nums [i] ≠ nums [i + 1], and returns its peak is found element index. 
 * Array may contain a plurality of peaks, in this case, any return to a peak location.
 * You can assume nums [-1] = nums [n ] = -∞. 
 * 
 * Source: stay button (LeetCode) 
 * Link: https://leetcode-cn.com/problems/find-peak-element 
 * all the copyright collar button network. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. 
 * 
 * The Input: the nums = [1,2,3,1] 
 * the Output: 2 
 * Explanation: Element. 3 IS A Peak Should return function and your Number 2. The index 
 * 
 * @date: 2019/7/24 8:57 
 @Version *: 1.0 
 * / 
public  class FindPeakElement { 

    public  int Solution ( int [] the nums) { 

        IF (the nums == null || nums.length <= 0 ) {
             return -1 ; 
        } 

        IF (nums.length. 1 ==) {
             Return 0 ; 
        } 

        // binary search local maximum 
        int L = 0, R & lt nums.length = -. 1 ;
         int RES = -1 ; 


        the while (L <= R & lt) {
             int MID = (L + R & lt) / 2 ;
             // determine whether the front mid value, bigger than data before and after the data, if the data is an edge, then if you compare the side of the line 
            int left = (mid - 1) <0 of Integer.MIN_VALUE: nums [mid? -. 1 ];
             int right = (+ MID. 1)> = nums.length of Integer.MIN_VALUE: the nums [MID. 1 +? ];
             IF (the nums [MID]> && left the nums [MID]> = right) {
                RES MID;
                 return RES; 
            } the else  IF (left> the nums [MID]) {
                 // left relatively large, to the left we rely 
                R & lt MID = -. 1 ; 
            } the else { 
                L = MID +. 1 ; 
            } 
        } 

        return RES; 
    } 
}

 

package y2019.Algorithm.array.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: SetZeroes
 * @Author: xiaof
 * @Description:  TODO 73. Set Matrix Zeroes
 * Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
 *
 * Input:
 * [
 *   [1,1,1],
 *   [1,0,1],
 *   [1,1,1]
 * ]
 * Output:
 * [
 *   [1,0,1],
 *   [0,0,0],
 *   [1,0,1]
 * ]
 *
 * A straight forward solution using O(mn) space is probably a bad idea.
 A Simple Improvement uses O * (n-m +) Space, But Not Still The Best Solution. 
 * Could you devise Solution Space A Constant? 
 * 
 * @Date: 2019/7/24 9:44 
 * @Version: 1.0 
 * / 
public  class SetZeroes { 

    public  void Solution ( int [] [] matrix) {
         // this problem requires the spatial complexity is less than O (n-m +)
         // If this is desired, then a direct two-cycle operation of the original matrix
         // 1 setting the position of the edge of the first bar of the matrix to be 0 0 
        int C =. 1 ;
         for ( int I = 0; I <matrix.length; ++ I) {
             IF (matrix [I] [0] == 0) C = 0; // if this position of the edge is itself 0
            for ( int  J = 1; j <matrix [i ] .length; ++J) {
                 // set sides are 0 
                IF (Matrix [I] [J] == 0 ) { 
                    Matrix [I] [ 0] = Matrix [0] [J] = 0 ; 
                } 
            } 
        } 
        // 2 again traversing the matrix, i as long as i, j corresponding values, 0 or 0, j is 0, then it is set to 0
         // because 0 is set to above and to the left, then we begin to traverse from the lower right 
        for ( int I = matrix.length -. 1; I> = 0; - I) {
             for ( int J = Matrix [I] .length -. 1; J> =. 1; --j) { // first column without traverse , as it has been modified over
                 // set side for the 0 
                IF(Matrix [I] [0] == 0 || Matrix [0] [J] == 0  ) {
                    Matrix [I] [J] = 0 ; 
                } 
            } 
            // Finally, a separate determination, to avoid duplication of data after it has changed operation again 
            IF (c == 0 ) {
                 // the presence of the c 0 identifies the column itself 
                Matrix [I] [0] = 0 ; 
            } 
        } 

    } 

}

 

Guess you like

Origin www.cnblogs.com/cutter-point/p/11236247.html
Recommended