[日常刷题]leetcode D44

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/83317575

561. Array Partition I

Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1, b1), (a2, b2), …, (an, bn) which makes sum of min(ai, bi) for all i from 1 to n as large as possible.

Example 1:

Input: [1,4,3,2]

Output: 4
Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).

Note:

  1. n is a positive integer, which is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

Solution in C++:

关键点:

  • 相差最小的两个数一组所求和最大

思路:

  • 基于关键点,实现方式就很简单了,直接sort排序一下,然后顺序加就好了。(记得一组里面加最前面那个最小的)
int arrayPairSum(vector<int>& nums) {
        int sum = 0;
        sort(nums.begin(), nums.end());
        for(int i = 0; i < nums.size(); i = i + 2){
            sum += nums[i];
        }
        return sum;
    }

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.

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:

  1. The height and width of the given matrix is in range [1, 100].
  2. The given r and c are all positive.

Solution in C++:

关键点:

  • 多维数组实质

思路:

  • 可能对于vector实现多维数组还是不太熟,习惯了以前的int[][]的方法,这里让我纠结了一下哪个是行哪个是列的问题,其实本质剖析一下,二维数组就是一维数组的每个元素是数组,那么这里就很清楚了,最外层的是列,里层的vector是行,nums[0]相当于第一个数组一样,nums[0][0]就是第一个数组的第一个数。搞清楚这个关系之后就好办了,转换前后,元素总个数一样,实际每个元素的位置就是i * origin_c+j也可以表示为tr * c + tc,每替换一次++tc这里的tr = tc / c来表示行号,tc = tc % c来表示列号即可。
  • 对于特殊情况的处理,根据实例可以知道在两个矩阵大小不相同的情况下不可以转换直接返回原矩阵即可。
vector<vector<int>> matrixReshape(vector<vector<int>>& nums, int r, int c) {
        vector<vector<int>> result;
        
        // 判断是否可行
        int origin_r = nums.size();
        int origin_c = nums[0].size();
        if (origin_r * origin_c != r * c)
            return nums;
        
        // 转换
        int tc = 0;
        vector<int> tmp;
        for(int i = 0; i < origin_r; ++i){
            
            for(int j = 0; j < origin_c; ++j){
                tmp.push_back(nums[i][j]);
                ++tc;
                if (tc == c){
                    result.push_back(tmp);
                    tc = 0;
                    tmp.clear();
                }
            }
             if (tc == c){
                    result.push_back(tmp);
                    tc = 0;
                    tmp.clear();
                }
        }
        return result;
    }

小结

今天算是克制住自己没有玩耍来刷题了,因为明天要出行啦,去杭州参加CNCC的会议,正在收拾行李,还在想要不要缓存什么东西去看呢。一醒悟就来刷了两题,不过可能也是过于简单了,刷的还比较顺利。不过第二题以及之前的题不断用sort,让我觉得可能自己对于排序算法还是要再重新捡起来了,加油~

知识点

  • 多维数值本质

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83317575