LeetCode之ArrayPartition
来源:互联网 发布:网络诈骗按照什么罪 编辑:程序博客网 时间:2024/05/21 11:10
题目概述:
给定一个长度为2n的整数数组,把它分成n 对,比如(a1,b1),(a2,b2),...,(an,bn),使得每一对中较小的那个数的总和足够大,并求出总和。
思路:
先将数组按照降序排列,相邻的两个数分为一组,再计算总和。
示例:
Input: [1,4,3,2]Output: 4Explanation: n is 2, and the maximum sum of pairs is 4.
代码:
class Solution {public: int arrayPairSum(vector<int>& nums) { int num=0; //vector排序(升序) sort(nums.begin(),nums.end()); //求和 for(int i=0;i<nums.size();i+=2) { num+=nums[i]; } return num; }};
1 0
- LeetCode之ArrayPartition
- LeetCode 561 : ArrayPartition I
- leetcode 561---- ArrayPartition
- LeetCode 之
- LeetCode之Surrounded Regions
- LeetCode之Palindrome Partitioning
- LeetCode之Word Ladder
- leetcode之Same Tree
- leetcode之sqrt(x)
- leetcode之atoi
- leetcode之Reverse Integer
- leetcode之subsets
- leetcode之Path Sum
- leetcode之same tree
- leetcode之Plus One
- leetcode之Palindrome Partitioning
- leetcode之Valid Palindrome
- leetcode之Edit Distance
- Angular 常见内置指令
- BZOJ 1367: [Baltic2004]sequence 左偏树
- 【电机控制】整流电路Simulink仿真
- 跟小刀学习 lucene 分词的原理
- 内部排序算法1(插入排序)
- LeetCode之ArrayPartition
- Optimal Milking POJ
- C# 中的委托和事件(详解)
- linux虚拟机配置ngnix所遇到的问题
- c语言文件操作函数应用(2) ——学习整理
- 阿里2017实习生招聘笔试-编程题-过滤
- Qt程序显示出命令窗口
- opencv3.2 增加opencv_contrib组件
- [BZOJ4606][Apio2008]DNA(dp)