LeetCode 学习笔记:Question 1、Two Sum

UniTouch:多模态触觉对齐技术如何重塑零样本跨模态应用 UniTouch模型通过创新的多模态触觉对齐技术,成功将触觉数据映射到通用的视觉-语言嵌入空间,解决了触觉模态的“孤岛困境”。该技术利用对比学习和传感器特定标记,实现了高质量的零样本学习能力,使模型无需额外训练即可完成触觉分类、描述等任务,并能驱动跨模态生成,为机器人、VR和智能制造等领域提供了强大的触觉理解新范式。 阅读详情

Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number.

The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

标签:  Array Hash Table

(以下仅是自己记录学习)

解析:

1、可以复制原数组并排序,对排序后的数组两头start和end指针同时扫描,找到结果后再在原数组中找到下标,

   时间复杂度取决于数组排序O(N lg N)时间

<span style="font-family:Microsoft YaHei;">    public int[] twoSum(int[] numbers, int target) {
    	int[] result = new int[2];
    	int[] data = new int[numbers.length];
    	
    	for(int i=0; i<numbers.length; i++) {
    		data[i] = numbers[i];
    	}
    	
    	Arrays.sort(data);
    	
    	int start = 0;
    	int end = data.length - 1;
    	while(start < end) {
    		if(data[start] + data[end] > target) {
    			end --;
    		} else if(data[start] + data[end] < target) {
    			start ++;
    		} else {
    	    	int k = 0;
    	    	for(int i=0; i<numbers.length && k < 2; i++) {
    	    		if(numbers[i] == data[start] || numbers[i] == data[end]) {
    	    			result[k++] = i + 1;
    	    		}
    	    	}
    			break;
    		}
    	}
    	return result;
    }</span>


2、使用hashmap记录数组元素的值与下标记录,则只需要O(N)时间

 public int[] twoSum2(int[] data, int target) {
    	int[] result = new int[2];
    	boolean flag = false;
    	HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    	for(int i=0; i<data.length; i++) {
    		if(map.get(data[i]) != null) {
    			if(target == (data[i] * 2)) {
    				result[0] = i + 1;
        			result[1] = map.get(data[i]) + 1;
        			flag = true;
        			break;
    			}
    		} 
    		map.put(data[i], i);
    	}
    	if(flag == false) {
        	for(int i=0; i<data.length; i++) {
        		if(map.containsKey(target - data[i]) && map.get(target - data[i]) != i) {
        			result[0] = i + 1;
        			result[1] = map.get(target - data[i]) + 1;
        			break;
        		}
        	}
    	}
    	if(result[0] > result[1]) {
    		int tmp = result[0];
    		result[0] = result[1];
    		result[1] = tmp;
    	}
    	return result;
    }

解决RocketMQ中@Bean配置DefaultMQProducer时的MQClientException问题 本文针对Spring Boot整合RocketMQ时,因在@Bean配置中手动调用DefaultMQProducer的start()方法而引发的MQClientException问题,深入剖析了其根本原因在于生命周期管理冲突。文章提供了简洁有效的解决方案——移除手动启动,让Spring容器自动管理,并给出了完整的配置示例与生产环境最佳实践。 阅读详情

相关推荐

谐振电路原理与应用场景全解析

本文全面解析了谐振电路的原理、类型及其核心应用。详细阐述了串联谐振与并联谐振的工作特性,并重点介绍了其在无线通信调谐、音频处理分频以及电力系统滤波与无功补偿等关键场景中的实际作用。通过公式推导与实例分析,帮助读者深入理解LC谐振的选频与能量交换机制。

t1u2v的博客 836

LeetCode Question Difficulty Distribution : Sheet1

转自:https://docs.google.com/spreadsheet/pub?key=0Aqt--%20wSNYfuxdGxQWVFsOGdVVWxQRlNUVXZTdEpOeEE&output=html LeetCode Question Difficulty Distribution : Sheet1

R_xiaozhu_Q的专栏 8095

STM32F407驱动OV2640摄像头:从SCCB协议到I2C模拟的保姆级避坑指南

本文详细介绍了STM32F407驱动OV2640摄像头的完整流程,重点解析SCCB协议与I2C的关键差异,并提供GPIO模拟实现方案。通过实战案例和调试技巧,帮助开发者避开常见陷阱,实现稳定的图像采集功能。

weixin_28700411的博客 345

N sum Question

N sum Question 2 sum, 3 sum and 3 Sum closet 4 sum - > K sum

Xiaomao要学coding 416

开源 AHB不对称半桥反激电路设计(六)死区时间,谐振腔电流,谐振电感函数表达式

电流跌落可以使副边电流峰值和有效值降低,减小损耗,但如果开关管 S2 导通期间,谐振电流谐等于励磁电流,即副边电流A 点降至 0,会在原边变压器内形成环流,产生损耗,同时也不利于副边同步整流管驱动的正常工作,为了保证开关管 S2 导通期。在开关管 S1 和开关管 S2 均关断的死区时间内,谐振电流给原边开关管结电容和副边整流管结电容充放电,因此会引起开关 S1 关断瞬间电流跌落与开关 S2 关断瞬间电流震荡现象,受寄生参数影响原边谐振电流波形和副边电流波形如图。降到最低,变压器以最高效率向副边传递能量。

【Simon】的博客 1225

51单片机——LCD12864驱动函数深度解析与实战优化

本文深入解析了51单片机驱动LCD12864液晶屏的核心驱动函数,从底层时序、关键延时到地址计算进行逐行剖析,并针对常见显示问题提供系统性的调试技巧与实战优化方案。重点探讨了如何将基础代码优化为稳定高效的驱动,涵盖延时精准化、批量写入及显示缓冲区等高级技巧,旨在帮助开发者从“点亮”屏幕进阶到“精通”驱动,确保显示稳定可靠。

