题目:
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
public static int[] twoSum2(int[] nums, int target) { int[] result = new int[2]; int length = nums.length; int[] tempNums = nums.clone(); sortit(tempNums, 0, length -1); result[0] = 0; result[1] = 0; int min = 0; int max = length -1; boolean find = false; while(min < max && !find){ if((tempNums[min] + tempNums[max]) > target){ max--; }else if((tempNums[min] + tempNums[max]) < target){ min++; }else{ find = true; } } if(find){ for(int i = 0; i < length; i++){ if(tempNums[min] == nums[i]){ result[0] = i + 1; break; } } for(int i = 0; i < length; i++){ if(tempNums[max] == nums[i]){ if(tempNums[max] == tempNums[min] && (result[0]==(i+1)) ){ continue; } result[1] = i + 1; break; } } } if(result[0] > result[1]){ int j = result[0]; result[0] = result[1]; result[1] = j; } return result; } private static void sortit(int[] list, int left, int right){ if(left< right){ int middle = partition(list, left, right); sortit(list, left, middle-1); sortit(list, middle+1, right); } } private static int partition(int[] list, int left, int right) { int privot = list[left]; while(left < right){ while(left < right && privot <= list[right]){ right--; } list[left] = list[right]; while(left < right && privot >= list[left]){ left++; } list[right] = list[left]; } list[left] = privot; return left; }
相关推荐
Leetcode 1 two sum C++ code
Leetcode two sum java 解法
答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...
c c语言_leetcode 0001_two_sum
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)...
def twoSum(nums, target): hash_map = {} for i, num in enumerate(nums): complement = target - num if complement in hash_map: return [hash_map[complement], i] hash_map[num] = i return [] ``` ###...
Two Sum.cpp》这篇文档详细解释了如何使用C++语言解决LeetCode上的第一个问题,即“两数之和”(Two Sum)。这个问题是编程面试中常见的一个问题,也是一个经典的哈希表应用实例。在这篇题解中,作者首先介绍了题目...
本篇文件内容,主要围绕着LeetCode上的“two-sum”问题,给出一个C语言的解决方案,该方案可以作为C语言初学者入门实践的经典案例。 “two-sum”问题是这样的:给定一个整数数组nums和一个目标值target,请你在该...
"两数之和"(TwoSum)是LeetCode上的一道经典题目,也是许多初学者和面试者必练的问题。本资料包"python-leetcode面试题解之两数之和TwoSum.zip"显然包含了关于这个问题的Python解决方案。 **问题描述:** 给定一个...
Two-Sum leetcode两数之和代码 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组...
leetcode。 3sum leetcode-练习 算法实践 15. 3和 给定一个由 n 个整数组成的数组 nums,nums ...[-1, ...1, ...-1, ...[-1, ...1], [-1, -1, ...search_sum(target,num_idx,nums):#...two elements sum up to target ls=[] for i in ra
java入门 java_leetcode题解之001_Two_Sum
- 定义一个名为`twoSum`的函数,接受`nums`数组和`target`目标数作为参数。 - 创建一个空对象`hashMap`作为哈希表。 - 使用for循环遍历数组。 - 在每次循环中,计算目标数与当前数的差值,并检查这个差值是否...
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 one solution, and you may not use the same ...
LeetCode刷题汇总1 LeetCode刷题汇总1是LeetCode平台上的一系列算法题目汇总,涵盖了各种难度级别的题目,从简单到困难。以下是从这个汇总中提取的知识点: 1. 数组和链表操作: * 两数之和(Two Sum):给定一个...
1 双指针 编号 题目 LeetCode 11 Container With Most Water LeetCode 19 Remove Nth Node From End of List LeetCode 42 Trapping Rain Water LeetCode 61 RotateList LeetCode 75 Sort Colors LeetCode 125 Valid ...
leetcode-1-Two-Sum 这是我在 Github 的第一天。 我只是 AC leetcode 中的第一个问题。 从现在开始,我将使用Github在leetcode中记录我的生活。 我擅长 C++,但对 java 和 Python 不是很专业,我将使用最流行的 3 种...
def twoSum(numbers, target): left, right = 0, len(numbers) - 1 while left current_sum = numbers[left] + numbers[right] if current_sum == target: return [left + 1, right + 1] elif current_sum ...
执行命令后,还需要输入案例名称,例如Leetcode 1 Two Sum 。 它将在文件夹/src/yourPath/下创建三个新文件yourFile.mjs , yourFile.test.mjs和yourFile.readme.md 。 yourFile.mjs >面试响应 yourFile.test.mjs ...
Leetcode\TwoSum\TwoSum.cs 问题: 业绩报告: 反转整数 代码: Leetcode\ReverseInteger\ReverseInteger.cs 问题: 业绩报告: 回文数 代码: Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中...