Two Sum - Difference equals to target
2018-01-11 14:16
288 查看
Given an array of integers, find two numbers that their
to a target value.
where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.
Notice
It's guaranteed there is only one available solution
Have you met this question in a real interview?
Yes
Example
Given nums =
return
该题目可以使用hashmap的方式尽心处理,也可以使用双指针的方式进行处理。使用双指针的方法需要重新设置一个类,进而定义一个带有索引和
该处数值的一个类。
java
differenceequals
to a target value.
where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are NOT zero-based.
Notice
It's guaranteed there is only one available solution
Have you met this question in a real interview?
Yes
Example
Given nums =
[2, 7, 15, 24], target =
5
return
[1, 2](7 - 2 = 5)
该题目可以使用hashmap的方式尽心处理,也可以使用双指针的方式进行处理。使用双指针的方法需要重新设置一个类,进而定义一个带有索引和
该处数值的一个类。
java
class Pair { int value; int index; public Pair(int value, int index) { this.value = value; this.index = index; } } public class Solution { /* * @param nums: an array of Integer * @param target: an integer * @return: [index1 + 1, index2 + 1] (index1 < index2) */ public int[] twoSum7(int[] nums, int target) { // write your code here if (nums == null || nums.length < 2) { return new int[]{}; } target = Math.abs(target); Pair[] p = new Pair[nums.length]; for (int i = 0; i < nums.length; i++) { p[i] = new Pair(nums[i], i); } Comparator<Pair> cmp = new Comparator<Pair>() { public int compare(Pair a, Pair b) { return a.value - b.value; } }; Arrays.sort(p, cmp); int left = 0, right = 0, sum = 0; int[] arr = new int[2]; while (left < nums.length && right < nums.length) { sum = p[right].value - p[left].value; if (sum == target) { if (left == right) { right++; continue; } arr[0] = p[left].index + 1; arr[1] = p[right].index + 1; break; } else if (sum < target) { right++; } else { left++; } } Arrays.sort(arr); return arr; } }
相关文章推荐
- Two Sum - Difference equals to target
- Two Sum - Closest to target
- Distribute numbers to two “containers” and minimize their difference of sum
- To find sum of two numbers without using any operator. Only use of printf() is allowed.
- Partition a set of numbers into two so that difference between their sum is minimum with equal size
- find two numbers such that they add up to a specific target number
- leetocode371 Sum of Two Integers
- 【NO.3】 c program to caculate and display sum of two matrix
- Submatrix Sum equals to K
- 【NO.3】 c program to caculate and display sum of two matrix
- I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum o
- Two Sum Closest to K
- find the closest sum to a target value
- 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 andsum =
- Subarray Sum to the given target value
- 【Leetcode】Given a binary tree, find all root-to-leaf paths whose sum equals a given number
- How to Calculate difference between two dates in C# z
- Is it possible to get the difference from two dynamic arrays regardless of their order?
- Finding all elements(which are integer) in an array whose sum is equals to the element exists within
- 1 Two Sum(找和为target的两个数字下标Medium)