[LintCode] Subarray Sum & Subarray Sum II】的更多相关文章

Subarray Sum Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number. Example Given [-3, 1, 2, -3, 4], return [0, 2] or [1, 3]. 用hashmap来存从nums[0…
LintCode 402: Continuous Subarray Sum 题目描述 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的下标.(如果两个相同的答案,请返回其中任意一个) 样例 给定[-3, 1, 3, -3, 4], 返回[1,4]. Thu Feb 23 2017 思路 本题有很多解法,最巧妙的解法就是一遍扫描记忆的方法了,时间复杂度为\(O(n)\). 用一个变量记录连续累加的和,当和为负数时,变量清零,从下一个数字…
题目 连续子数组求和 给定一个整数数组,请找出一个连续子数组,使得该子数组的和最大.输出答案时,请分别返回第一个数字和最后一个数字的值.(如果两个相同的答案,请返回其中任意一个) 样例 给定 [-3, 1, 3, -3, 4], 返回[1,4]. 解题 法一:直接暴力,时间复杂度O(N2),时间超时 public class Solution { /** * @param A an integer array * @return A list of integers includes the i…
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number. Example Given [-, , , -, ], , ] or [, ]. Note There is at least one subarray that it's s…
2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已经给出是50000量级,显然只能是O(n),至多O(nlogn)的复杂度. 本题使用DP和滑动数组都比较棘手,这才是最麻烦的地方,我们知道一般来说对于subarray的问题,dp和滑动数组是两大利器,但是本题这两个最实用的解法都不再适用,那么该如何解决呢? 其实想通了就是一个很简单的问题,本题是lo…
Path Sum OJ: https://oj.leetcode.com/problems/path-sum/ Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and…
Path Sum Total Accepted: 81706 Total Submissions: 269391 Difficulty: Easy Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example:Given the below…
Path Sum 题目链接 题目要求: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 /…
文章目录 Two Sum Two Sum II 3Sum 4Sum Two Sum 题意 给定一个数组,和指定一个目标和.从数组中选择两个数满足和为目标和.保证有且只有一个解.每个元素只可以用一次. 思路 Hash表快速查询值是否在数组中存在. 枚举一个数,查询另一个数是否存在. 注意:虽然一个元素只可以使用一次,但是数组中可以出现重复的元素. 复杂度 T(N)=O(N),M(N)=O(N)T(N)=O(N),M(N)=O(N)T(N)=O(N),M(N)=O(N) 结果 Runtime: 5…
今天的模拟赛里T2要使用到数论分块,里面有一个重要的坎就是关于r=sum/(sum/l)的证明,网上关于这道题的题解里都没有关于这个的证明,那么我就来填补一下: 在以下的文章里,我都会使用lo(x)表示对x向下取整,同理up(x)表示对x向上取整; 我们要求左右区间的边界,那么我们就不妨设 取两个数 i 和 i‘ 使得lo(N/i')==lo(N/i)则,我们就可以证明 设 lo(N/i)=k;则有  k*i+p=N   (p一定有  0<=p<i 成立)   设i’=i+d  则有   lo…
今天在浏览网页的时候,发现了一个叫做  华为面试题(8分钟写出代码) 的链接,不确定真实性,纯属好奇,就点进去看看 这个可能是很老的题目吧,因为我看到这题目时,底下有好多评论了.提到XX排序,内存占用,等等词汇 小丑是新人,也想用自己的方法解一下题目,如有雷同,纯属巧合 解: (1) 有前辈在解题时用到排序,但我认为没有必要,排序后的sum(a) - sum(b), 与任意杂乱顺序的sam(a) - sam(b) 结果肯定是一致的 ; (2) 如果 c[i] = a[i] - b[i]  , …
有两个数组a,b,大小都为n,数组元素的值任意整形数,无序: 要求:通过交换a,b中的元素,使数组a元素的和与数组b元素的和之间的差最小. 当前数组a和数组b的和之差为    A = sum(a) - sum(b) a的第i个元素和b的第j个元素交换后,a和b的和之差为    A' = sum(a) - a[i] + b[j] - (sum(b) - b[j] + a[i])           = sum(a) - sum(b) - 2 (a[i] - b[j])           = A…
Given an integer N, the task is to find out the count of numbers M that satisfy the condition M + sum(M) + sum (sum(M)) = N, where sum(M) denotes the sum of digits in M. Input:The first line of input contains an integer T denoting the number of test…
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return -1 instead. Have you met this question in a real interview?     Example Given the array [2,3,1,2,4,…
Question: Find the contiguous subarray within an array (containing at least one number) that has the largest sum. For example, given the array [2, 1, –3, 4, –1, 2, 1, –5, 4], The contiguous array [4, –1, 2, 1] has the largest sum = 6. 此问题提供了两种解决方案,首先…
http://www.geeksforgeeks.org/find-if-there-is-a-subarray-with-0-sum/ #include <iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> #include <string> #include <fstream> #include <m…
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the combination.  Example Given candidate set [10,1,6,7,2,1,5]…
题目: 最小子数组 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 样例 给出数组[1, -1, -2, 1],返回 -3 注意 子数组最少包含一个数字 解题: 和最大子数组 ,差不多的,动态规划的还是可以继续用的 Java程序: public class Solution { /** * @param nums: a list of integers * @return: A integer indicate the sum of minimum subarray */ publ…
Problem 1 [Balanced Binary Tree] Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. Pr…
39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc…
思路: 简单搜索 总结: dfs 框架 1. 需要打印路径. 在 dfs 函数中假如 vector 变量, 不用 & 修饰的话就不需要 undo 2. 不需要打印路径, 可设置全局变量 ans, 在 dfs 函数中对 ans 判定, 判定的位置尽可能的多 3. 对 tree 遍历, 有两种办法, 第一种是 if(root == NULL) 第二种是 if(root->left == NULL), 我一般用第二种, 效率比较高, 但是在第二种 return 1, 第一种 return 0 4.…
1. Combination Sum Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. No…
112题目如下: Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 r…
啊啊啊啊.好怀念这样的用递归保存路径然后打印出来的题目啊.好久没遇到了. 分了两种,一种是能够反复使用数组中数字的,一种是每一个数字仅仅能用一次的.事实上没有多大差别,第一种每次进入递归的时候都要从头開始尝试.另外一种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样. 跟我一样用引用保存中间结果的话.要注意回退的情况. 另外一种回退时,要把用到的那个数也恢复为可用,就全然像全排列时做的一样.破例贴两个题的代码.由于他们是在是不值得用两片文章来写. class Solution…
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Example Given candidate set [2,3,6,7] …
1.Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. The same repeated number may be chosen from candidates unlimited…
Maximum Product Subarray Title: Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 6. 对于Product…
There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins. Could you please decide the first player will…
题目 硬币排成线 II 有 n 个不同价值的硬币排成一条线.两个参赛者轮流从左边依次拿走 1 或 2 个硬币,直到没有硬币为止.计算两个人分别拿到的硬币总价值,价值高的人获胜. 请判定 第一个玩家 是输还是赢? 样例 给定数组 A = [1,2,2], 返回 true. 给定数组 A = [1,2,4], 返回 false. 解题 动态规划.博弈论 坑死,看了好久 定义dp[i]表示从i到end能取到的最大值 当我们在i处,有两种选择: 1.若取values[i],对方可以取values[i+1…
买卖股票的最佳时机 II 假设有一个数组,它的第i个元素是一个给定的股票在第i天的价格.设计一个算法来找到最大的利润.你可以完成尽可能多的交易(多次买卖股票).然而,你不能同时参与多个交易(你必须在再次购买前出售股票). 给出一个数组样例[2,1,2,0,1], 返回 2 解题 参考买卖股票的最佳时机I ,求出相邻两天的股票差值,当差值大于0的时候,完成一次交易.当连续一段的交易,可以理解为:该连续交易的起始是购买,连续交易的结束时卖出.这样求出的就是一个子段的最大值.对所有的子段求和就是答案了…