leetcode[Reshape the Matrix]//待整理多种解法
来源:互联网 发布:创意美工设计招聘 编辑:程序博客网 时间:2024/06/01 09:42
解法一:
class Solution { public int[][] matrixReshape(int[][] nums, int r, int c) { //首先判断能不能重塑矩阵 if(nums.length == 0) return nums; int row = nums.length;//代表原矩阵的行数 int col = nums[0].length;//代表原矩阵的列数,因为原二维数组代表的是矩阵,所以二维数组的第二维的长度都是相同的 if(row * col != r * c) return nums; int[][] res = new int[r][c]; int count = 1;//用一个count来计数,矩阵转换后元素个数是不变的,通过count算出原数组对应的位置 for(int i = 0; i < res.length; i++){ for(int j = 0; j < res[0].length; j++){ //计算当前的count应该对应原数组中的哪一个数 //[[1,2], // [3,4]] 观察这个 int numsI = 0; int numsJ = 0; if(count % col == 0){ numsJ = col - 1; numsI = count / col - 1; } else{ numsJ = count % col - 1; numsI = count / col; } //System.out.println("numsI:" + numsI + " numsJ:" + numsJ); res[i][j] = nums[numsI][numsJ]; count++;//不要忘了递增count } } return res; }}
阅读全文
1 0
- leetcode[Reshape the Matrix]//待整理多种解法
- leetcode[Find the Difference]//待整理多种解法
- leetcode[Construct the Rectangle]//待整理多种解法
- leetcode [Path Sum]//待整理多种解法
- leetcode [Majority Element]//待整理多种解法
- leetcode [Rotate Array]//待整理多种解法
- leetcode [Reverse Bits]//待整理多种解法
- leetcode [Happy Number]//待整理多种解法
- leetcode [Isomorphic Strings]//待整理多种解法
- leetcode [Contains Duplicate]//待整理多种解法
- leetcode[Add Digits]//待整理多种解法
- leetcode[Ugly Number]//待整理多种解法
- leetcode[Missing Number]//待整理多种解法
- leetcode[Word Pattern]//待整理多种解法
- leetcode[Ransom Note]//待整理多种解法
- leetcode[Longest Palindrome]//待整理多种解法
- leetcode[Fizz Buzz]//待整理多种解法
- leetcode[Add Strings]//待整理多种解法
- Ubuntu16 64位安装android studio的32位库 sudo apt-get install lib32bz2-1.0失败
- 华为新员工入职180天详细培训计划
- jsp页面显示当前时间
- JS正则表达式验证数字非常全
- 移动端踩坑解决方法汇
- leetcode[Reshape the Matrix]//待整理多种解法
- 初时HTTP协议
- 朴素贝叶斯法
- 最近公共祖先--RMQ
- hdu6140(思维)
- 4 Network配置
- 使用java 实现网络爬虫 demo
- gr-osmosdr支持GNURadio的扩展
- 游戏中的设计模式二(外观模式)