139. Subarray Sum Closest
2017-11-16 20:54
375 查看
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number.
Have you met this question in a real interview?
Yes
Example
Given
这个题目并不困难,需要计算一个前项和数组,然后将该数组排序,依次计算后项减前项的最小值,计算出分别计算出坐标,注意点,最小值的位置需要加一,因为最小值并不会被计算在内。
java
class Pairs {
int index;
int sum;
public Pairs(int index, int sum) {
this.index = index;
this.sum = sum;
}
}
public class Solution {
/*
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number and the index of the last number
*/
public int[] subarraySumClosest(int[] nums) {
// write your code here
int[] arr = new int[2];
if (nums == null || nums.length == 0) {
return arr;
}
if (nums.length == 1) {
arr[0] = arr[1] = 0;
return arr;
}
Pairs[] p = new Pairs[nums.length + 1];
p[0] = new Pairs(0, 0);
for (int i = 1; i <= nums.length; i++) {
p[i] = new Pairs(i, p[i - 1].sum + nums[i - 1]);
}
Comparator<Pairs> cmp = new Comparator() {
public int compare(Object a, Object b) {
return ((Pairs)a).sum - ((Pairs)b).sum;
}
};
Arrays.sort(p, cmp);
int max = Integer.MAX_VALUE;
for (int i = 1; i <= nums.length; i++) {
if (max > p[i].sum - p[i - 1].sum) {
max = p[i].sum - p[i - 1].sum;
arr[0] = p[i].index - 1;
arr[1] = p[i - 1].index - 1;
Arrays.sort(arr);
}
}
arr[0]++;
return arr;
}
}
Have you met this question in a real interview?
Yes
Example
Given
[-3, 1, 1, -3, 5], return
[0, 2],
[1, 3],
[1, 1],
[2, 2]or
[0, 4].
这个题目并不困难,需要计算一个前项和数组,然后将该数组排序,依次计算后项减前项的最小值,计算出分别计算出坐标,注意点,最小值的位置需要加一,因为最小值并不会被计算在内。
java
class Pairs {
int index;
int sum;
public Pairs(int index, int sum) {
this.index = index;
this.sum = sum;
}
}
public class Solution {
/*
* @param nums: A list of integers
* @return: A list of integers includes the index of the first number and the index of the last number
*/
public int[] subarraySumClosest(int[] nums) {
// write your code here
int[] arr = new int[2];
if (nums == null || nums.length == 0) {
return arr;
}
if (nums.length == 1) {
arr[0] = arr[1] = 0;
return arr;
}
Pairs[] p = new Pairs[nums.length + 1];
p[0] = new Pairs(0, 0);
for (int i = 1; i <= nums.length; i++) {
p[i] = new Pairs(i, p[i - 1].sum + nums[i - 1]);
}
Comparator<Pairs> cmp = new Comparator() {
public int compare(Object a, Object b) {
return ((Pairs)a).sum - ((Pairs)b).sum;
}
};
Arrays.sort(p, cmp);
int max = Integer.MAX_VALUE;
for (int i = 1; i <= nums.length; i++) {
if (max > p[i].sum - p[i - 1].sum) {
max = p[i].sum - p[i - 1].sum;
arr[0] = p[i].index - 1;
arr[1] = p[i - 1].index - 1;
Arrays.sort(arr);
}
}
arr[0]++;
return arr;
}
}
相关文章推荐
- #139 Subarray Sum Closest
- LintCode 139. Subarray Sum Closest
- Lintcode139 Subarray Sum Closest solution 题解
- Subarray Sum Closest
- lintcode:Subarray Sum Closest
- Subarray Sum Closest
- [LintCode]Subarray Sum Closest
- [LintCode] Subarray Sum Closest
- LintCode : 最接近零的子数组和 Subarray Sum Closest
- Lintcode: Subarray Sum Closest
- lintcode-medium-Subarray Sum Closest
- LintCode Subarray Sum Closest
- lintcode: Subarray Sum Closest
- Subarray Sum Closest
- [刷题]Subarray Sum Closest
- **[Lintcode]Subarray Sum Closest最接近零的子数组和
- Subarray Sum Closest
- LintCode-Subarray Sum Closest
- Subarray Sum Closest
- Subarray Sum Closest