刚开始学习Python编程,欢迎交流学习!

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.

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

Note:

n is a positive integer, which is in the range of [1, 10000].

All the integers in the array will be in the range of [-10000, 10000].

思路:由题目示例可以看出输出只要为最终的和可以了,所以不一定非得先将一个大的数组(列表)中的数据先挨个化成分开的小数组(列表),正确理解题目意思,可以输出结果就行了。所以直接对原数组(列表)排序(Python自带的函数),然后取偶数项相加就可以了。

    class Solution:def arrayPairSum(self, nums):""":type nums: List[int]:rtype: int"""nums.sort()return sum(nums[::2])

561.Array Partition I--Python相关推荐

  1. Leetcode PHP题解--D14 561. Array Partition I

    561. Array Partition I 题目链接 561. Array Partition I 题目分析 本题给了一个数组,要求将数组分为n个只有2个元素的一对. 使得每对数字中最小的数加起来的 ...

  2. Leetcode刷题记录[java]——561 Array Partition I

    一.前言 二.题561 Array Partition I Given an array of 2n integers, your task is to group these integers in ...

  3. [LeetCode]561. Array Partition I (数组分区 1)

    561. Array Partition I Given an array of 2n integers, your task is to group these integers into n pa ...

  4. LeetCode 561. Array Partition I

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

  5. LeetCode 561 Array Partition I(数组划分)

    翻译 原文 Given an array of 2n integers, your task is to group these integers into n pairs of integer, s ...

  6. leetcode#561 Array Partition I

    Description: Given an array of 2n integers, your task is to group these integers into n pairs of int ...

  7. 【刷leetcode,拿Offer-009】561. Array Partition I(贪心,C++)

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

  8. 561. Array Partition I

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

  9. 561. 数组拆分 I(javascript)561. Array Partition I

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

最新文章

  1. 微软私有云系列----域服务器准备
  2. Linux 创建yum源和软件仓库实例
  3. SAP WM 有无保存WM Level历史库存的Table?
  4. Python错误“ImportError: No module named MySQLdb”解决方法
  5. [Java并发编程实战] 共享对象之可见性
  6. 39道高级java面试题及答案(最新)
  7. C/C++学习之路: 多态
  8. OpenCV学习——形态学
  9. 取文字_把这4个字母输入word,会得到一段神秘文字,承载着一段历史
  10. C++学习之路 | PTA乙级—— 1039 到底买不买 (20 分)(精简)
  11. 华为云跻身Gartner报告中国三强,预示云计算市场的未来变局?
  12. python等待用户输入_Python等待时间,等待用户输入
  13. UDP和TCP 简单 区别
  14. vscode下搭建vue项目
  15. Oracle多层ifend嵌套,Excel IF函数以及IF多层嵌套如何使用,看完你就知道了
  16. 阿里云centos7.0安装java环境
  17. 谈谈时间管理--陶哲轩
  18. C语言中 1%3,算术什么意思啊 算数什么意思
  19. 织梦如何添html5播放器,织梦ckplayer视频播放器插件的介绍与使用
  20. 多啦a梦的python代码_分享一篇文本文档画哆啦A梦代码!

热门文章

  1. c语言中怎么调用自己定义的函数,c语言中怎么调用自己定义的函数?
  2. 12个基本的在线设计竞赛目录
  3. 百度变更使命后首个政府AI合作落地:在李彦宏老家
  4. 使用selenium和phantomjs解决爬虫中对渲染页面的爬取
  5. python图像处理教程,【图像处理】使用OpenCV+Python进行图像处理入门教程(二)...
  6. Stay hungry, Stay young
  7. 【计算机网络】计算机网络基础知识(三次握手,四次挥手,OSI七层网络模型)
  8. 一家之言:de_nuke全面分析
  9. jsp物流信息发布管理平台
  10. uniapp 小程序 加载显示插屏广告