【LEETCODE】57、數組分類,適中級別,題目:969、442、695

package y2019.Algorithm.array.medium;

import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: PancakeSort
 * @Author: xiaof
 * @Description: TODO 969. Pancake Sorting
 * Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length,
 * then reverse the order of the first k elements of A.
 * We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A.
 *
 * Return the k-values corresponding to a sequence of pancake flips that sort A.
 * Any valid answer that sorts the array within 10 * A.length flips will be judged as correct.
 *
 * Input: [3,2,4,1]
 * Output: [4,2,4,3]
 * Explanation:
 * We perform 4 pancake flips, with k values 4, 2, 4, and 3.
 * Starting state: A = [3, 2, 4, 1]
 * After 1st flip (k=4): A = [1, 4, 2, 3]
 * After 2nd flip (k=2): A = [4, 1, 2, 3]
 * After 3rd flip (k=4): A = [3, 2, 1, 4]
 * After 4th flip (k=3): A = [1, 2, 3, 4], which is sorted.
 *
 * 參考:https://blog.csdn.net/fuxuemingzhu/article/details/85937314
 *
 * @Date: 2019/7/16 8:57
 * @Version: 1.0
 */
public class PancakeSort {

    public List<Integer> solution(int[] A) {
        //思路是這樣的,就是每次吧最大的做一個翻轉,移動到最前面,然後再翻轉到最後面,這樣每次都可以從數據中排除掉最大的那個
        //但是由於這個題的數字都是按照1~n的順序給的值,那麼就不需要每次都取最大值,只要取index索引就可以了
        List<Integer> res = new ArrayList<>();
        for(int i = A.length, x; i > 0; --i) {
            //尋找最大的位置
            for(x = 0; A[x] != i; ++x);
            //當x所在的索引跟當前應該的最大值相等的時候,也就是x指向了最大值的位置的-1位置,我們翻轉兩次
            //第一次吧值翻轉到最前面,第二次翻轉到最後面
            reverse(A, x);
            res.add(x + 1);
            //然後翻轉到對應的位置
            reverse(A, i - 1);
            res.add(i);
        }

        return res;
    }

    private void reverse(int[] A, int k) {
        //翻轉k位
        for(int i = 0, j = k; i < j; ++i,--j) {
            //前後交換
            int temp = A[i];
            A[i] = A[j];
            A[j] = temp;
        }
    }

    public static void main(String[] args) {
        int data[] = {3,2,4,1};
        PancakeSort fuc = new PancakeSort();
        System.out.println(fuc.solution(data));
        System.out.println();
    }

}

 

package y2019.Algorithm.array.medium;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: FindDuplicates
 * @Author: xiaof
 * @Description: TODO 442. Find All Duplicates in an Array
 * Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.
 * Find all the elements that appear twice in this array.
 * Could you do it without extra space and in O(n) runtime?
 *
 * Input:
 * [4,3,2,7,8,2,3,1]
 * Output:
 * [2,3]
 *
 * 給定一個整數數組 a,其中1 ≤ a[i] ≤ n (n爲數組長度), 其中有些元素出現兩次而其他元素出現一次。
 * 找到所有出現兩次的元素。
 * 你可以不用到任何額外空間並在O(n)時間複雜度內解決這個問題嗎?
 * @Date: 2019/7/16 9:00
 * @Version: 1.0
 */
public class FindDuplicates {

    public List<Integer> solution(int[] nums) {

        //直接用set
        List<Integer> res = new ArrayList<>();
        if(nums == null || nums.length <= 0) {
            return res;
        }
        //受限還是排序
        Arrays.sort(nums);
        //遍歷一次
        int preValue = nums[0];
        for(int i = 1; i < nums.length; ++i) {
            int curValue = nums[i];
            if(preValue == curValue) {
                res.add(curValue);
            } else {
                preValue = curValue;
            }
        }

        return res;
    }
}
package y2019.Algorithm.array.medium;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array.medium
 * @ClassName: MaxAreaOfIsland
 * @Author: xiaof
 * @Description: TODO 695. Max Area of Island
 * Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land)
 * connected 4-directionally (horizontal or vertical.) You may assume all four edges of the grid are surrounded by water.
 *Find the maximum area of an island in the given 2D array. (If there is no island, the maximum area is 0.)
 *
 * [[0,0,1,0,0,0,0,1,0,0,0,0,0],
 *  [0,0,0,0,0,0,0,1,1,1,0,0,0],
 *  [0,1,1,0,1,0,0,0,0,0,0,0,0],
 *  [0,1,0,0,1,1,0,0,1,0,1,0,0],
 *  [0,1,0,0,1,1,0,0,1,1,1,0,0],
 *  [0,0,0,0,0,0,0,0,0,0,1,0,0],
 *  [0,0,0,0,0,0,0,1,1,1,0,0,0],
 *  [0,0,0,0,0,0,0,1,1,0,0,0,0]]
 * Given the above grid, return 6. Note the answer is not 11, because the island must be connected 4-directionally.
 *
 * 給定一個包含了一些 0 和 1的非空二維數組 grid , 一個 島嶼 是由四個方向 (水平或垂直) 的 1 (代表土地) 構成的組合。你可以假設二維矩陣的四個邊緣都被水包圍着。
 * 找到給定的二維數組中最大的島嶼面積。(如果沒有島嶼,則返回面積爲0。)
 * 來源:力扣(LeetCode)
 * 鏈接:https://leetcode-cn.com/problems/max-area-of-island
 * 著作權歸領釦網絡所有。商業轉載請聯繫官方授權,非商業轉載請註明出處。
 *
 * @Date: 2019/7/16 9:00
 * @Version: 1.0
 */
public class MaxAreaOfIsland {

    public int solution(int[][] grid) {
        //尋找聚集度最高的和,遍歷所有的島嶼,然後對附近的所有1求和,每次求和探索四個位置的和,上下左右
        int maxIsland = 0;
        for(int i = 0; i < grid.length; ++i) {
            for(int j = 0; j < grid[i].length; ++j) {
                //求出最大的島嶼
                maxIsland = Math.max(maxIsland, areaOfIsLand(grid, i, j));
            }
        }
        return maxIsland;
    }

    private int areaOfIsLand(int[][] grid, int x, int y) {
        if(x >= 0 && x < grid.length && y >=0 && y < grid[x].length && grid[x][y] == 1) {
            //設置標記
            grid[x][y] &= 0;
            return 1 + areaOfIsLand(grid, x - 1, y) + areaOfIsLand(grid, x + 1, y) + areaOfIsLand(grid, x, y - 1) + areaOfIsLand(grid, x, y + 1);
        }

        return 0;
    }



}

 

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