【LEETCODE】49、数组分类,简单级别,题目:566,1089

package y2019.Algorithm.array;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: MatrixReshape
 * @Author: xiaof
 * @Description:  TODO 566. Reshape the Matrix
 *
 * In MATLAB, there is a very useful function called 'reshape',
 * which can reshape a matrix into a new one with different size but keep its original data.
 * You're given a matrix represented by a two-dimensional array,
 * and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
 * The reshaped matrix need to be filled with all the elements of the original matrix in the same row-traversing order as they were.
 * If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise,
 * output the original matrix.
 *
 * Input:
 * nums =
 * [[1,2],
 *  [3,4]]
 * r = 1, c = 4
 * Output:
 * [[1,2,3,4]]
 * Explanation:
 * The row-traversing of nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.
 *
 * @Date: 2019/7/8 9:03
 * @Version: 1.0
 */
public class MatrixReshape {

    public int[][] solution(int[][] nums, int r, int c) {
        //吧对应的数组输出为新的矩阵数组,如果不够那么直接输出原来数组
        if(nums[0].length * nums.length != (r * c)) {
            return nums;
        }
        int[][] result = new int[r][c];
        int indexNum = 0;
        //依次遍历数组
        for(int i = 0; i < nums.length; ++i) {
            for(int j = 0; j < nums[i].length; ++j) {
                //遍历所有
                int curNum = i * nums[i].length + j;
                result[curNum / c][curNum % c] = nums[i][j];
            }
        }

        return result;
    }

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

}
package y2019.Algorithm.array;

import java.util.Arrays;

/**
 * @ProjectName: cutter-point
 * @Package: y2019.Algorithm.array
 * @ClassName: DuplicateZeros
 * @Author: xiaof
 * @Description: TODO 1089. Duplicate Zeros
 * Given a fixed length array arr of integers, duplicate each occurrence of zero, shifting the remaining elements to the right.
 * Note that elements beyond the length of the original array are not written.
 * Do the above modifications to the input array in place, do not return anything from your function.
 *
 * Input: [1,0,2,3,0,4,5,0]
 * Output: null
 * Explanation: After calling your function, the input array is modified to: [1,0,0,2,3,0,0,4]
 *
 * @Date: 2019/7/8 9:17
 * @Version: 1.0
 */
public class DuplicateZeros {

    public void solution(int[] arr) {
        //每次遇到0就修改为两次0,然后所有其他的数据右移
        int[] copyArry = Arrays.copyOf(arr, arr.length);
        int index = 0;

        for(int i = 0; i < copyArry.length && index < arr.length; ++i) {
            if(copyArry[i] == 0) {
                arr[index++] = 0;
                if(index < arr.length)
                    arr[index++] = 0;
            } else {
                arr[index++] = copyArry[i];
            }
        }

    }
}
原文地址:https://www.cnblogs.com/cutter-point/p/11152954.html