前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >剑指offer | 面试题43:和为s的两个数字

剑指offer | 面试题43:和为s的两个数字

作者头像
千羽
发布2022-02-23 15:39:02
2510
发布2022-02-23 15:39:02
举报
文章被收录于专栏:程序员千羽程序员千羽

死磕算法系列文章

  1. 干货 | 手撕十大经典排序算法
  2. 剑指offer | 认识面试
  3. 剑指offer | 面试题2:实现Singleton模式
  4. 剑指offer | 面试题3:二维数组的查找
  5. 剑指offer | 面试题4:替换空格
  6. 剑指offer | 面试题5:从尾到头打印链表
  7. 剑指offer | 面试题6:重建二叉树
  8. 剑指offer | 面试题7:用两个栈实现队列
  9. 剑指offer | 面试题8:旋转数组的最小数字
  10. 剑指offer | 面试题9:斐波那契数列
  11. 剑指offer | 面试题10:青蛙跳台阶问题
  12. 剑指offer | 面试题11:矩阵覆盖
  13. 剑指offer | 面试题12:二进制中1的个数
  14. 剑指offer | 面试题13:数值的整数次方
  15. 剑指offer | 面试题14:打印从1到最大的n位数
  16. 剑指offer | 面试题15:删除链表的节点
  17. 剑指offer | 面试题16:将数组中的奇数放在偶数前
  18. 剑指offer | 面试题17:链表中倒数第k个节点
  19. 剑指offer | 面试题18:反转链表
  20. 剑指offer | 面试题19:合并两个有序链表
  21. 剑指offer | 面试题20:判断二叉树A中是否包含子树B
  22. 剑指offer | 面试题21:二叉树的镜像
  23. 剑指offer | 面试题22:顺时针打印矩阵
  24. 剑指offer | 面试题23:包含min函数的栈
  25. 剑指offer | 面试题24:栈的压入、弹出序列
  26. 剑指offer | 面试题25:从上到下打印二叉树
  27. 剑指offer | 面试题26:二叉搜索树的后序遍历序列
  28. 剑指offer | 面试题27:二叉树中和为某一值的路径
  29. 剑指offer | 面试题28:复杂链表的复制
  30. 剑指offer | 面试题29:二叉搜索树转换为双向链表
  31. 剑指offer | 面试题30:字符串的排列
  32. 剑指offer | 面试题31:数组中出现次数超过一半的数字
  33. 剑指offer | 面试题32:最小的k个数
  34. 剑指offer | 面试题33:连续子数组的最大和
  35. 剑指offer | 面试题34:1~n 整数中 1 出现的次数
  36. 剑指offer | 面试题35:把数组排成最小的数
  37. 剑指offer | 面试题36:丑数
  38. 剑指offer | 面试题37:第一个只出现一次的字符
  39. 剑指offer | 面试题38:数组中的逆序对
  40. 剑指offer | 面试题39:两个链表的第一个公共节点
  41. 剑指offer | 面试题40:数组中数字出现的次数
  42. 剑指offer | 面试题41:二叉树的深度
  43. 剑指offer | 面试题42:平衡二叉树

Leetcode : https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/

GitHub : https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_43_twoSum/Solution.java

剑指 Offer 57 - I. 和为s的两个数字

题目描述 :输入一个递增排序的数组和一个数字s,在数组中查找两个数,使得它们的和正好是s。如果有多对数字的和等于s,则输出任意一对即可。 难度:简单

示例 1:

代码语言:javascript
复制
输入:nums = [2,7,11,15], target = 9
输出:[2,7] 或者 [7,2]

示例 2:

代码语言:javascript
复制
输入:nums = [10,26,30,31,47,60], target = 40
输出:[10,30] 或者 [30,10]

方法一:双指针

算法流程:

  1. 初始化:双指针i, j分别指向数组nums的左右两端(俗称对撞双指针) 。
  2. 循环搜索:当双指针相遇时跳出;
    1. 计算和s = nums[i] + nums[j] ;
    2. 若s > target,则指针j向左移动,即执行j=j- 1 ;
    3. 若s < target,则指针i向右移动,即执行i=i+1 ;
    4. 若s = target,立即返回数组[nums[i], nums[[j]] ;
  3. 返回空数组,代表无和为target的数字组合。

复杂度分析:

  • 时间复杂度O(N) : N为数组nums的长度;双指针共同线性遍历整个数组。
  • 空间复杂度0(1) :量i, j使用常数大小的额外空间。
代码语言:javascript
复制
package com.nateshao.sword_offer.topic_43_twoSum;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @date Created by 邵桐杰 on 2022/1/25 8:23
 * @微信公众号 千羽的编程时光
 * @个人网站 www.nateshao.cn
 * @博客 https://nateshao.gitee.io
 * @GitHub https://github.com/nateshao
 * @Gitee https://gitee.com/nateshao
 * Description:
 */
public class Solution {
    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        int[] result = twoSum(nums, target);
        for (int i : result) {
            System.out.println("i1 = " + i);// i1 = 7   i1 = 2
        }
    }
    /**
     * 双指针 O(1)
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum2(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        while (left < right) {
            int cur = nums[left] + nums[right];
            if (cur == target) return new int[]{nums[left] , nums[right]};
            else if (cur > target) right--;
            else left++;
        }
        return new int[]{};
    }
}

方法二:HashMap,HashSet

使用hash表一次遍历,时间复杂度O(N),空间复杂度O(N),

代码语言:javascript
复制
package com.nateshao.sword_offer.topic_43_twoSum;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * @date Created by 邵桐杰 on 2022/1/25 8:23
 * @微信公众号 千羽的编程时光
 * @个人网站 www.nateshao.cn
 * @博客 https://nateshao.gitee.io
 * @GitHub https://github.com/nateshao
 * @Gitee https://gitee.com/nateshao
 * Description:
 */
public class Solution {
    public static void main(String[] args) {
        int[] nums = {2, 7, 11, 15};
        int target = 9;
        int[] result = twoSum(nums, target);
        for (int i : result) {
            System.out.println("i1 = " + i);
        }
        int[] twoSum3 = twoSum3(nums, target);
        for (int i : twoSum3) {
            System.out.println("i3 = " + i);
        }

    }

    /**
     * HashSet O(n)
     *
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum(int[] nums, int target) {
        Set<Object> set = new HashSet<>();
        for (int num : nums) {
            if (!set.contains(target - num)) {
                set.add(num);
            } else {
                return new int[]{num, target - num};
            }
        }
        return new int[]{};
    }
    /**
     * HashMap
     * @param nums
     * @param target
     * @return
     */
    public static int[] twoSum3(int[] nums, int target) {
        HashMap<Integer, Integer> hashMap = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            hashMap.put(nums[i],i);
        }

        int[] result = new int[2];
        for (int num : nums) {
            int value =  hashMap.getOrDefault((target-num),-1);
            // System.out.println("key Three 对应的 value: " + value);
            if (value!= -1 && value != hashMap.get(num)) {
                result[0] = num;
                result[1] = target-num;
            }
        }
        return result;
    }
}

参考链接:https://leetcode-cn.com/problems/he-wei-sde-liang-ge-shu-zi-lcof/solution/mian-shi-ti-57-he-wei-s-de-liang-ge-shu-zi-shuang-/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2022-01-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 千羽的编程时光 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 剑指 Offer 57 - I. 和为s的两个数字
  • 方法一:双指针
  • 方法二:HashMap,HashSet
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档