链接 4Sum 难度 Medium 描述 Given an array nums of n integers and an integer target, are there elements a , b , c , and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target. 给定一个n个整数的数组n,和一个整数tar…
给定两个非空链表来表示两个非负整数.位数按照逆序方式存储,它们的每个节点只存储单个数字.将两数相加返回一个新的链表. 你可以假设除了数字 0 之外,这两个数字都不会以零开头. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 解法: class Solution {public: ListNode* addTwoNumbers(ListNode* l1, ListNode* l2…
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that…
题目描述: 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 nums = [2, 7, 1…
Problem:Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please no…
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 应该注意问题:1.数组是否可以改变原来顺序.2.同样的元素是否可以被重复利用.3.是否存在多组解.4.时间复杂度.5.空间复杂度. 方法1:利用 map,时间复杂度 O(n),空间复杂度 O(n). c…
这是悦乐书的第179次更新,第181篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第38题(顺位题号是167).给定已按升序排序的整数数组,找到两个数字,使它们相加到特定的目标数.函数twoSum应该返回两个数字的索引,使它们加起来到目标,其中index1必须小于index2. 注意: 返回的答案(index1和index2)不是从零开始的. 可以假设每个输入只有一个解决方案,并且您可能不会两次使用相同的元素. 例如: 输入:数字= [2,7,11,15],目标=…
乘风破浪:LeetCode真题_040_Combination Sum II 一.前言 这次和上次的区别是元素不能重复使用了,这也简单,每一次去掉使用过的元素即可. 二.Combination Sum II 2.1 问题 2.2 分析与解决 通过分析我们可以知道使用递归就可以解决问题,并且这次我们从头遍历一次就不会出现多次使用某一个元素了. class Solution { List<List<Integer>> ans; public List<List<Intege…
leecode刷题(8)-- 两数之和 两数之和 描述: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 思路: 这道题其实很简单,我们可以直接用暴力搜索的方法,设置双重…
乘风破浪:LeetCode真题_039_Combination Sum 一.前言     这一道题又是集合上面的问题,可以重复使用数字,来求得几个数之和等于目标. 二.Combination Sum 2.1 问题 2.2 分析与解决 我们可以先将大于该数字的元素去除掉,之后取出一个元素,做减法,将得到的结果再放到集合中比较,直至最后减得结果为零则成功,为负数则失败回退.因此我们可以使用递归.堆栈.队列等来解决. public class Solution { public List<List<…
原题链接:https://leetcode-cn.com/problems/add-two-numbers/ 查看请另起链接打开. 解题思路执行用时 :2 ms, 在所有 Java 提交中击败了99.97%的用户.内存消耗 :40.6 MB, 在所有 Java 提交中击败了91.21%的用户. 此题的关键是自己实现一个两数的加法.之前以为可以通过数学运算实现,发现无论是int还是long都会存在溢出,无法完成所有测试案例.1.加法思路很简单,像解小学数学加法一样,个位和个位相加,十位和十位相加,…
题目描述 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 思路 思路一: 暴力 思路二: 用 HashMap 存储数组元素和索引的映射,在访问到 nums[i] 时,判断 HashMap 中是否存在 target - nums[i] ,如果存在说明…
题目:两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那两个整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素不能使用两遍. 示例 给定nums = { 2 , 7 , 11 ,15 } target = 9 因为nums[0] + nums[1] = 9 所以返回 [ 0 , 1 ] 解题思路: 编程语言:C++ 暴力破解法:即循环遍历两次数组nums 时间复杂度:O(n2) 代码实现如下 class Sol…
这是悦乐书的第280次更新,第296篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第148题(顺位题号是653).给定二进制搜索树和目标数,如果BST中存在两个元素,使得它们的总和等于给定目标,则返回true.例如: 5 / \ 3 6 / \ \ 2 4 7 目标值:9 输出:true 5 / \ 3 6 / \ \ 2 4 7 目标值:28 输出:false 本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Jav…
这是悦乐书的第227次更新 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第94题(顺位题号是437).您将获得一个二叉树,其中每个节点都包含一个整数值.找到与给定值相加的路径数.路径不需要在根或叶子处开始或结束,但必须向下(仅从父节点行进到子节点).树的节点数不超过1,000个,值范围为-1,000,000到1,000,000.例如: root = [10,5,-3,3,2,null,11,3,-2,null,1],sum = 8 10 / \ 5 -3 / \ \ 3…
第一题:题目内容 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 nums = [2, 7…
这是悦乐书的第204次更新,第214篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第70题(顺位题号是303).给定整数数组nums,找到索引i和j(i≤j)之间的元素之和,包括端点.例如: 给定nums = [-2,0,3,-5,2,-1] sumRange(0,2) - > 1 sumRange(2,5) - > -1 sumRange(0,5) - > -3 注意: 您可以假设数组不会更改. sumRange函数有很多调用. 本次解题使用的开发工具是e…
这是悦乐书的第169次更新,第171篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第28题(顺位题号是112).给定二叉树和整数sum,确定树是否具有根到叶路径,使得沿路径的所有值相加等于给定的sum.叶子节点是没有子节点的节点.例如: 给定以下二叉树和sum = 22, 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 返回true,因为存在根到叶的路径5-> 4-> 11-> 2,并且和为22. 本次解题使用的开发工具是eclips…
第一题 给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出 和为目标值 target 的那 两个 整数,并返回它们的数组下标. 你可以假设每种输入只会对应一个答案.但是,数组中同一个元素在答案里不能重复出现. 你可以按任意顺序返回答案. 实例1. 输入:nums = [2,7,11,15], target = 9 输出:[0,1] 解释:因为 nums[0] + nums[1] == 9 ,返回 [0, 1] . 实例2 输入:nums = [3,2,4], targ…
程序 = 数据结构 + 算法. 算法是每一位程序员学习成长之路上无法避开的重要一环,并且越早接触越好.今后会每天做些算法题,至少每天做一道题目,同时会记录自己的解题思路和代码,通过[算法]专题来分享.针对数据结构这一块的知识,我也会抽时间补习,毕竟不是科班出生,从长远看,数据结构与算法学的越早越扎实越好,不管你使用的是哪一种开发语言. 01 看题和准备 给定一个整数数组和一个目标整数,该目标整数满足数组中两元素之和,返回数组中两个数字的下标索引,以数组形式表示结果.例如给定数组int[] num…
Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that…
两数相加 题目: 给出两个非空的链表用来表示两个非负的整数.其中,它们各自的位数是按照逆序的方式存储的,并且它们的每个节点只能存储一位数字.如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和. 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 思路: 1.定义一个带头结点的链表同时定义一个指针指向该结点 2.定义两个指针分别指向两个链表的头结点 3.定义一个int的…
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description   给定数组,数组中的元素均为正数,target也是正数.(数组中的元素可能有重复) 求出所有的满足求和等于terget的组合. 数组中的元素只能使用一次.(数组中重复的元素可以最多使用重复次数)   参考代码:  package leetcode_50; import java.util.ArrayList; import java.util.Array…
虽然题目简单,但我这好不容易优化到前2%,感觉也值得分享给大家(方法比较偷机) 题目: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/two-sum著作权归领扣网络所有.商业转载请联系官方授权,非商业转载请注明出处. 给定 num…
题目描述 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 方法一:暴力法 暴力法很简单,遍历每个元素 xx,并查找是否存在一个值与 target - xtarget−x…
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 nums = [2, 7, 11, 15]…
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. 英文不好,简单翻译一下,能自己翻译的跳过,看不懂我的翻译自行结合翻译网站…
@author: ZZQ @software: PyCharm @file: findMedianSortedArrays.py @time: 2018/10/10 19:24 说明:给定两个大小为 m 和 n 的有序数组 nums1 和 nums2 .请找出这两个有序数组的中位数.要求算法的时间复杂度为 O(log (m+n)) .你可以假设 nums1 和 nums2 不同时为空. 示例 1: nums1 = [1, 3] nums2 = [2] 中位数是 2.0 示例 2: nums1 =…
啊啊啊啊.好怀念这样的用递归保存路径然后打印出来的题目啊.好久没遇到了. 分了两种,一种是能够反复使用数组中数字的,一种是每一个数字仅仅能用一次的.事实上没有多大差别,第一种每次进入递归的时候都要从头開始尝试.另外一种要找一个标记的数组,把已经用到过的排除掉,就像生成全排列时的做法一样. 跟我一样用引用保存中间结果的话.要注意回退的情况. 另外一种回退时,要把用到的那个数也恢复为可用,就全然像全排列时做的一样.破例贴两个题的代码.由于他们是在是不值得用两片文章来写. class Solution…
题目标签:Linked List 题目给了我们两个 数字的linked list,让我们把它们相加,返回一个新的linked list. 因为题目要求不能 reverse,可以把 两个list 的数字都 存入 两个stack.利用了stack的特性,就可以从后面把数字相加,具体看code. Java Solution: Runtime:  6 ms, faster than 51.06% Memory Usage: 45.4 MB, less than 64.71% 完成日期:07/08/201…