[DP]1.two sum

本文介绍了一种解决“两数之和”问题的简单Python实现方法,通过使用内置函数index()来查找目标值,同时提供了map()和enumerate()两个实用函数的示例。

题目描述:

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].


这道题主要用的还是Python的内置函数index()

class Solution:
    def twoSum(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: List[int]
        """
        re = [0,0]
        for i in range(len(nums)):
            if (target - nums[i]) in nums[i+1:]:
                re[0] = i
                re[1] = nums[i+1:].index(target-nums[i])+(i+1)
                return re

我觉得这个方法还是挺简单的。就是效率低,不过代码短。又好理解。

这里再记录两个Python里的常用函数:
map(f,nums)
第一个参数是一个函数f,作用在num的每一个元素上。
>>> e = list(range(10))
>>> def f(num):
...     return num+2
...
>>> list(map(f,e))
[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
>>>
第二个函数是enumerate(nums),可以将Index和item一起输出
>>> list1 = [10,11,12,13,14]
>>> for index,item in enumerate(list1):
...     print(index,item)
... 
0 10
1 11
2 12
3 13
4 14
>>> 


 
乐播投屏是一款简单好用、功能强大的专业投屏软件,支持手机投屏电视、手机投电脑、电脑投电视等多种投屏方式。 多端兼容与跨网投屏:支持手机、平板、电脑等多种设备之间的自由组合投屏,且无需连接 WiFi,通过跨屏技术打破网络限制,扫一扫即可投屏。 广泛的应用支持:支持 10000+APP 投屏,包括综合视频、网盘与浏览器、美韩剧、斗鱼、虎牙等直播平台,还能将央视、湖南卫视等各大卫视的直播内容一键投屏。 高清流畅投屏体验:腾讯独家智能音画调校技术,支持 4K 高清画质、240Hz 超高帧率,低延迟不卡顿,能为用户提供更高清、流畅的视觉享受。 会议办公功能强大:拥有全球唯一的 “超级投屏空间”,扫码即投,无需安装。支持多人共享投屏、远程协作批注,PPT、Excel、视频等文件都能流畅展示,还具备企业级安全加密,保障会议资料不泄露。 多人互动功能:支持多人投屏,邀请好友加入投屏互动,远程也可加入。同时具备一屏多显、语音互动功能,支持多人连麦,实时语音交流。 文件支持全面:支持 PPT、PDF、Word、Excel 等办公文件,以及视频、图片等多种类型文件的投屏,还支持网盘直投,无需下载和转格式。 特色功能丰富:投屏时可同步录制投屏画面,部分版本还支持通过触控屏或电视端外接鼠标反控电脑,以及在投屏过程中用画笔实时标注等功能。
题目: Problem Statement There are N houses numbered from 1 to N on a number line. House i is located at coordinate X i ​ . Multiple houses may be located at the same coordinate. You place M base stations at arbitrary real coordinates on the number line. Then, you set a non-negative integer signal strength for each base station. When the signal strength of a base station is set to x, The signal from that base station reaches a house if and only if the distance between the base station and the house is at most 2 x ​ . Particularly, when x=0, the signal reaches only houses located at the same coordinate as the base station. Find the minimum possible sum of signal strengths when the positions and signal strengths of the base stations are set such that at least one base station's signal reaches every house. It can be proved that the answer is an integer for any input satisfying the constraints. Constraints 1≤M≤N≤5×10 5 1≤X i ​ ≤10 12 ( 1≤i≤N) All input values are integers. Input The input is given from Standard Input in the following format: N M X 1 ​ … X N ​ Output Output the answer as an integer in one line. Sample Input 1 Copy 7 3 5 10 15 20 8 14 15 Sample Output 1 Copy 6 By placing three base stations as follows, signals reach all houses. Place a base station with signal strength 5 at coordinate 7.5. This base station reaches houses 1,2,5. Place a base station with signal strength 1 at coordinate 14.5. This base station reaches houses 3,6,7. Place a base station with signal strength 0 at coordinate 20. This base station reaches house 4. The sum of signal strengths in this case is 6. It is impossible to satisfy the condition with an arrangement where the sum of signal strengths is smaller than 6, so output 6. 如何用c++在1s内解决
07-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值