RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 1365760
Accepted
alex_t
alex_t
Asked:2022-05-25 15:43:45 +0000 UTC2022-05-25 15:43:45 +0000 UTC 2022-05-25 15:43:45 +0000 UTC

调整算法

  • 772

任务:给定一个整数数组,您需要返回两个数字的索引,其和等于给定数字。我们可以假设数组只有一个解。您不能两次使用相同的号码。

Пример1: [2, 7, 11, 15], 9
Ответ: [0, 1]

Пример2: [3, 2, 4], 6
Ответ: [1, 2]

如果我们假设数组是有序的(这不是条件!),那么我使用两个指针的方法来解决它:

    public int[] twoSum(int[] nums, int target) {
        int l = 0, r = nums.length - 1;

        while (l < r) {
            int currentSum = nums[l] + nums[r];

            if (currentSum == target) {
                return new int[]{l, r};
            } else if (currentSum > target) {
                r--;
            } else if (currentSum < target) {
                l++;
            }
        }

        throw new RuntimeException("the pair is not found");
    }

O(n)复杂度。

但是第二个例子有问题:

Пример2: [3, 2, 4], 6
Ответ: [1, 2]

应该给1.2,我有一个异常崩溃。 告诉我如何在数组无序的情况下更正算法?

java
  • 3 3 个回答
  • 10 Views

3 个回答

  • Voted
  1. MBo
    2022-05-25T17:41:08Z2022-05-25T17:41:08Z

    是的,没有办法解决。要么排序,要么使用另一种算法——在最简单的情况下,用二次时间的两个循环,或者使用哈希表,那么时间是线性的,但是内存是 O(n)

    • 3
  2. Nowhere Man
    2022-05-25T19:10:49Z2022-05-25T19:10:49Z

    如果不考虑排序,那么最简单的解决方案是不使用额外的数据结构,检查嵌套循环:

    public static int[] twoSum(final int target, int ... nums) {
        for (int i = 0, n = nums.length; i < n - 1; i++) {
            int a = nums[i];
            for (int j = i + 1; j < n; j++) {
                int b = nums[j];
                if (target == a + b) {
                    return new int[]{i, j};
                }
            }
        }
        
        throw new RuntimeException("the pair is not found");
    }
    

    是否可以使用哈希表的解决方案(如果输入数组包含重复项,则修复):

    public static int[] twoSum(final int target, int ... nums) {
        System.out.println(Arrays.toString(nums));
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0, n = nums.length; i < n; i++) {
            int a = nums[i];
            int b = target - a;
            if (map.containsKey(a) && map.get(b) > -1) {
                return new int[]{map.get(b), i};
            }
            map.putIfAbsent(a, i); // запомнить меньший индекс
            map.computeIfAbsent(b, k -> -1);
            System.out.println("i=" + i + "; map=" + map);
        }
        
        throw new RuntimeException("the pair is not found");
    }
    

    测试:

    System.out.println("результат 1: " + Arrays.toString(twoSum(6, 2, 2, 3, 5, 1)));
    System.out.println("результат 2: " + Arrays.toString(twoSum(6, 2, 5, 2, 3, 4, 1)));
    
    [2, 2, 3, 5, 1]
    i=0; map={2=0, 4=-1}
    i=1; map={2=0, 4=-1}
    i=2; map={2=0, 3=2, 4=-1}
    i=3; map={1=-1, 2=0, 3=2, 4=-1, 5=3}
    результат 1: [3, 4]
    [2, 5, 2, 3, 4, 1]
    i=0; map={2=0, 4=-1}
    i=1; map={1=-1, 2=0, 4=-1, 5=1}
    i=2; map={1=-1, 2=0, 4=-1, 5=1}
    i=3; map={1=-1, 2=0, 3=3, 4=-1, 5=1}
    результат 2: [0, 4]
    
    • 2
  3. Best Answer
    alex_t
    2022-06-15T22:35:25Z2022-06-15T22:35:25Z

    感谢HashMap提示!这是最终正确的决定(系统接受它):

        public int[] twoSum(int[] nums, int target) {
            Map<Integer, Integer> map = new HashMap<>();
    
            for (int i = 0; i < nums.length; i++) {
                map.put(nums[i], i);
            }
    
            for (int i = 0; i < nums.length; i++) {
                int key = target - nums[i];
    
                if (map.containsKey(key) && i != map.get(key)) {
                    return new int[]{i, map.get(key)};
                }
            }
    
            throw new RuntimeException("the pair is not found");
        }
    
    • 0

相关问题

  • wpcap 找不到指定的模块

  • 如何以编程方式从桌面应用程序打开 HTML 页面?

  • Android Studio 中的 R.java 文件在哪里?

  • HashMap 初始化

  • 如何使用 lambda 表达式通过增加与原点的距离来对点进行排序?

  • 最大化窗口时如何调整元素大小?

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    表格填充不起作用

    • 2 个回答
  • Marko Smith

    提示 50/50,有两个,其中一个是正确的

    • 1 个回答
  • Marko Smith

    在 PyQt5 中停止进程

    • 1 个回答
  • Marko Smith

    我的脚本不工作

    • 1 个回答
  • Marko Smith

    在文本文件中写入和读取列表

    • 2 个回答
  • Marko Smith

    如何像屏幕截图中那样并排排列这些块?

    • 1 个回答
  • Marko Smith

    确定文本文件中每一行的字符数

    • 2 个回答
  • Marko Smith

    将接口对象传递给 JAVA 构造函数

    • 1 个回答
  • Marko Smith

    正确更新数据库中的数据

    • 1 个回答
  • Marko Smith

    Python解析不是css

    • 1 个回答
  • Martin Hope
    Alexandr_TT 2020年新年大赛! 2020-12-20 18:20:21 +0000 UTC
  • Martin Hope
    Alexandr_TT 圣诞树动画 2020-12-23 00:38:08 +0000 UTC
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5