LeetCode 学习笔记:Question 1、Two Sum

Matplotlib字体警告终结者:为什么总提示DejaVu Sans?5种场景解决方案大全 本文深入解析Matplotlib中常见的DejaVu Sans字体警告问题,并提供5种典型场景的完整解决方案。从理解字体系统的三层架构和缓存机制,到解决中文显示、远程服务器部署、TeX渲染、缓存优化及生产环境配置等实际问题,帮助开发者彻底摆脱字体警告困扰,实现稳定可靠的数据可视化。 阅读详情

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;
    }

高通平台Android稳定性调试笔记:手把手教你用T32、Crash Utility分析Kernel Panic与RAM Dump 本文详细介绍了高通平台Android内核崩溃的分析方法,包括使用T32 Simulator和Crash Utility工具链解析Kernel Panic与RAM Dump。从环境配置、dump文件预处理到高级调试技巧,手把手教你如何定位和解决Android系统崩溃问题,特别适用于MSM/SA8155平台的开发者。 阅读详情

相关推荐

共聚焦显微镜的光源、工作原理与选型

共聚焦显微镜作为科研与工业领域中重要的微观成像工具,凭借光源的精准适配性、光学切片技术的三维成像能力,以及对不同工业场景的定制化适配潜力,为半导体检测、工业部件分析、智能制造升级提供了关键技术支撑。深入理解其光源特性、工作原理与选型逻辑,能帮助工程师与技术人员精准匹配设备与生产需求,既保障微观检测的精度与效率,又为产业优化、技术创新筑牢微观观测的基础。

PhotonixBay的博客 1551

LeetCode Question Difficulty Distribution : Sheet1

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

R_xiaozhu_Q的专栏 8096

保姆级教程:用STM32H743+TJA1042T玩转FDCAN,CubeMX配置1M/5M双速率通信(附完整代码)

本文提供STM32H743与TJA1042T实现FDCAN双速率通信的保姆级教程,详细讲解CubeMX配置1M/5M双速率通信的全过程,包括硬件连接、时钟树配置、过滤器设置及中断管理,并附完整代码示例,助力开发者快速掌握FDCAN高速通信技术。

weixin_30698297的博客 482

N sum Question

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

Xiaomao要学coding 417

【MFRC522】STM32与MFRC522的SPI通信优化及常见问题解析

本文深入解析了STM32与MFRC522进行SPI通信的优化方法与常见问题。从基础的硬件连接到SPI时序优化,再到关键的寄存器配置(如RFCfgReg设为0x4F以提升读卡稳定性),文章提供了详细的调试技巧和实战排坑指南,帮助开发者实现稳定高效的IC读卡应用。

yhn45678901的博客 848

告别Python依赖!用C++编译PaddleOCR打造轻量级本地OCR工具(附模型选择与性能对比)

本文详细介绍了如何将PaddleOCR从Python迁移到C++平台,打造高性能、低资源占用的本地OCR工具。通过对比Python和C++实现的性能差异,展示了C++在冷启动时间、内存占用和部署便捷性上的显著优势。文章还提供了模型选择、编译配置和优化技巧等实用指南,帮助开发者轻松构建轻量级OCR解决方案。

weixin_28717467的博客 261

Leetcode 1: Two Sum

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

huangbx_tx的博客 178

STM32G474的HRTIM实战:用CubeMX配置互补PWM驱动全桥电路(附移相算法代码)

本文详细介绍了如何使用STM32G474的HRTIM高分辨率定时器,通过CubeMX配置互补PWM驱动全桥电路,并附移相算法代码。内容涵盖HRTIM硬件架构解析、CubeMX配置全流程、移相控制算法实现及调试技巧,为数字电源和电机控制领域提供实用解决方案。

weixin_27017211的博客 491

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的博客 278

人工智能之数学基础:弄懂数学本质,让我们不再害怕数学

数学的用途远远超过了人们的想象,甚至无处不在,任何领域都需要用到数学知识,人工智能也不例外。我将尽可能地将数学知识和机器学习算法相结合,让大家知道这些数学知识咋人工智能领域中的应用,使得数学的学习不会过于枯燥,同时也帮助大家扫清在学习算法过程中的数学障碍。除此之外,我还希望帮助大家建立一种不一样的数学思维,不再将数学看成是符号的演算、公式的推导,而是将数学看成是一种描述这个世界的工具,这样你就会发现这个世界处处有数学。当我们面对问题的时候,你是否会对问题进行抽象,然后通过数学来解决问题?

huanfeng_AI的博客 637

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

图像处理:张量分解在图像处理领域的应用

随着计算机视觉和图像处理技术的快速发展,图像数据呈现出高维、多模态、海量等特点。传统的矩阵分解方法已经无法有效处理这些复杂的图像数据。张量分解作为一种高阶矩阵分解方法,可以很好地捕捉图像数据的高维结构信息,在图像处理领域展现出巨大的应用前景。本节介绍几种常用的张量分解算法,包括高阶SVD(HOSVD)、Tucker-ALS、CP-ALS等。这些算法的核心思想是通过迭代优化的方式,交替更新张量分解的因子矩阵,直到收敛为止。综上所述,本文系统地介绍了张量分解技术在图像处理领域的应用。

AI天才研究院 7115

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

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

假人有限元模型

假人模型,车辆碰撞假人有限元模型,适合做乘员安全的研究者利用

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

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

LeetCode Question Difficulty Distribution

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

wolf96的专栏 1385

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

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; }

相扑小猫的博客 343

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的博客 1346

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的博客 165

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

相扑小猫的博客 297

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、付费专栏及课程。

余额充值