leetcode题解-566. Reshape the Matrix && 495. Teemo Attacking
来源:互联网 发布:故宫淘宝有没有app 编辑:程序博客网 时间:2024/06/07 18:25
566,题目:
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.Example 1:Input: nums = [[1,2], [3,4]]r = 1, c = 4Output: [[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.Example 2:Input: nums = [[1,2], [3,4]]r = 2, c = 4Output: [[1,2], [3,4]]Explanation:There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.Note:The height and width of the given matrix is in range [1, 100].The given r and c are all positive.
其实本题实现的功能很简单–矩阵reshape操作。示例也很清晰,当reshape满足数组维度时返回结果,否则返回原数组即可。要想判断是否满足reshape条件,我们只需要判断数组元素个数与要reshape的两个维度乘机是否相等即可。所以本题有两种思路,一是遍历原数组nums,将其每个元素赋值给最终结果,二是遍历结果数组,将原数组相应位置元素赋值。两种思路代码入下:
public int[][] matrixReshape1(int[][] nums, int r, int c) { if(r*c != nums.length*nums[0].length) return nums; int [][] res = new int[r][c]; int a=nums.length, b=nums[0].length, count=0; for(int i=0; i<r; i++){ for(int j=0; j<c; j++, count++){ if(count%b == 0) res[i][j] = nums[count/b-1][b-1]; else res[i][j] = nums[count/b][count%b-1]; } } return res; } public int[][] matrixReshape2(int[][] nums, int r, int c) { if(nums==null) return null; if(r*c!=nums.length*nums[0].length)return nums; if(r==nums.length)return nums; int[][] res= new int[r][c]; int a=0; int b=0; for(int[] i:nums){ for(int j:i) { res[a][b]=j; b++; if(b==c){ b=0; a++; } } } return res; }
495, 题目:
In LLP world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.You may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.Example 1:Input: [1,4], 2Output: 4Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately. This poisoned status will last 2 seconds until the end of time point 2. And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds. So you finally need to output 4.Example 2:Input: [1,2], 2Output: 3Explanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned. This poisoned status will last 2 seconds until the end of time point 2. However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status. Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3. So you finally need to output 3.Note:You may assume the length of given time series array won't exceed 10000.You may assume the numbers in the Teemo's attacking time series and his poisoning time duration per attacking are non-negative integers, which won't exceed 10,000,000.
本题也很简单,给了下毒时间点和毒性持续时间,求中毒时间总和。所以我们只需要判断两次下毒时间间隔是否大于毒性持续时间即可,当小于时,加上二者的差而不是毒性持续时间,最后在遍历完数组时,再加上一个持续时间,因为最后一次下毒的毒性将仍然会持续。代码入下所示:
public int findPoisonedDuration(int[] timeSeries, int duration) { int sum = 0; if(timeSeries.length<=0) return sum; for(int i=1; i<timeSeries.length; i++){ if(timeSeries[i] - timeSeries[i-1] >= duration) sum += duration; else{ sum +=timeSeries[i] - timeSeries[i-1]; } } return sum+duration; }
0 0
- leetcode题解-566. Reshape the Matrix && 495. Teemo Attacking
- LeetCode题解:Teemo Attacking
- 【数组】Leetcode编程题解:495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- LeetCode:495. Teemo Attacking
- 【Leetcode】495. Teemo Attacking
- [leetcode] 495. Teemo Attacking
- leetcode 495. Teemo Attacking
- 【LeetCode】495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- LeetCode 495. Teemo Attacking
- leetcode 495. Teemo Attacking
- leetcode 495. Teemo Attacking
- [LeetCode] 495. Teemo Attacking
- leetcode 495. Teemo Attacking
- leetcode 495. Teemo Attacking
- leetcode 566. Reshape the Matrix
- Mask-RCNN的主要贡献
- win10需要system权限才能修改的解决办法
- Windows下VS2013 C++编译测试faster-rcnn(2017-02-26)
- python小工具——下载更新代码工具
- linux命令之tail
- leetcode题解-566. Reshape the Matrix && 495. Teemo Attacking
- 回归树
- AIX系统下如何开启和关闭防火墙
- 使用maven进行springmvc+hibernate的配置文件
- Web缓存
- iOS 应用程序生命周期
- 数独;深搜加回溯南阳722题
- Zxing二维码的生成与扫描,调起相册
- 纯虚析构函数和非纯虚析构函数