每日一题一一LeetCode1. 两数之和 - 力扣(LeetCode)

news/2025/3/16 2:41:25/

每日一题一一LeetCode1. 两数之和 - 力扣(LeetCode)

leetcode.cn/problems/two-sum/description/?envType=study-plan-v2&envId=top-100-liked" rel="nofollow">1. 两数之和 - 力扣(LeetCode)

本题的要求是给你一个数组,然后让你从中找出两个值,他们的和为target,然后返回这两个数的下标

暴力版本:

直接双层遍历,取两个不同的数组中的元素相加为target,直接返回它的下标。

C++版本:

class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {int i,j;for(i=0;i<nums.size()-1;i++){for(j=i+1;j<nums.size();j++){if(nums[i]+nums[j]==target){return {i,j};}}}return {i,j};}
};

Java版本:

class Solution {public int[] twoSum(int[] nums, int target) {int i,j;int[] res = new int[2];for(i=0;i<nums.length-1;i++){for(j=i+1;j<nums.length;j++){if(nums[i]+nums[j]==target){res[0]=i;res[1]=j;return res;}}}return res;}
}

优化版本:

我们可以创建一个Hash表,遍历nums数组,每次询问Hash表中是否存在(target-nums[i]),如果存在,直接返回下标值即可。如果不存在就当前元素为键,其对应的下标为值放入哈希表中。

C++版本

class Solution {
public:vector<int> twoSum(vector<int>& nums, int target) {unordered_map<int,int> map;vector<int> res;for(int i=0;i<nums.size();i++){if(map.find(target-nums[i])!=map.end()){res.push_back(i);res.push_back(map[target-nums[i]]);return res;}map[nums[i]]=i;}return res;}
};

java版本

import java.util.Arrays;
import java.util.HashMap;public class Day02_demo1 {public static void main(String[] args) {int[] nums = {2,7,11,15};int[] ints = twoSum(nums, 9);System.out.println(Arrays.toString(ints));}public static int[] twoSum(int[] nums, int target){HashMap<Integer,Integer> map = new HashMap<>();for (int i = 0; i < nums.length; i++) {if(map.containsKey(target-nums[i])){return new int[] {map.get(target-nums[i]),i};}map.put(nums[i],i);}throw new IllegalArgumentException();}
}

http://www.ppmy.cn/news/1579462.html

相关文章

从0开始,手搓Tomcat

一、什么是Tomcat Tomcat 是一款开源的、轻量级的 Web 服务器&#xff0c;它不仅能够提供 HTTP 服务&#xff0c;还能够运行 Java Servlet 和 JavaServer Pages&#xff08;JSP&#xff09;。对于许多开发者来说&#xff0c;理解 Tomcat 的目录结构以及如何在该结构中组织应用…

如何使用postman就可以查看base64图片

一、应用场景 纯后端开发&#xff0c;想要知道自己返回的图片数据是否正常返回&#xff0c;使用简单的工具就可以解析 二、postman介绍 Postman 提供直观的图形用户界面&#xff0c;使用户能够轻松构建和发送 HTTP 请求。能够编写脚本&#xff0c;进行自动化测试&#xff0c;是…

Python网络爬虫之requests库的使用方法

requests库是Python中用于发送HTTP请求的一个重要库,在实际应用中,它被广泛用于爬取网页数据、调用API接口等。本节将详细讲解requests库的使用流程,包括发送HTTP请求、携带请求参数、处理服务器响应以及错误处理,帮助读者掌握requests库的基本使用方法。 1. 使用requests库…

Swift 手动导入 RxSwift.xcframework 报错

0x00 问题 The signature of “RxCocoa.xcframework” cannot be validated and may have been compromised. Validation Error: A sealed resource is missing or invalid 0x01 办法 手动修复签名&#xff0c;能 Build 成功&#xff01; 打开终端&#xff0c;重新签名&…

MIFNet (论文阅读笔记)

Frequency-aware robust multidimensional information fusion framework for remote sensing image segmentation 用于遥感图像分割的频率感知鲁棒多维信息融合框架 Junyu Fan a, Jinjiang Li b, Yepeng Liu b, Fan Zhang b 论文地址 代码地址 1. 摘要 遥感图像复杂的三维结…

LVDS系列3:Xilinx的IOBUFDS原语

前面两节讲解了差分转单端的IBUFDS原语和单端转差分的OBUFDS原语&#xff0c;今天来讲一个同时带有两者功能的原语IOBUFDS&#xff1b; 前述的IBUFDS原语只能接收外部差分信号&#xff0c;此时连接管脚为input管脚&#xff0c;OBUFDS只能向外部输出差分信号&#xff0c;此时连接…

vue3 使用 el-popover实现换行

vue3 使用 el-popover实现换行 在<template #default>中填写内容 <!-- 经纬度地图拾取 --><el-popover placement"top-start" :title"$t(message.dataSearch.lonLatPick)" :width"400" trigger"hover"><templat…

解决 React 中的 Hydration Failed 错误

解决 React 中的 Hydration Failed 错误 React 的 服务器端渲染&#xff08;SSR&#xff09;通过在服务器端生成 HTML 并将其发送给客户端&#xff0c;帮助提高页面加载速度和搜索引擎优化&#xff08;SEO&#xff09;。然而&#xff0c;在进行 SSR 后&#xff0c;React 需要进…