leetcode

LeetCode All in One 题目讲解汇总(持续更新中...)

﹥>﹥吖頭↗ 提交于 2020-01-21 22:06:17
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. Note: A leaf is a node with no children. Example: Given the below binary tree and sum = 22 , 5 / \ 4 8 / / \ 11 13 4 / \ \ 7 2 1 return true, as there exist a root-to-leaf path 5->4->11->2 which sum is 22. 这道题给了一棵二叉树,问是否存在一条从跟结点到叶结点到路径,使得经过到结点值之和为一个给定的 sum 值,这里需要用深度优先算法 DFS 的思想来遍历每一条完整的路径,也就是利用递归不停找子结点的左右子结点,而调用递归函数的参数只有当前结点和 sum 值。首先,如果输入的是一个空结点,则直接返回 false,如果如果输入的只有一个根结点,则比较当前根结点的值和参数 sum 值是否相同,若相同,返回 true,否则 false。 这个条件也是递归的终止条件。下面就要开始递归了

LeetCode All in One 题目讲解汇总(持续更新中...)

杀马特。学长 韩版系。学妹 提交于 2020-01-21 20:46:42
There are a row of n houses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color. The cost of painting each house with a certain color is represented by a n x 3 cost matrix. For example, costs[0][0] is the cost of painting house 0 with color red; costs[1][2] is the cost of painting house 1 with color green, and so on... Find the minimum cost to paint all houses. Note: All costs are positive integers. 这道题说有n个房子

LeetCode All in One 题目讲解汇总(持续更新中...)

我是研究僧i 提交于 2020-01-21 20:41:54
The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour, the smart thief realized that "all houses in this place forms a binary tree". It will automatically contact the police if two directly-linked houses were broken into on the same night. Determine the maximum amount of money the thief can rob tonight without alerting the police. Example 1: 3 / \ 2 3 \ \ 3 1 Maximum amount of money the thief can rob = 3 + 3 + 1 = 7. Example 2: 3 / \ 4 5 / \ \ 1 3 1

LeetCode All in One 题目讲解汇总(持续更新中...)

人盡茶涼 提交于 2020-01-21 20:37:32
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police. Example 1: Input: [2,3,2] Output: 3

Leetcode数组题*3

隐身守侯 提交于 2020-01-21 16:37:40
目录 Leetcode数组题*3 66.加一 题目描述 思路分析 88.合并两个有序数组 题目描述 思路分析 167.两数之和Ⅱ-输入有序数组 题目描述 思路分析 Leetcode数组题*3 66.加一 题目描述 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。 你可以假设除了整数 0 之外,这个整数不会以零开头。 示例: 输入: [1,2,3] 输出: [1,2,4] 解释: 输入数组表示数字 123。 链接: https://leetcode-cn.com/problems/plus-one 思路分析 参考题解大神的巧妙解法: Java数学解题 一个数用数组存储,那么加上1之后只有两种情况 最后一位不是9,直接让数组的最后一位加1后返回该数组。 最后一位是9的情况,需要在循环中判断,因为它的前面一位也可能是9,首先让9变为0,然后前一位加1。 如果最高位为9,可以创建一个新的数组,长度比原数组长度大一,首位置0。 /** * 将一个用表示数字的数组传入,加上1之后,返回新数组 * @param digits 传入的数组 * @return 返回加一之后的新数组 */ public static int[] plusOne(int[] digits){ int len = digits.length;

LeetCode All in One 题目讲解汇总(持续更新中...)

╄→гoц情女王★ 提交于 2020-01-20 10:05:41
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. Note: The same word in the dictionary may be reused multiple times in the segmentation. You may assume the dictionary does not contain duplicate words. Example 1: Input: s = "catsanddog" wordDict = ["cat", "cats", "and", "sand", "dog"] Output: [ "cats and dog", "cat sand dog" ] Example 2: Input: s = "pineapplepenapple" wordDict = ["apple", "pen", "applepen", "pine", "pineapple"] Output:

LeetCode All in One 题目讲解汇总(持续更新中...)

倾然丶 夕夏残阳落幕 提交于 2020-01-20 08:55:52
Given a pattern and a string str , find if str follows the same pattern. Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty substring in str . Example 1: Input: pattern = "abab", str = "redblueredblue" Output: true Example 2: Input: pattern = pattern = "aaaa", str = "asdasdasdasd" Output: true Example 3: Input: pattern = "aabb", str = "xyzabcxzyabc" Output: false Notes: You may assume both pattern and str contains only lowercase letters. 这道题是之前那道 Word Pattern 的拓展,之前那道题词语之间都有空格隔开,这样我们可以一个单词一个单词的读入,然后来判断是否符合给定的特征,而这道题没有空格了,那么难度就大大的增加了

LeetCode All in One 题目讲解汇总(持续更新中...)

大兔子大兔子 提交于 2020-01-20 06:48:24
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k) such that the distance between i and j equals the distance between i and k ( the order of the tuple matters ). Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive). Example: Input: [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]] 这道题定义了一种类似回旋镖形状的三元组结构,要求第一个点和第二个点之间的距离跟第一个点和第三个点之间的距离相等。现在给了我们n个点,让我们找出回旋镖的个数。那么我们想,如果我们有一个点a,还有两个点b和c

《1.16-1.19 Leetcode》

廉价感情. 提交于 2020-01-20 01:49:23
1. 最小栈 题目描述: 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) – 将元素 x 推入栈中。 pop() – 删除栈顶的元素。 top() – 获取栈顶元素。 getMin() – 检索栈中的最小元素。 代码: package leetcode . week5 ; import java . util . Stack ; /** * @author chengzhengda * @version 1.0 * @date 2020-01-17 10:36 * @desc 最小栈 */ public class t155 { private Stack < Integer > stack1 ; private Stack < Integer > stack2 ; public t155 ( ) { this . stack1 = new Stack < > ( ) ; this . stack2 = new Stack < > ( ) ; } public void push ( int x ) { stack1 . push ( x ) ; // 如果栈不为空,且当前元素小于等于栈顶元素,则入栈 if ( ! stack2 . isEmpty ( ) ) { if ( x <= stack2 . peek ( ) ) {

leetcode刷题记第19题解法(python解析)

烈酒焚心 提交于 2020-01-19 13:47:56
leetcode刷题记--> 19题解法(python解析) 题目定义 解题 1. 转为列表,然后再转为链表 2. 使用递归的思路 3. 使用字典存储的方法 4. 使用单指针 5. 使用双指针 实现 题目定义 给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点。 示例: 给定一个链表: 1->2->3->4->5, 和 n = 2. 当删除了倒数第二个节点后,链表变为 1->2->3->5. 说明: 给定的 n 保证是有效的。 进阶: 你能尝试使用一趟扫描实现吗? 来源:力扣(LeetCode) 链接: leetcode_19题 . 解题 本次使用4种方法,在leetcode上还有更多的方法,只能说真牛逼,真聪明。 1. 转为列表,然后再转为链表 先将链表转为列表,然后将列表转换为链表 2. 使用递归的思路 需要自己debug来进行理解 3. 使用字典存储的方法 首先将链表分为不同的节点进行存储,然后一步步的将链表进行替换 4. 使用单指针 定义单指针 先计算出链表的长度 然后通过链表长度减去n 就是要铲除的那个节点的前一个节点 然后循环 5. 使用双指针 定义双指针 先让first指针先走 先走n步 然后再让first和second同时走 当first走到末尾的时候 second也就走到要删除的那个节点的前一个节点 其实就是两者的一个差值 =============