subarray 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 List<Integer> subarraySum(int[] nums) {
// write your code here
ArrayList<Integer> res = new ArrayList<Integer>();
for(int i = 0;i < nums.length ; i ++){
int sum = 0;
for(int j = i; j < nums.length ;j++){
sum = sum + nums[j];
if(sum == 0){
res.add(i);
res.add(j);
return res;
}
}
}
return res;
}
}
这个题目两个循环就实现了,但一般两个循环就实现了的题性能都不够好,都应该还有比较好的算法。
这个题和string里那个longest common string有类似之处,都可以dp.稍后再写写这个了。
subarray sum的更多相关文章
- [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 ...
- Subarray Sum Closest
Question Given an integer array, find a subarray with sum closest to zero. Return the indexes of the ...
- 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 ...
- Maximum Subarray Sum
Maximum Subarray Sum 题意 给你一个大小为N的数组和另外一个整数M.你的目标是找到每个子数组的和对M取余数的最大值.子数组是指原数组的任意连续元素的子集. 分析 参考 求出前缀和, ...
随机推荐
- ORACLE重装之后恢复数据库,相当于sqlserver的附加数据库
在开发机器上经常会遇到重装系统的问题,重装之前如果ORACLE没有及时备份的话重装之后就纠结了,数据还原很头疼. 各种娘中只能找到一些ORACLE安装与重装系统前目录相同的解决办法,目录不同就没招了. ...
- Idea中类上有叉的解决方法
idea中类的头上出现X解决办法 ctrl+alt+s 在弹出的菜单上选择Compiler下的Excludes 右边会有 移除掉,点击ok, 重启idea就可以了
- 编程最好用的字体consolas
python 自带idle最好用的字体consolas https://www.icourse163.org/learn/BIT-268001?tid=1002788003#/learn/forumd ...
- python中__get__,__getattr__,__getattribute__的区别
__get__,__getattr__和__getattribute都是访问属性的方法,但不太相同. object.__getattr__(self, name) 当一般位置找不到attribute的 ...
- oracle中文乱码的解决方法
select userenv('language') from dual; NLS_LANG AMERICAN_AMERICA.AL32UTF8
- 页面JS实现按钮点击增加输入框
学习记录 https://www.tuicool.com/articles/byUf2qe
- easyui改变tab标题
截图: 代码: //更改tab的标题 var tab = $('#microAppVersionTabs').tabs('getTab',0);// 取得第一个tab $('#microAppVe ...
- Jenkins之定时任务
H的用法: H 10 * * * ,这里H不是小时的意思,符号H(代表“Hash”,后面用“散列”代替) 符号H 在一定范围内可被认为是一个随机值,但实际上它是任务名称的一个散列而不是随机函数,每个 ...
- css3属性蒙版:-webkit-mask
-webkit-mask: 蒙版,用于将固定形状设置透明度,形状可以是一个div,也可以是一张图片: 用法:-webkit-mask: radial-gradient(transparent 50px ...
- Codeforces Round #554 (Div. 2)-C(gcd应用)
题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k ...