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]

按照题意,实际上应该是数组元素的排序问题。排序完成后,从第一个元素隔一个求和。

注意:

1. vector 类模板提供了一对操作begin()和end() 它们分别返回指向向量开始处和结束处后1 个的迭代器。这一对迭代器合起来可以标记出待遍历元素的范围。

2.关于sort() 的用法 参考:https://blog.csdn.net/sunshangjin/article/details/40296357

class Solution {
public:
int arrayPairSum(vector<int>& nums) {
sort(nums.begin(),nums.end()); //nums.begin()代表数组的起始地址;nums.end()代表数组的结束地址; sort(s1,s2)对s1到s2位置之间的数组元素进行排序,默认从小到大。
int sum=;
for(int i=;i<nums.size();i+=) //Line 6: stray '\357' in program 原因是输入的分号存在中文字符! 数组长度用nums.size()得到
{
sum+=nums[i];
}
return sum;
}
};

leetcode 561.Array Partition I-easy的更多相关文章

  1. Leetcode#561. Array Partition I(数组拆分 I)

    题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...

  2. LeetCode 561. Array Partition I (数组分隔之一)

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  3. LeetCode 561 Array Partition I 解题报告

    题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...

  4. [LeetCode] 561. Array Partition I_Easy tag: Sort

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  5. LeetCode 561. Array Partition I (C++)

    题目: Given an array of 2n integers, your task is to group these integers into npairs of integer, say ...

  6. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

  7. 561. Array Partition I - LeetCode

    Question 561. Array Partition I Solution 题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个 ...

  8. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  9. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)

    题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...

随机推荐

  1. 爬虫(BeautifulSoup--select--class的选择)

    <div class="item name" title="中央公园"> <a href="/Attraction_Review-g ...

  2. Java 并发编程实践基础 读书笔记: 第一章 JAVA并发编程实践基础

    1.创建线程的方式: /** * StudySjms * <p> * Created by haozb on 2018/2/28. */ public class ThreadDemo e ...

  3. hibernate框架学习笔记12:查询优化

    类级别查询优化: 创建一个实体类: package domain; import java.util.HashSet; import java.util.Set; //客户实体 public clas ...

  4. Ubuntu16.04安装postgresql9.4及pgadmin3图形管理界面

    参考原文链接:http://www.cnblogs.com/sparkdev/p/5678874.html 安装前的检查 首先查看是否已经安装了旧版本: dpkg -l |grep postgresq ...

  5. 网易云音乐APP分析

    网易云音乐-感受音乐的力量 你选择的产品是?  网易云音乐 为什么选择该产品作为分析? 之前用的一直是QQ音乐,但是有一天一个朋友分享了一首网易云上的音乐(顺便分享一下歌名:Drop By Drop) ...

  6. C语言第二次作业---分支结构

    一.PTA实验作业 题目1:计算分段函数[2] 1.实验代码 double x,y; scanf("%lf",&x); if(x>=0){ y=sqrt(x); } ...

  7. C语言博客作业一二维数组

    一.PTA实验作业 题目1.7-5 数组循环左移 1.本题PTA提交列表 2.设计思路 定义变量a[100]存放整数,整数n,整数m,change作为交换数组的媒介,j,i作为循环的变量 输入整数n, ...

  8. 2017-2018-1 Java演绎法 第六七周 作业

    团队任务:修改完善<需求规格说明书>等 团队组长:袁逸灏 本次编辑:刘伟康 修改完善上周提交的需求规格说明书 [markdown 链接] [pdf 链接] 不足之处:仅就现在的问题来看,结 ...

  9. Beta开始前准备

    Beta准备 1. 讨论组长是否重选的议题和结论. 经过讨论,我们认为,经过一段时间的磨合,现任组长是不需要更换的. 2. 下一阶段需要改进完善的功能. 增加关于征信的功能,贴近选题主题 美化界面,尽 ...

  10. margin-top导致父标签偏移问题

    从一个大神博客中看到这句话: 这个问题发生的原因是根据规范,一个盒子如果没有上补白(padding-top)和上边框(border-top),那么这个盒子的上边距会和其内部文档流中的第一个子元素的上边 ...