[日常刷题]leetcode D44

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,让我觉得可能自己对于排序算法还是要再重新捡起来了,加油~

知识点

  • 多维数值本质
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python 是一种流行的高级编程语言,因其简洁易读的语法和广泛的应用领域而受到开发者喜爱。LeetCode 是一个在线编程平台,专门用于算法和技术面试的准备,提供了大量的编程题目,包括数据结构、算法、系统设计等,常用于提升程序员的编程能力和解决实际问题的能力。 在 Python 中刷 LeetCode 题目通常涉及以下步骤: 1. **注册账户**:首先在 LeetCode 官网 (https://leetcode.com/) 注册一个账号,这样你可以跟踪你的进度和提交的代码。 2. **选择语言**:登录后,在个人主页设置中选择 Python 作为主要编程语言。 3. **学习和理解题目**:阅读题目描述,确保你理解问题的要求和预期输出。题目通常附有输入示例和预期输出,可以帮助你初始化思考。 4. **编写代码**:使用 Python 编写解决方案,LeetCode 提供了一个在线编辑器,支持实时预览和运行结果。 5. **测试和调试**:使用给出的测试用例来测试你的代码,确保它能够正确地处理各种边界条件和特殊情况。 6. **提交答案**:当代码完成后,点击 "Submit" 提交你的解法。LeetCode 会自动运行所有测试用例并显示结果。 7. **学习他人的代码**:如果遇到困难,可以查看社区中的其他优秀解法,学习他人的思路和技术。 8. **反复练习**:刷题不是一次性的事情,通过反复练习和优化,逐渐提高解题速度和代码质量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值