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. iOS之内存管理(ARC)

    iOS的内存管理,相信大家都不陌生,之前是使用的MRC,由开发人员手动来管理内存,后来使用了ARC,来由系统管理内存.本文主要讲讲Autorelease,Core Foundation对象在内存管理方 ...

  2. Oracle查询优化改写--------------------操作多个表

    一.union all与空字符串 二.组合相关行 三.in .exists.inter join .left join .right join .full join 之间的区别 'inner  joi ...

  3. vue Echarts 柱状图点击事件

    drawBar(){ let that = this; let chart = this.$echarts.init(document.getElementById('bar-graph')); le ...

  4. Oracle中死锁与等待

    在数据库中有两种基本的锁类型:排它锁(Exclusive Locks,即X锁)和共享锁(即S锁).当数据对象被加上排它锁时,其他的事务不能不  能对它读取和修改.加了共享锁的数据对象可以被其他事务读取 ...

  5. 获取服务器时间js代码

    function getSevertime(){ var xmlHttp = new XMLHttpRequest(); if( !xmlHttp ){ xmlHttp = new ActiveXOb ...

  6. [Scala] 安装及环境配置(图文)

    Window 上安装配置 1.Java(JDK)环境配置,详见 Java(JDK)环境 2.从 Scala 官网下载安装包:https://downloads.lightbend.com/scala/ ...

  7. 查看端口使用情况(lsof,netstat, kill)

    在Mac上查看端口使用情况只能使用lsof(list open file),无法使用 netstat. 查看某个端口是否正在被占用: lsof -i:portno 另外,可以通过: lsof 指令来查 ...

  8. CountDownLatch 源码解析—— countDown()

    上一篇文章从源码层面说了一下CountDownLatch 中 await() 的原理.这篇文章说一下countDown() . public void countDown() { //CountDow ...

  9. 福州大学W班-需求分析评分排名

    作业链接 https://edu.cnblogs.com/campus/fzu/FZUSoftwareEngineering1715W/homework/1019 作业要求 1.需求文档 1) 参考& ...

  10. beta冲刺7

    前言:最后一篇惹.明天就是正式交差了.有点慌-- 昨天的未完成: 用户试用+测评 输入部分的正则式判定 今天的工作: 登陆界面修改 我的社团显示效果优化 部分信息注册后锁定无法修改 其他部分功能优化 ...