广告
返回顶部
首页 > 资讯 > 后端开发 > Python >leetCode
  • 685
分享到

leetCode

leetCode 2023-01-31 08:01:08 685人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

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

two sum

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].

result:

//typescript
var twoSum=function(nums:Array<number>,target:number):Array<number>{
    let len=nums.length;
    let obj={};
    for(let i=0;i<len;i++){
        if(obj[target-nums[i]]!==undefined){
            return [obj[target-nums[i]],i];
        }
        obj[nums[i]]=i;//翻转 key value
    }
}
//python3
def twoSum(nums,target):
    dict={};
    for i in range(len(nums)):
        if target-nums[i] in dict:
            return dict[target-nums[i]],i;
        dict[nums[i]]=i;
        
result=twoSum([1,2,3,-1],0);
print(result);

--结束END--

本文标题: leetCode

本文链接: https://www.lsjlt.com/news/193270.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • leetCode
    two sum Given an array of integers, return indices of the two numbers such that they add up to a specific target. You m...
    99+
    2023-01-31
    leetCode
  • [leetcode]39. Combin
    题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the...
    99+
    2023-01-31
    leetcode Combin
  • python(leetcode)-344
    编写一个函数,其作用是将输入的字符串反转过来。输入字符串以字符数组 char[] 的形式给出。 不要给另外的数组分配额外的空间,你必须原地修改输入数组、使用 O(1) 的额外空间解决这一问题。 你可以假设数组中的所有字符都是 ASCII...
    99+
    2023-01-30
    python leetcode
  • leetcode 3. Longest
    题目:Given a string, find the length of the longest substring without repeating characters. For example, the longest subst...
    99+
    2023-01-31
    leetcode Longest
  • 【leetcode】 46. Permu
    Permutations Given a collection of distinct numbers, return all possible permutations. For example, [1,2,3] hav...
    99+
    2023-01-31
    leetcode Permu
  • leetcode 241. Differ
    题目:Given a string of numbers and operators, return all possible results from computing all the different possible ways t...
    99+
    2023-01-31
    leetcode Differ
  • [LeetCode Python3]5
    在MATLAB中,有一个非常有用的函数 reshape,它可以将一个矩阵重塑为另一个大小不同的新矩阵,但保留其原始数据。 给出一个由二维数组表示的矩阵,以及两个正整数r和c,分别表示想要的重构的矩阵的行数和列数。 重构后的矩阵需要将原始矩...
    99+
    2023-01-31
    LeetCode
  • python(leetcode)-283
    给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少...
    99+
    2023-01-30
    python leetcode
  • leetcode SQL题目
    文章目录 组合两个表第二高的薪水第N高的薪水分数排名连续出现的数字超过经理收入的员工查找重复的电子邮件从不订购的客户部门工资最高的员工部门工资前三高的所有员工删除重复的电子邮箱上升的温度游戏玩法分析Ⅰ游戏玩法Ⅳ 组合两个表 SE...
    99+
    2023-08-30
    leetcode sql 算法
  • python(leetcode)-1.两
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums ...
    99+
    2023-01-30
    python leetcode
  • python(leetcode)-14最
    编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 ""。 示例 1: 输入: ["flower","flow","flight"] 输出: "fl" 示例 2: 输入: ["dog","racec...
    99+
    2023-01-30
    python leetcode
  • python(leetcode)-66加
    给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 你可以假设除了整数 0 之外,这个整数不会以零开头。 示例 1: 输入: [1,2,3] 输出: ...
    99+
    2023-01-30
    python leetcode
  • Java实现LeetCode(报数)
    题目如下: public String countAndSay(int n) { if(n == 1){ return "1"; ...
    99+
    2022-11-12
  • Python Leetcode 字符串中
    利用到了python中字典的collections.Counter()函数 collections中函数Counter的使用和用法:   counter工具用于支持便捷和快速地计数,   from collections import C...
    99+
    2023-01-30
    字符串 Python Leetcode
  • python leetcode 字符串相
    给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。 示例 1: 输入: num1 = "2", num2 = "3" 输出: "6" 示例 2: 输入: num...
    99+
    2023-01-30
    字符串 python leetcode
  • Leetcode 1:两数之和
    给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = ...
    99+
    2023-01-31
    之和 Leetcode
  • C++如何实现LeetCode
    这篇文章给大家分享的是有关C++如何实现LeetCode的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。[LeetCode] Reverse Linked List 倒置链表Reverse a singly lin...
    99+
    2023-06-20
  • C++怎么实现LeetCode
    这篇文章主要介绍“C++怎么实现LeetCode”,在日常操作中,相信很多人在C++怎么实现LeetCode问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”C++怎么实现LeetCode”的疑惑有所帮助!接下来...
    99+
    2023-06-20
  • LeetCode算法题python解法:
    英文题目:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to d...
    99+
    2023-01-30
    解法 算法 LeetCode
  • vscode刷acm、leetcode的题目
    目录简介编译器 Windows使用 Code Runner 插件运行代码使用 C/C++ 插件编译并调试安装插件配置编译配置 GDB/LLDB 调试器开始调试代码简介 Vi...
    99+
    2022-11-12
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作