题目:
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)...
c++ C++_leetcode题解之001. Two Sum.cpp
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 [] ``` ###...
c语言入门 c语言_leetcode题解01-two-sum.c
js js_leetcode题解之1-two-sum.js
"两数之和"(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
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 种...
执行命令后,还需要输入案例名称,例如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 问题: 从排序数组中...
java java_leetcode题解之Minimum ASCII Delete Sum for Two Strings.java