【LEETCODE】61、對leetcode的想法&數組分類,適中級別,題目:162、73

這幾天一直再想這樣刷題真的有必要麼,這種單純的刷題刷得到盡頭麼???

這種出題的的題目是無限的隨便百度,要多少題有多少題,那麼我這一直刷的意義在哪裏???

最近一直苦苦思考,不明所以,刷題刷得更多的感受是機械化的操作。

抽空看了以前喬布斯的演講有點感受,經過幾天的思考最終我想通了。

這裏刷題是對自己思考方式的提煉,這種題寫多了對自己編碼的思維方式有所提升這個是無形的。

其次我沒必要去這麼刷題,因爲題目無限,時間有限,所以應該做的是去品味每一道題,思考如果以後遇到同類的題,我應該從一個什麼樣的角度去思考,明白如何去實現。

所以要時刻總結,不能題海,還要有質量,只有質量和數量都上去了,才能見識正在的威力,單純求質量也是不夠的,這個度要自己把握了!!!

加油

今日份的leetcode

package y2019.Algorithm.array.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: FindPeakElement
 * @Author: xiaof
 * @Description: TODO 162. Find Peak Element
 * A peak element is an element that is greater than its neighbors.
 * Given an input array nums, where nums[i] ≠ nums[i+1], find a peak element and return its index.
 * The array may contain multiple peaks, in that case return the index to any one of the peaks is fine.
 * You may imagine that nums[-1] = nums[n] = -∞.
 *
 * 峯值元素是指其值大於左右相鄰值的元素。
 * 給定一個輸入數組 nums,其中 nums[i] ≠ nums[i+1],找到峯值元素並返回其索引。
 * 數組可能包含多個峯值,在這種情況下,返回任何一個峯值所在位置即可。
 * 你可以假設 nums[-1] = nums[n] = -∞。
 *
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/find-peak-element
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 *
 * Input: nums = [1,2,3,1]
 * Output: 2
 * Explanation: 3 is a peak element and your function should return the index number 2.
 *
 * @Date: 2019/7/24 8:57
 * @Version: 1.0
 */
public class FindPeakElement {

    public int solution(int[] nums) {

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

        if(nums.length == 1) {
            return 0;
        }

        //二分查找局部最大值
        int l  = 0, r = nums.length - 1;
        int res = -1;


        while (l <= r) {
            int mid = (l + r) / 2;
            //判斷mid是否鋒值,比之前的數據和之後的數據都要大,如果是邊緣數據,那麼就只要比較一邊就行
            int left = (mid - 1) < 0 ? Integer.MIN_VALUE : nums[mid - 1];
            int right = (mid + 1) >= nums.length ? Integer.MIN_VALUE : nums[mid + 1];
            if(nums[mid] > left && nums[mid] > right) {
                res = mid;
                return res;
            } else if (left > nums[mid]) {
                //左邊比較大,我們往左邊靠
                r = mid - 1;
            } 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(m + n) space, but still not the best solution.
 * Could you devise a constant space solution?
 *
 * @Date: 2019/7/24 9:44
 * @Version: 1.0
 */
public class SetZeroes {

    public void solution(int[][] matrix) {
        //這題要求空間複雜度小於O(m+n)
        //如果是這個要求的話,雙循環直接再原來的矩陣中操作
        //1.首先吧矩陣的邊上的位置應該爲0的設置爲0
        int c = 1;
        for(int i = 0; i < matrix.length; ++i) {
            if(matrix[i][0] == 0) c = 0; //如果本身這個位置邊上就是0
            for(int j = 1; j < matrix[i].length; ++j) {
                //設置邊爲0
                if(matrix[i][j] == 0) {
                    matrix[i][0] = matrix[0][j] = 0;
                }
            }
        }
        //2.再次遍歷矩陣,只要這個i,j對應的值的i,0或者0,j爲0,那麼就設置爲0
        //因爲設置爲0的是上面和左邊,那麼我們從右下開始遍歷
        for(int i = matrix.length - 1; i >= 0; --i) {
            for(int j = matrix[i].length - 1; j >= 1; --j) { //第一列不用遍歷,因爲已經修改過了
                //設置邊爲0
                if(matrix[i][0] == 0 || matrix[0][j] == 0) {
                    matrix[i][j] = 0;
                }
            }
            //最後一列單獨判斷,避免重複吧變化了之後的數據再次操作
            if(c == 0) {
                //c標識這個列上本身存在0
                matrix[i][0] = 0;
            }
        }

    }

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章