1-Two Sum @LeetCode】的更多相关文章

Problem Description: http://oj.leetcode.com/problems/path-sum/ Pretty easy. /** * Definition for binary tree * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solu…
Question 39. Combination Sum Solution 分析:以candidates = [2,3,5], target=8来分析这个问题的实现,反向思考,用target 8减2,3,5这三个数,等到target为0的时候,所减过的数就是我们要求的一个结果. Java实现: public List<List<Integer>> combinationSum(int[] candidates, int target) { List<List<Integ…
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. Summary: DP, calucate the minimun…
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. Each element is either an integer, or a list -- whose elements may also be integers or other lists. Example 1:Given the list [[1,1],2,[1,1]], return…
Problem Description: http://oj.leetcode.com/problems/combination-sum/ Basic idea: It seems complicate at first. But once I got the recursive idea, the solution was neat. This code could be not efficient. I will rewrite with dynamic programming next t…
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2…
题目描写叙述: 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 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7, the subarray [4,3] has the…
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 0 instead. For example, given the array [2,3,1,2,4,3] and s = 7,the subarray [4,3] has the minimal…
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 题目大意:给定一个m*n矩阵,都是非负数字,找到一条路径从左上角到…
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 / \ \ 7 2 1 r…
1-Two Sum 题目 思路 题目中得到的信息有: 都是整数,并且可正可负,也可一个值包含多个: 只有一个正确的结果. 方法一: 最直接的思路就是两重循环遍历,时间复杂度是O(n^2),这样肯定不行. 方法二: 由于是乱序的,1)可以先排序,2)然后再遍历一遍就可以找到结果.排序的话不能再原来的基础上进行,这样就破坏了下标顺序,因此需要申请额外的空间,用于保存他们的索引,然后再该空间上进行排序.时间复杂度是[排序O(logn) + 查找O(n)],空间复杂度是O(n). /** * Note:…
题目: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at any point in time. 题解: 这道题跟Unique Path系列是思路一样的.具…
题目: Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. For example: Given the below binary tree, 1 / \ 2 3 Return 6. 题解: 递归求解. 取当前点和左右边加和,当前点的值中最大的作为本层返回值.并在全局维护一个max.使用数组,因为是引用类型.所以在递归过程中可以保存结果. 代码如下:…
题目: 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 return…
题目: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. Note: Elements in a quadruplet (a,b,c,d) must be in non-descending or…
题目: Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The s…
Given a binary tree, find the maximum path sum. For this problem, a path is defined as any sequence of nodes from some starting node to any node in the tree along the parent-child connections. The path does not need to go through the root. For exampl…
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2…
Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution. Example: Given nums = [, , , ], target = , Because nums[] + nums[] = + = , , ].…
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 (including target) will…
a(i):在节点i由于单边路径的最大结束 b(i):在节点i路径和 a(i) = max{ i->val, i->val + max{a(i->left), a(i->right) }}; b(i) = max{ i->val, i->val + max{a(i->left), a(i->right) } , i->val + a(i->left) + a(i->right)}; 因为a(i), b(i)只和a(i->left)和a(…
原题如下: 思路:将nums放到一个map<int,int>中,其中,键是nums中元素,值对应其下标.然后遍历nums,取nums中一个值nums[i],接着用target减去它,最后再map中找差值map[num[i]].如果发现差值,则返回i,map[num[i]]. 代码如下: class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { vector<in…
题目: 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 (including target) w…
1. 二数之和 题目 English Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nu…
题目链接:Path Sum | LeetCode OJ 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…
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1…
根据issac3 用Java总结了backtracking template, 我用他的方法改成了Python. 以下为template. def backtrack(ans, temp, nums, start): # 可能有start, 也可能没有 if len(temp) == len(nums): ans.append(temp) else: for i in range(start, len(nums)): if nums[i] not in temp: backtrack(ans,…
Path Sum leetcode java 描述 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…
最近在准备找工作的算法题,刷刷LeetCode,以下是我的解题报告索引,每一题几乎都有详细的说明,供各位码农参考.根据我自己做的进度持续更新中......                              本文地址 LeetCode:Reverse Words in a String LeetCode:Evaluate Reverse Polish Notation LeetCode:Max Points on a Line LeetCode:Sort List LeetCode:Ins…
17.8 You are given an array of integers (both positive and negative). Find the contiguous sequence with the largest sum. Return the sum. LeetCode上的原题,请参见我之前的博客Maximum Subarray. 解法一: int get_max_sum(vector<int> nums) { int res = INT_MIN, sum = INT_MI…