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];
}
} }
}

最新文章

  1. 使用SecureCRT连接虚拟机(ubuntu)配置记录
  2. [转载]彻底弄清struct和typedef struct
  3. Centos 7.0查看硬盘使用情况 命令
  4. Android学习笔记之如何使用圆形菜单实现旋转效果...
  5. OC基础(21)
  6. 百练 2973 Skew数 解题报告
  7. BZOJ_3282_Tree_(LCT)
  8. stack around the variable “XX” was corrupted
  9. 抽出SqlHelper
  10. MYSQL导入中文数据乱码的四种解决办法
  11. loj553 「LibreOJ Round #8」MINIM
  12. 幽灵自建的html5 的模板文件!
  13. html页面转jsp后 乱码问题。
  14. [转]JIRA 7.2.6与Confluence 6.0.3的安装与配置之MS SQL Server版
  15. Nginx反向代理2--配置文件配置
  16. Fetch API 了解 及对比ajax、axois
  17. 【Android】3.14 公交线路查询功能
  18. javascript 获取服务时间
  19. struts2 - View页面中获取Action的成员变量
  20. ik分词器

热门文章

  1. HTML5 文件读取
  2. 开源一个golang小程序商城后台系统(moshopserver)
  3. MacBook Air装Windows7双系统后的一些(未尝试)想法
  4. MySQL免安装配置步骤
  5. linux 运行时限制CPU核数、内存、读写速度
  6. firewall防火墙常用操作
  7. cat命令创建文件
  8. centos7 docker swarm加入集群失败
  9. oracle导入及导出dmp文件
  10. 使用Fiddler工具发送post请求(带有json数据)以及get请求(Header方式传参)