码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • 【LeetCode】two num·两数之和


    题目描述

    英文版描述

    Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice. You can return the answer in any order.

    英文版地址

    leetcodeicon-default.png?t=M5H6https://leetcode.com/problems/two-sum/​leetcode.com/problems/two-sum/

    中文版描述

    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组中同一个元素在答案里不能重复出现。 你可以按任意顺序返回答案。

    示例 1:

    输入:nums = [2,7,11,15], target = 9

    输出:[0,1]

    解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] 。

    示例 2:

    输入:nums = [3,2,4], target = 6

    输出:[1,2]

    示例 3:

    输入:nums = [3,3], target = 6

    输出:[0,1]

    提示

    2 <= nums.length <= 104

    -109 <= nums[i] <= 109

    -109 <= target <= 109

    只会存在一个有效答案

    中文版地址

    力扣icon-default.png?t=M5H6https://leetcode.cn/problems/two-sum/

    解题思路

    遍历数组,每获取到一个值,再去遍历他后面的值,看是否有值等于目标值减去当前值

    解题方法

    俺这版

    1. class Solution {
    2. public int[] twoSum(int[] nums, int target) {
    3. int[] result = new int[2];
    4. for(int i=0; i<nums.length; i++){
    5. int cur = nums[i];
    6. int needed = target-cur;
    7. for(int j=(nums.length-1); j>i; j--){
    8. if(nums[j] == needed){
    9. result[0] = i;
    10. result[1] = j;
    11. return result;
    12. }
    13. }
    14. }
    15. return result;
    16. }
    17. }

    官方版

    查找表法

    1. class Solution {
    2. public int[] twoSum(int[] nums, int target) {
    3. Map<Integer,Integer> map = new HashMap<>(nums.length);
    4. map.put(nums[0], 0);
    5. for(int i=1;i<nums.length;i++){
    6. int cur = nums[i];
    7. int needed = target - cur;
    8. if(map.containsKey(needed)){
    9. return new int[]{map.get(needed),i};
    10. }else{
    11. map.put(cur,i);
    12. }
    13. }
    14. throw new IllegalArgumentException("Can not find!");
    15. }
    16. }

  • 相关阅读:
    树莓派ubuntu上配置miniconda并创建虚拟环境
    【IVA】一个开源OPENIVAS智能分析系统的思考
    二叉树汇总
    北漂七年拿过阿里、腾讯、华为offer的资深架构师,分享经验总结
    电脑重装系统后Win11底部任务栏大小调整方法
    c语言实现数据结构中的队列
    基于matlab寻找并显示一维数组t中的素数
    shell脚本—— case语句+函数
    Springboot连接两个数据库
    this指向、bind()、call()、apply()
  • 原文地址:https://blog.csdn.net/aqin1012/article/details/125432139
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号