Subarray Sum Closest
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的更多相关文章
- Lintcode: Subarray Sum Closest
Given an integer array, find a subarray with sum closest to zero. Return the indexes of the first nu ...
- [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 ...
- [LeetCode] Minimum Size Subarray Sum 最短子数组之和
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- [LintCode] Minimum Size Subarray Sum 最小子数组和的大小
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 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 ...
- [LintCode] Continuous Subarray Sum II
Given an integer array, find a continuous rotate subarray where the sum of numbers is the biggest. Y ...
- leetcode面试准备:Minimum Size Subarray Sum
leetcode面试准备:Minimum Size Subarray Sum 1 题目 Given an array of n positive integers and a positive int ...
- [LeetCode] Minimum Size Subarray Sum 解题思路
Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...
- 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 ...
随机推荐
- MVC jsp+servlet+javabean 连接Mysql数据库測试demo
本文介绍的是怎样使用MVC架构去实现jsp+servlet+javabean连接数据库 首先我们应该了解什么是MVC: MVC包含三个部分 : ①View:由各种JSP页面组成. ②Controlle ...
- 使用truncate命令清空当前用户所有表的所有数据
--批量清空当前用户所有表的所有数据 declarev_sql varchar2(2000) ;CURSOR cur is select table_name from user_tables ord ...
- new关键字在虚方法的动态调用中的阻断作用
关于new关键字在虚方法动态调用中的阻断作用,也有了更明确的理论基础.在子类方法中,如果标记 new 关键字,则意味着隐藏基类实现,其实就是创建了与父类同名的另一个方法,在编译中这两个方法处于动态方法 ...
- ADB错误“more than one device and emulator”(转)
当我连着手机充电的时候,启动模拟器调试,执行ADB指令时,报错.C:\Users\gaojs>adb shellerror: more than one device and emulatorC ...
- smarty半小时快速上手教程(转)
来源于:http://www.chinaz.com/program/2010/0224/107006.shtml 一:smarty的程序设计部分: 在smarty的模板设计部分我简单的把smarty在 ...
- java静态代码块 类加载顺序问题。
class B extends Object { static {System.out.println("Load B");} public B(){System.out.prin ...
- 图文教程:手把手教你用U盘安装Ubuntu
说到ubuntu,有接触linux的童鞋都应该听过,用wubi安装只是像在电脑上安装一个软件,可以轻松体验ubuntu,不过毕竟性能会打折扣,所以本人是比较喜欢直接安装在硬盘上的. 这种方法只适合用d ...
- jS放大镜效果
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="demo4.aspx.cs& ...
- SpringMVC 文件上传配置,多文件上传,使用的MultipartFile(转)
文件上传项目的源码下载地址:http://download.csdn.net/detail/swingpyzf/6979915 一.配置文件:SpringMVC 用的是 的MultipartFil ...
- Spring4.0学习笔记(3) —— Spring_Bean之间的关系
1.继承关系 bean-relation.xml <?xml version="1.0" encoding="UTF-8"?> <beans ...