leetcode

LeetCode: Binary Tree Maximum Path Sum

删除回忆录丶 提交于 2020-02-07 08:45:48
LeetCode: Binary Tree Maximum Path Sum 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 . 地址: https://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ 算法:用递归来解决。首先,最大的路径和出现在以下三种情况:1)出现在左子树中;2)出现在右子树;3)经过根节点,在这种情况下,相当与左边从根节点到叶节点的最大路径加上右边从根节点到叶节点的最大路径。 对于第三种情况也可以用递归来解决。代码: 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11

[LeetCode] 66. Plus One

耗尽温柔 提交于 2020-02-07 08:45:18
Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself. Example 1: Input: [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Example 2: Input: [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. 把一个vector存储的 digits 当成int来做加法,只+1 和 [LeetCode] 2. Add Two Numbers 有点像,然后我写出了一样(丑

[LeetCode] 426. Convert Binary Search Tree to Sorted Doubly Linked List 将二叉搜索树转为有序双向链表

拜拜、爱过 提交于 2020-02-07 07:07:23
Convert a BST to a sorted circular doubly-linked list in-place. Think of the left and right pointers as synonymous to the previous and next pointers in a doubly-linked list. Let's take the following BST as an example, it may help you understand the problem better: We want to transform this BST into a circular doubly linked list. Each node in a doubly linked list has a predecessor and successor. For a circular doubly linked list, the predecessor of the first element is the last element, and the successor of the last element is the first element. The figure below shows the circular doubly linked

[leetcode 6]ZigZag Conversion

邮差的信 提交于 2020-02-07 03:07:00
1 题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string convert(string text, int nRows); convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR" . Hide Tags String 2 思路: 2.1 什么是zigzag? https://leetcode.com/discuss/14483/share-simple-c-solution 2.2 解决方法:需要使用nRows个StringBuffer来存储每列的字符,最后再循环一遍即可,参考: https:/

LeetCode-237 删除链表中的节点

北城余情 提交于 2020-02-07 03:03:15
问题: 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。 现有一个链表 -- head = [4,5,1,9],它可以表示为: 示例 1: 输入: head = [4,5,1,9], node = 5 输出: [4,1,9] 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9. 示例 2: 输入: head = [4,5,1,9], node = 1 输出: [4,5,9] 解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9. 说明: 链表至少包含两个节点。 链表中所有节点的值都是唯一的。 给定的节点为非末尾节点并且一定是链表中的一个有效节点。 不要从你的函数中返回任何结果。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 分析:这道题的难度在于容易陷入思维误区,题干说明是删除节点,所以我们在想的是利用某种算法来删除给定的节点,但实际上这是不可能实现的,所以我们应该透过现象看本质,本质是将链表[4,5,1,9],转换为[4,5,9]

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

两盒软妹~` 提交于 2020-02-06 07:31:46
In the "100 game," two players take turns adding, to a running total, any integer from 1..10. The player who first causes the running total to reach or exceed 100 wins. What if we change the game so that players cannot re-use integers? For example, two players might take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100. Given an integer maxChoosableInteger and another integer desiredTotal , determine if the first player to move can force a win, assuming both players play optimally. You can always assume that maxChoosableInteger will not

LeetCode:44 通配符匹配 动态规划

南笙酒味 提交于 2020-02-06 03:52:28
LeetCode:44 通配符匹配 动态规划 给定一个字符串 (s) 和一个字符模式 ( p ) ,实现一个支持 ‘?’ 和 ‘*’ 的通配符匹配。 ‘?’ 可以匹配任何单个字符。 ‘*’ 可以匹配任意字符串(包括空字符串)。 两个字符串完全匹配才算匹配成功。 说明: s 可能为空,且只包含从 a-z 的小写字母。 p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *。 示例 1: 输入: s = "aa" p = "a" 输出: false 解释: “a” 无法匹配 “aa” 整个字符串。 示例 2: 输入: s = "aa" p = "*" 输出: true 解释: ‘*’ 可以匹配任意字符串。 示例 3: 输入: s = "cb" p = "?a" 输出: false 解释: ‘?’ 可以匹配 ‘c’, 但第二个 ‘a’ 无法匹配 ‘b’。 示例 4: 输入: s = "adceb" p = "*a*b" 输出: true 解释: 第一个 ‘ ’ 可以匹配空字符串, 第二个 ' ’ 可以匹配字符串 “dce”. 示例 5: 输入: s = "acdcb" p = "a*c?b" 输入: false 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/wildcard-matching 著作权归领扣网络所有

LeetCode高频题:链表(四)

别来无恙 提交于 2020-02-06 03:35:33
我们讲解的题目都是leetcode上经典的题目,而且我们的解答一定也是最简单最经典的 。今天带来两道关于链表的经典题。本期所用的链表的数据结构均如下: public class ListNode { int val ; ListNode next ; ListNode ( int x ) { val = x ; } } 143. 重排链表 题意:给定一个单链表 L:L0→L1→…→Ln-1→Ln , 将其重新排列后变为: L0→Ln→L1→Ln-1→L2→Ln-2→… 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换。如图: 这道题是leetcode经典的绕来绕去的题目,我们来看一下粉丝投稿的代码是怎么样的: ListNode tailNode = null ; public void reorderList ( ListNode head ) { if ( head == null ) return ; ListNode firstNode = head ; ListNode lastNode = head ; while ( lastNode . next != tailNode ) lastNode = lastNode . next ; tailNode = lastNode ; if ( firstNode == lastNode ) { firstNode

vscode登陆中国版leetcode

别来无恙 提交于 2020-02-05 09:57:52
准备刷leetcode,在vscode上登陆时发现一直提示“invalid password”。最后发现需要默认的leetcode是美国版的,因为我的账号密码都是中国版的,所以需要更换为中国版才能正常登陆。 Extensions–>search"leetcode"–>istall完成出现leetcode的logo 点击地球图标,更换为leetcode中国版 账号密码登陆即可。 来源: CSDN 作者: Horace__liu 链接: https://blog.csdn.net/Horace__liu/article/details/104017270

学习库,刷题提升

本秂侑毒 提交于 2020-02-05 09:36:33
1、leetcode 英文网址:https://leetcode.com/ 中文网址:https://leetcode-cn.com/ 估计 leetcode(力扣)大家都很熟悉了,都被推荐烂了,很多国内外的程序员在上面刷题,难度从 Easy、Medium 至 Hard 都有,据说很多面试官都会从中挑选各种题目,号称大厂的筛码工。 2、hihoCoder :网址:https://hihocoder.com 网站的技术团队来自于原北大 POJ 的开发团队,至于 POJ 会在后面的篇章中介绍,反正膜拜就完事了。一些知名的大厂比如微软、百度、腾讯、网易等会在上面举办在线编程比赛,风格倒是和 ACM 比赛类似。 hihoCoder 每周有周赛,每月有月赛。周赛是一道题,题目比较难但是极有意思,可以很好的拓宽自己的解题思路,月赛就更厉害了,题目均出自北大等一流高校玩 ACM 的菊苣出题,通过这个的检验可以迅速定位到自己真实的水平,同时了解自身在解决问题过程中的不足。 3、牛客网:网址:https://www.nowcoder.com/ 牛客网作为国内内容超级丰富的 IT 题库,各种东西看的我眼花缭乱,题库+面试+学习+求职+讨论 360 度无死角服务,堪称"互联网求职神器"。它好就好在不只是一个刷题的平台,还是一个交流学习的平台,发个问题贴总有热心的大佬帮助,别问我怎么知道