leetcode题解-566. Reshape the Matrix && 495. Teemo Attacking

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 = 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.
Example 2:
Input: 
nums = 
[[1,2],
 [3,4]]
r = 2, c = 4
Output: 
[[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], 2
Output: 4
Explanation: 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], 2
Output: 3
Explanation: 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值