1. Two Sum

0. Java

  • 使用 map 存储,判断 target-num 是否在 map 中(需要遍历两次数组):

     public int[] twoSum(int[] nums, int target) {Map<Integer, Integer> valueIdxMap = new HashMap<>();int[] idx = new int[2];for (int i = 0; i < nums.length; ++i) {valueIdxMap.put(nums[i], i);}for (int i = 0; i < nums.length; ++i) {int other = target - nums[i];if (valueIdxMap.containsKey(other)) {int pos = valueIdxMap.get(other);if (pos == i) {continue;}idx[0] = i;idx[1] = pos;return idx;}}throw new IllegalArgumentException("No solution");
    }
    
  • 仍然使用 map,不过这次 map中存放的是元素的补,此时仅需要遍历数组一次:

    public int[] twoSum(int[] nums, int target) {
    Map<Integer, Integer> complementary = new HashMap<>();
    int[] result = new int[2];
    for (int i = 0; i < nums.length; ++i) {if (!complementary.containsKey(nums[i])) {complementary.put(target-nums[i], i);}else {result[0] = complementary.get(nums[i]);result[1] = i;return result;}
    }
    throw new IllegalArgumentException("No solution");
    }
    

1. Python

class Solution(object):def twoSum(self, nums, target):d = defaultdict(int)for i, num in enumerate(nums):if num not in d:d[target-num] = ielse:return [d[num], i]

这里所使用的字典 d,建立 值 与 与其关于 target 成互补关系的值的下标;

leetcode —— 数组(1. Two Sum)相关推荐

  1. python sum 数组原理_Python – Sum 4D数组

    你可以使用 einsum: In [21]: np.einsum('ijkl->kl', M) Out[21]: array([[32, 8], [16, 8]]) 其他选项包括将前两个轴重新整 ...

  2. 【DP】LeetCode 64. Minimum Path Sum

    LeetCode 64. Minimum Path Sum Solution1:标准的动态规划题目 class Solution { public:int minPathSum(vector<v ...

  3. C#LeetCode刷题之#167-两数之和 II - 输入有序数组(Two Sum II - Input array is sorted)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3903 访问. 给定一个已按照升序排列 的有序数组,找到两个数使得 ...

  4. [LeetCode]Count of Range Sum

    题目:Count of Range Sum Given an integer array nums, return the number of range sums that lie in [lowe ...

  5. leetcode数组相关简单习题,玉米迪迪的刷题之旅(*╹▽╹*)

    好啦好啦!我今天开始重鼓信心刷题啦!也不知道怎么回事一道简单的数组题开始有了思路,却总是写不出来,但是也就是看了别的大佬的思路以及自己的回忆,突然发现!这个题的做法我的老师讲过!一下子又发现我回忆起来 ...

  6. LeetCode数组高频题目整理

    刷题是应届生找工作不可缺少的部分,一种公认的刷题策略是按类别刷题,可是每个类别也有许多题,在有限的时间里到底该刷哪些题呢?个人根据LeetCode官方给出的每个题目的出现频率,整理并收录了每个类别里高 ...

  7. leetcode解题方案--015--3 sum

    题目 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  8. LeetCode Partition Equal Subset Sum(动态规划)

    问题:给出一个数组,问是否可以分成两个子集,其和相等 思路:首先要求数组和是偶数,然后用动态规划来求. 用dp(i,j)表示第i步时和为j是否可行,则有dp(i,j)= dp(i-1,j) || dp ...

  9. LeetCode 64. Minimum Path Sum(最小和的路径)

    Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...

最新文章

  1. Linux wine
  2. 《大型网站技术架构》5、6、7章阅读笔记
  3. Repeater的嵌套结合用户控件的使用
  4. make zImage和make uImage的区别和mkimage工具的使用
  5. [Spark]PySpark入门学习教程---RDD介绍(2)
  6. Wildfly 8.0通过其JAXRS 2.0实现提供了无缝的JSON支持。
  7. mysql的unique_MySQL使用UNIQUE实现数据不重复插入
  8. 易经读书笔记18山风蛊
  9. python进阶(一)关联sql的算法操作
  10. C++封装SQLite实例六
  11. 我来做百科(第八天)
  12. 共青城市大力推进国家智慧城市试点建设
  13. c# List 里面的Linq方法
  14. 全国计算机三级嵌入式资料
  15. UniWebView3 使用中遇到的坑
  16. 万年历的c语言程序代码含节假日,用C语言编写的万年历程序代码
  17. mac打开网页速度特别慢
  18. 微信使用篇 - 如何在订阅号与服务号之间做出选择
  19. 集成测试最全详解,看完必须懂了
  20. 四阶代数余子式怎么求_四阶行列式的计算-四阶行列式详细的计算

热门文章

  1. 九、索引与执行计划、索引的分类
  2. 详解:离线项目二 每个区域下最受欢迎的产品TOPN
  3. oracle全角括号忽略,Oracle中SQL查询语句日文不区分全角/半角
  4. java 字符串递归_关于java:递归函数以通配符模式匹配字符串
  5. Linux内核 触摸板,Linux下关闭触摸板和触摸杆
  6. Python数据结构实战——数(Tree)
  7. Python——数组(列表)的基本操作
  8. tensorflow之XORerr1例题
  9. React技术栈梳理
  10. 删除一个带有文件的文件夹