`

LeetCode [1] 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

 

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

 

 

 

 

分享到:
评论

相关推荐

    1 two sum_SUM_leetcodec++代码_

    Leetcode 1 two sum C++ code

    Leetcode two sum java 解法

    Leetcode two sum java 解法

    leetcode答案-LeetCode_1_TwoSum:LeetCode_1_TwoSum

    答案LeetCode_1_TwoSum LeetCode 问题:给定一个整数数组,找出两个数字,使它们相加为特定的目标数字。 函数 twoSum 应该返回两个数字的索引,使它们相加为目标,其中 index1 必须小于 index2。 请注意,您返回的...

    Two Sum leetcode c++

    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)...

    0-leetcode:leetcode练习:twosum问题;addtwonumbers问题;reverse integer问题;最大不重复子字符串长度问题等,详细见readme.md;

    leetcode leetcode练习 twosum 问题 ;add two numbers问题;reverse integer问题;最大不重复子字符串长度问题;atoi问题;

    python-leetcode面试题解之两数之和TwoSum.zip

    python python_leetcode面试题解之两数之和TwoSum

    leetcode答案-Two-Sum:leetcode两数之和代码

    Two-Sum leetcode两数之和代码 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,数组...

    leetcode.3sum-leetcode-practice:算法实践

    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

    LeetCode 1.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 ...

    javalruleetcode-LeetCode:LeetCode算法问题

    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和oj-leetcode-1-Two-Sum:解决方案

    leetcode-1-Two-Sum 这是我在 Github 的第一天。 我只是 AC leetcode 中的第一个问题。 从现在开始,我将使用Github在leetcode中记录我的生活。 我擅长 C++,但对 java 和 Python 不是很专业,我将使用最流行的 3 种...

    InterviewByJS:JavaScript回答的面试问题

    执行命令后,还需要输入案例名称,例如Leetcode 1 Two Sum 。 它将在文件夹/src/yourPath/下创建三个新文件yourFile.mjs , yourFile.test.mjs和yourFile.readme.md 。 yourFile.mjs &gt;面试响应 yourFile.test.mjs ...

    leetcode卡-leetcode:一些leetcode问题的解决方法,请注意license!

    Leetcode\TwoSum\TwoSum.cs 问题: 业绩报告: 反转整数 代码: Leetcode\ReverseInteger\ReverseInteger.cs 问题: 业绩报告: 回文数 代码: Leetcode\PalindromeNumber\PalindromeNumber.cs 问题: 从排序数组中...

    leetcode不会-1TwoSum:leetcode-1二和

    leetcode 不会1. 二和 资源: 优化搜索无序列表 你不能。 您能做的最好的事情是分解搜索任务并使用并发或并行来加速搜索任务。 从最后一个索引遍历数组 范围运算符只能以增量方式工作。 // In Kotlin/Swift // this ...

    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. Two Sum 2. Add Two Numbers 3. Longest Substring Without Repeating Characters 4. Median of Two Sorted Arrays 7. Reverse Integer 9. Palindrome Number 11. Container With Most Water 13. Roman to ...

    LeetCode最全代码

    371 | [Sum of Two Integers](https://leetcode.com/problems/sum-of-two-integers/) | [C++](./C++/sum-of-two-integers.cpp) [Python](./Python/sum-of-two-integers.py) | _O(1)_ | _O(1)_ | Easy | LintCode | ...

    leetcode答案-Two-Sum:二和

    Two-Sum(两数和问题) 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下 可以假设每种输入只会对应一个答案。但是,不能重复利用这个数组中同样的...

    手绘算法力扣 1 两数之和(Two Sum)

    手绘算法力扣 1 两数之和(Two Sum)

    leetcode2sumc-Two-Sum:我的LeetCodeC解决方案:二和

    leetcode 2 和 c 二和 我的 LeetCode C 代码解决方案:二和 Success Details Runtime: 4 ms, faster than 97.08% of C online submissions for Two Sum. Memory Usage: 5.8 MB, less than 99.36% of C online ...

Global site tag (gtag.js) - Google Analytics