weixin_29190169的博客 84

Leetcode 1: Two Sum

LeetCode上面的难度为easy的一道题,作为进入Leetcode的第一道题,做下来比较顺利,不过跑的结果不好,原因在于算法的复杂度高,下面给出做这道题遇到的问题 问题描述 ## 解法 根据题目很容易得出第一种解法,直接暴力求解即可 遍历数组,找出符合要求的两个数,并返回 以下是C++代码 #include &amp;lt;vector&amp;gt; using names...

huangbx_tx的博客 178

第 3 天:Actor 是什么?创建你的第一个游戏对象!

本文详细介绍 Actor 在 Unreal Engine 5 中的作用,并通过 C++ 创建你的第一个游戏对象。你将学习 Actor 的生命周期(BeginPlay、Tick)、如何添加组件(Static Mesh),并让 Actor 在游戏世界中运作。同时提供 常见问题解决方案,助你快速上手 UE5 C++ 开发!🎮 跟随本教程,构建属于你的第一个游戏对象!

m0_37643873的博客 2629

LeetCode Sum Question

Question 1.两个数求和问题,找出求和等于target的数对,只有一对。 Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly o...

c_living的博客 277

Claude Code 完全指南:从入门到精通

《AI编程助手ClaudeCode深度指南》摘要 ClaudeCode作为新一代AI编程助手,凭借200K tokens的超长上下文窗口和深度代码理解能力,正在重塑开发工作流程。本文系统介绍了从安装配置到高级应用的完整使用指南: 核心优势包括: 项目级代码理解能力 Agent自主规划复杂任务 原生终端集成支持 多文件协同编辑功能 实战应用场景: 快速理解陌生项目架构 诊断性能瓶颈并提供优化方案 自动生成符合规范的API代码 开发可复用技术组件 高级技巧: 通过CLAUDE.md定义项目规范 合理使用/cle

分享AI、区块链技术及应用,未来已来 603

【CANoe】CAPL_ECU上电后首帧报文发出时间检测

【CANoe】CAPL_ECU上电后首帧报文发出时间检测,通过CANoe I/O口检测电压实现

星Yeah 的博客 2507

LeetCode第1题:Two_Sum

Question: 1.Given an array of integers, returnindicesof the two numbers such that they add up to a specific target. 2.You may assume that each input would haveexactlyone solution, and you may no...

weixin_41502937的博客 145

潜望式长焦模组全解析:结构构造、光路设计与工程实战挑战

随着移动影像能力的不断进化,潜望式长焦模组成为旗舰手机实现 5x 甚至 10x 光学变焦的关键配置。相比传统横向堆叠镜组,潜望式结构通过反射棱镜实现纵向空间上的光路折叠,有效解决了模组厚度与变焦行程的矛盾问题。然而,高倍变焦模组不仅面临复杂的光学路径设计,还涉及反射损耗控制、防抖马达配合、模组热应力管控与 ISP 畸变校正等一系列工程难点。本文将结合主流机型的实战经验,深入解析潜望式长焦模组的结构构造与调试路径,为影像系统工程师提供完整的实战参考。

努力分享一些人工智能、计算机视觉、影像等相关的知识干货! 3136

leetcode question 18: 4Sum

问题:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: The solution se...

csdn609387481的博客 223

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

leetcode(E)two sum

Two Sum QuestionEditorial Solution My Submissions Total Accepted: 344929 Total Submissions: 1217901 Difficulty: Easy Contributors: Admin Given an array of integers, return indices of the two nu

Jang1996的博客 360

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode Question Difficulty Distribution

http://zephyrusara.blogspot.jp/2014/07/leetcode-question-difficulty.html LeetCode Question Difficulty Distribution : Sheet1               ID Question D

wolf96的专栏 1380

leetcode_question_112 Path Sum

  Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree an...

weixin_34085658的博客 114

LeetCode1: Top 100 Liked Question / Array / Easy

目录TwoSum解法一解法二解法三 TwoSum 解法一 public static int[] twoSum(int[] array, int t) { int[] c = new int[2]; for(int i=0; i<array.length-1; i++) { for(int j=i+1; j<array.length; j++) { if(array[i]+array[j]==t) { c[0]=i; c[1]=j; }

相扑小猫的博客 339

leetcode_question_113 Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \

ZkvIA的博客 2008

leetcode_question_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 minimizes the sum of all numbers along its path. Note: You can only move either down or righ

ZkvIA的博客 1345

leetcode question 15 : 3Sum

转自:https://www.cnblogs.com/grandyang/p/4481576.htmlGiven an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of...

csdn609387481的博客 164

LeetCode2: Top 100 Liked Question / Array / Medium

目录3Sum解法一Find the Duplicate Number解法一解法二解法三 3Sum 解法一 中心思想:遍历排序好的数组,选定一个数,然后剩下的数组用TwoSum的方法。 TwoSum Arrays.asList使用: 1. 一般使用方法 String[] myArray = { "Apple", "Banana", "Orange" }; List<String> myList = Arrays.asList(myArray); //or List<String> m

相扑小猫的博客 296

leetcode question 113: Path Sum II

问题: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 2...

csdn609387481的博客 227

leetcode question 16: 3Sum Closest

问题:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exac...

csdn609387481的博客 191
上一篇: mysql 基本使用 : 中文乱码问题
下一篇: 【剑指offer】 面试题3: 二维数组中的查找
java_wliang
博客等级 码龄15年 19粉丝 82原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值