【LEETCODE】49、数组分类,简单级别,题目:566,1089
2024-09-25 06:57:46
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];
}
} }
}
最新文章
- 使用SecureCRT连接虚拟机(ubuntu)配置记录
- [转载]彻底弄清struct和typedef struct
- Centos 7.0查看硬盘使用情况 命令
- Android学习笔记之如何使用圆形菜单实现旋转效果...
- OC基础(21)
- 百练 2973 Skew数 解题报告
- BZOJ_3282_Tree_(LCT)
- stack around the variable “XX” was corrupted
- 抽出SqlHelper
- MYSQL导入中文数据乱码的四种解决办法
- loj553 「LibreOJ Round #8」MINIM
- 幽灵自建的html5 的模板文件!
- html页面转jsp后 乱码问题。
- [转]JIRA 7.2.6与Confluence 6.0.3的安装与配置之MS SQL Server版
- Nginx反向代理2--配置文件配置
- Fetch API 了解 及对比ajax、axois
- 【Android】3.14 公交线路查询功能
- javascript 获取服务时间
- struts2 - View页面中获取Action的成员变量
- ik分词器