Question

Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first number and last number.

Given [-3, 1, 1, -3, 5], return [0, 2][1, 3],[1, 1][2, 2] or [0, 4].

Answer

这道题延续Subarray Sum的思路,即将[0, i]的sum存起来。这里构造了一个新的类,Pair。一方面存sum值,一方面存index。然后根据sum值排序,算相邻sum值的差值。差值最小的即为结果。时间复杂度是O(nlogn),空间复杂度是O(n)。

注意:假设[0-2]和[0-4]的sum值的差值最小,那么结果为index[3,4]

 public class Solution {

     class Pair {
public int sum;
public int index;
public Pair(int sum, int index) {
this.sum = sum;
this.index = index;
}
}
/**
* @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[] result = new int[2];
if (nums == null || nums.length == 0) {
return result;
}
int len = nums.length;
int sum = 0;
List<Pair> list = new ArrayList<Pair>();
for (int i = 0; i < len; i++) {
sum += nums[i];
Pair tmp = new Pair(sum, i);
list.add(tmp);
}
Collections.sort(list, new Comparator<Pair>() {
public int compare(Pair a, Pair b) {
return (a.sum - b.sum);
}
});
int min = Integer.MAX_VALUE;
for (int i = 1; i < len; i++) {
int diff = list.get(i).sum - list.get(i - 1).sum;
if (diff < min) {
min = diff;
int index1 = list.get(i).index;
int index2 = list.get(i - 1).index;
if (index1 < index2) {
result[0] = index1 + 1;
result[1] = index2;
} else {
result[0] = index2 + 1;
result[1] = index1;
}
}
}
return result;
}
}

Subarray Sum Closest的更多相关文章

  1. Lintcode: Subarray Sum Closest

    Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...

  2. [LeetCode] Maximum Size Subarray Sum Equals k 最大子数组之和为k

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  3. [LeetCode] Minimum Size Subarray Sum 最短子数组之和

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  4. [LintCode] Minimum Size Subarray Sum 最小子数组和的大小

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  5. Subarray Sum & Maximum Size Subarray Sum Equals K

    Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code sho ...

  6. [LintCode] Continuous Subarray Sum II

    Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...

  7. leetcode面试准备:Minimum Size Subarray Sum

    leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...

  8. [LeetCode] Minimum Size Subarray Sum 解题思路

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  9. LeetCode OJ 209. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

随机推荐

  1. [RxJS] Combining streams in RxJS

    Source: Link We will looking some opreators for combining stream in RxJS: merge combineLatest withLa ...

  2. INFORMATION_SCHEMA.COLUMNS 查询表字段语句

    INFORMATION_SCHEMA.COLUMNS 视图以 sysobjects.spt_data type_info.systypes.syscolumns.syscomments.sysconf ...

  3. ANDROID内存优化(大汇总——上)

    转载请注明本文出自大苞米的博客(http://blog.csdn.net/a396901990),谢谢支持! 写在最前: 本文的思路主要借鉴了2014年AnDevCon开发者大会的一个演讲PPT,加上 ...

  4. 【转】解决警告 warning: directory not found for option

    转:http://blog.sina.com.cn/s/blog_6f72ff900101es6x.html 解决方法: 选择项目名称----->Targets----->Build Se ...

  5. jdbc03 使用servlet实现

    <%@page import="cn.bdqn.bean.News"%> <%@page import="cn.bdqn.service.impl.Ne ...

  6. python的行与缩进

    #coding=utf-8#逻辑行与物理行#以下是3个物理行print "abc"print "789"print "777" #以下是1个 ...

  7. sourceTree添加git密钥步骤

    给多个远程服务器比如https://github.com/wangjian2014/wjtest/blob/master/wj.txt添加public密钥 本地服务器添加private密钥     S ...

  8. Phonegap 版本minSdkVersion为8的时候的自动更新与升级

    清单文件中: <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/> ...

  9. Android 聊天气泡

    网上搜到的只有一篇是自定义的TextView,其使用比较麻烦,所以采用大众化的方法--使用9.png来实现. 这里主要介绍sdk tool的draw9patch.bat的使用. 这个bat执行文件打开 ...

  10. SQL Server 和CLR集成

    通过在 Microsoft SQL Server 中托管 CLR(称为 CLR 集成),可以在托管代码中编写存储过程.触发器.用户定义函数.用户定义类型和用户定义聚合函数. 因为托管代码在执行之前会编 ...