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. cocos2dx3.0 超级马里奥开发笔记(两)——正确的规划游戏逻辑

    我将不得不拿出一个完整的开发笔记.由于个人原因.代码已OK该,博客,那么就不要粘贴代码,直接解释了整个游戏设计,更确切地说,当新手应该注意的地方发展. 1.继承类和扩展作用的权----展阅读(MVC) ...

  2. ndroid网络(4):HttpClient必经之路----使用线程安全的单例模式HttpClient,及HttpClient和Application的融合

    上文简 单介绍了HttpClient和Tomcat服务器的交互,主角是HttpClient,然后它跟服务器交互有两种方式即get和post.所以这个 HttpClient就类似于电脑上用的浏览器.当我 ...

  3. jboss学习 - vfs---转载

    jboss的VFS是为了解决什么问题,他为什么有用呢 在jboss中有很多类似的资源操作的代码都分散在程序的各个地方,大多数情况下代码首先确定操作的资源的类型,比如是文件或者是文件夹,通过URL加载的 ...

  4. Android 百度地图 SDK v3.0.0 (一)

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/37729091 最近公司要把百度地图集成的项目中,于是我就研究了一天百度地图的SD ...

  5. Java基础知识强化26(1):Object类之Object类的概述

    1.Object类 类Object是类层次结构的根类,每个类都使用 Object作为超类.所有对象(包括数组)都实现这个类的方法 每个类直接或者间接继承自Object类   2.Object类无参构造 ...

  6. Nginx优化—配置优化

    完整配置如下所示: user nginx; #运行的用户 pid /var/run/nginx.pid; worker_processes auto; #定义了nginx对外提供web服务时的word ...

  7. 03-StreamReader和StreamWriter的案例

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.I ...

  8. ClickOnce发布后不能安装

    当在internet发布用ClickOnce打包的客户端程序时,遇到ClickOnce启动后出错,错误信息如下: + Downloading https://xxxxx/Deploy/pc/Boote ...

  9. 为什么不使用frame框架的原因

    框架的优点 重载页面时不需要重载整个页面,只需要重载页面中的一个框架页(减少了数据的传输,增加了网页下载速度) 方便制作导航栏 框架的缺点 会产生很多页面,不容易管理 不容易打印 浏览器的后退按钮无效 ...

  10. SQL 语句修改列名 属性 默认值

    --修改字段名exec sp_rename '表名.列名','新列名' --修改字段属性alter table 表名 alter column 列名 nvarchar(100) null; --修改默 ...