
LeetCode刷题笔记
记录刷题的思路和代码
两鬓已不能斑白
这个作者很懒,什么都没留下…
展开
-
2020-11-30
Longest Valid ParenthesesGiven a string containing just the characters ‘(’ and ‘)’, find thelength of the longest valid (well-formed) parentheses substring.Example 1:Input: s = “(()”Output: 2 Explanation: The longest valid parenthesessubstring is .原创 2020-11-30 21:46:22 · 381 阅读 · 0 评论 -
2020-11-30
Merge k Sorted ListsYou are given an array of k linked-lists lists, each linked-list is sorted in ascending order.Merge all the linked-lists into one sorted linked-list and return it.Example 1:Input: lists = [[1,4,5],[1,3,4],[2,6]]Output: [1,1,2,3,.原创 2020-11-30 21:02:29 · 335 阅读 · 0 评论 -
LeetCode刷题笔记目录
1、Two Sumhttps://blog.youkuaiyun.com/u010429424/article/details/7764898927、Remove Elementhttps://blog.youkuaiyun.com/u010429424/article/details/7326056453、Maximum Subarrayhttps://blog.youkuaiyun.com/u010429424/art...原创 2019-03-01 22:59:39 · 518 阅读 · 0 评论 -
128. Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4].原创 2017-06-02 09:09:53 · 377 阅读 · 0 评论 -
27. Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.The orde原创 2017-06-14 23:26:46 · 332 阅读 · 0 评论 -
Leetcode 98. Validate Binary Search Tree
Leetcode 98. Validate Binary Search Tree Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only no原创 2017-09-04 09:16:14 · 654 阅读 · 0 评论 -
Leetcode 67. Add Binary
Leetcode 67. Add Binary———————– Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".思路:用字符串模拟二进制加法,代码如下:class Solution { public Str原创 2017-08-30 10:34:37 · 348 阅读 · 0 评论 -
Leetcode 523.Continuous Subarray Sum
Leetcode 523.Continuous Subarray Sum Given a list of non-negative numbers and a target integer k, write a function to check if the array has a continuous subarray of size at least 2 that sums原创 2017-09-01 10:26:56 · 623 阅读 · 0 评论 -
Leetcode 53. Maximum Subarray
Leetcode 53. Maximum Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the原创 2017-09-01 13:38:32 · 563 阅读 · 0 评论 -
Leetcode 653. Two Sum IV - Input is a BST
Leetcode 653. Two Sum IV - Input is a BST—————————————– Given a Binary Search Tree and a target number, return true if there exist two elements in the BST such that their sum is equal to the g原创 2017-08-30 09:38:05 · 1980 阅读 · 0 评论 -
Leetcode 415. Add Strings
Leetcode 415. Add Strings Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2.Note: The length of both num1 and num2 is < 5100.Both num1 an原创 2017-08-30 10:19:51 · 627 阅读 · 0 评论 -
1、Two Sum
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 ele原创 2017-08-28 15:14:32 · 1307 阅读 · 0 评论 -
LeetCode 236. Lowest Common Ancestor of a Binary Tree
LeetCode 236. Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia:原创 2017-08-28 16:43:59 · 1210 阅读 · 0 评论 -
LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikip原创 2017-08-28 16:51:35 · 650 阅读 · 0 评论 -
538. Convert BST to Greater Tree
Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.原创 2017-07-18 15:12:46 · 421 阅读 · 0 评论 -
171. Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ... Z -> 26原创 2017-07-18 23:22:54 · 515 阅读 · 0 评论 -
263. Ugly Number
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is no原创 2017-07-19 23:30:20 · 391 阅读 · 0 评论 -
79. Word Search
Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those horizontally or vertically nei原创 2017-07-13 12:10:47 · 339 阅读 · 0 评论 -
530. Minimum Absolute Difference in BST
Given a binary search tree with non-negative values, find the minimum absolute difference between values of any two nodes. Example: Input: 1 \ 3 / 2Output: 1Explanation: The m原创 2017-07-13 14:10:17 · 329 阅读 · 0 评论 -
637. Average of Levels in Binary Tree
Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.Example 1:Input: 3 / \ 9 20 / \ 15 7Output: [3, 14.5, 11]Explanation:T原创 2017-07-17 21:17:54 · 1182 阅读 · 0 评论 -
599. Minimum Index Sum of Two Lists
599. Minimum Index Sum of Two Lists Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings. You need原创 2017-07-06 17:06:18 · 593 阅读 · 0 评论 -
蛇形矩阵
题目描述 蛇形矩阵是由1开始的自然数依次排列成的一个矩阵上三角形。 样例输入 5 样例输出 1 3 6 10 15 2 5 9 14 4 8 13 7 12 11思路: 找规律,写了两层for循环Java 代码:import java.util.*;public class Main { public static vo原创 2017-07-01 20:56:53 · 565 阅读 · 1 评论 -
Leetcode 606. Construct String from Binary Tree
Leetcode 606. Construct String from Binary Tree———————————————– You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null原创 2017-07-04 13:12:21 · 1509 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
题意如下:对于一个长度为n的数组,其内部元素均由1 ~ n组成,有些元素重复出现了2次,有些元素出现了1次,有些元素从未出现,求那些从未出现的元素。要求:额外空间复杂度为O(1), 时间复杂度为O(n)解法:如果不考虑空间复杂度和时间复杂度,可以使用两层循环,统计每一个元素在数组中出现的次数,优点是好写,但是当数据量大时容易超时。标志位法:用正负标志位,区分出现的元素和原创 2017-04-07 09:48:42 · 719 阅读 · 0 评论 -
19. Remove Nth Node From End of List
题意如下: 给定一个链表和整数n,删除链表中的倒数第n个元素 例如: 1 -> 2 -> 3 -> 4 -> 5, n = 2 则删除4,返回1 -> 2 -> 3 -> 5 要求只能遍历一遍列表我的思路如下: 用到三个指针:pre、p、q。pre指向p的前一个元素;p指向待删除的元素;q指向链表中最后一个元素; 起初,p指向链表头结点head,q从头结点head向后移动到第n位。然后原创 2017-04-07 10:15:34 · 297 阅读 · 0 评论 -
62. Unique Paths
题意大致为: 一个机器人从网格的左上角Start,走到右下角Finish,则不同路径的走法有多少种。输入是网格的行、列值。 这是一道典型的动态规划题,首先,初始化一个m x n的数组arr,(i,j)位置上记录走到(i,j)时的走法数。 第一行均初始化为1,因为对于第一行的元素而言,想要走到它,只能是从左一路走过来。 第一列均初始化为1, 因为对于第一列的元素而言,想要走到它,只能是从上一路原创 2017-04-07 16:48:14 · 638 阅读 · 1 评论 -
120. Triangle
题目大意: 有一个三角形矩阵,从最上边的顶点出发累加到底边,一共有多种情况,求这些情况中累加和的最小值 要求额外空间复杂度为O(n),n是行数思路: 遍历矩阵,每走一步都更新当前值: triangle[i][j] += min(triangle[i-1][j-1], triangle[i-1][j]) Python代码如下:class Solution(object): def m原创 2017-04-10 14:41:20 · 450 阅读 · 0 评论 -
237. Delete Node in a Linked List
题目大意: 有一个链表如:1->2->3->4->5,要删除3这个节点,求删除后的链表思路:题目没有给出链表的头结点,也就获取不了待删除点的前驱结点,因此采用复制后一个元素的值并替代前一个元素值的方式,实现元素的删除 Python代码:# Definition for singly-linked list.# class ListNode(object):# def __init原创 2017-04-11 09:10:56 · 270 阅读 · 0 评论 -
21. Merge Two Sorted Lists
题目大意: 有两个已经排好序的链表,现在要合并这两个链表,同时保证大小顺序不变,返回合并后的链表,如:1->3->5和1->4合并后为:1->1->3->4->5思路: 令i,j分别为两个链表的起始节点,选择值较小的那个节点,加入到返回链表中,此处i指向的节点的值为1,j指向节点的值为1,两个值相等,(相等时默认加入i),此处把 i 指向的节点加入到返回链表中,随后 i 后移一位。 之后,i指原创 2017-04-11 17:28:09 · 564 阅读 · 0 评论 -
451. Sort Characters By Frequency
题目大意: 按照字符串中的字符出现次数排序,注意大写和小写字符要区分开。一道有点麻烦的题,用一个Map存储字符及出现的次数,然后重写comparator对Map进行排序。 Java代码如下:import java.util.*;import java.util.Map.Entry;public class Solution { public List<Map.Entry<Charact原创 2017-04-11 17:31:54 · 332 阅读 · 0 评论 -
204. Count Primes
题目大意: 给一个非负整数n,求小于n的素数的个数思路: 第一反应是采用素数的定义来做。(1)如果采用素数的定义,即不能被1和它本身整除,需要从2计算到它减一,举个栗子,如果要验证17是素数,则需要计算17 / 2,17 / 3,17 / 4 … 17 / 16,O(n)级别的复杂度。伪代码如下:for(int i = 2; i < n; i++) { // 此处是判断n是否可以被i整除原创 2017-04-12 11:37:18 · 344 阅读 · 0 评论 -
73. Set Matrix Zeroes
题目大意: 给出一个m * n的矩阵,如果矩阵中的某个元素为0,则将该元素所在的整行和整列设为0,要求空间复杂度为O(1),举个栗子: [0,1,2,3] [0,0,0,0] [4,5,6,7] -&gt; [0,5,6,7] [8,9,1,2] [0,9,1,2] 思路: 如果放宽对空间复杂度的限制,可以将原始矩阵复制一份,一边遍历原始矩阵,一边修改复制矩阵,这样原创 2017-04-13 09:15:31 · 391 阅读 · 0 评论 -
290. Word Pattern
题目大意: 给定两个字符串pattern和str,判断str的模式是否和pattern一直思路: 一开始没什么思路,直接写了两层for循环,O(n2)的代码辣眼睛,提交坐等超时,然而并没有超时,持续懵逼中。Python代码 arr = str.split(' ') print(arr) if len(pattern) != len(arr):原创 2017-04-13 09:36:45 · 541 阅读 · 0 评论 -
206. Reverse Linked List
题目大意: 反转一个链表,要求分别用迭代和递归实现 Python代码:# Definition for singly-linked list.class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def原创 2017-04-13 16:17:23 · 303 阅读 · 0 评论 -
226. Invert Binary Tree
题目大意: 翻转二叉树思路:递归一下,递归比迭代好写Python代码:# Definition for a binary tree node.class TreeNode(object): def __init__(self, x): self.val = x self.left = None self.right = Noneclass原创 2017-04-13 23:19:01 · 274 阅读 · 0 评论 -
Remove Duplicates系列笔记
第一题Python代码:# Definition for singly-linked list.class ListNode(object): def __init__(self, x): self.val = x self.next = Noneclass Solution(object): def deleteDuplicates(self, h原创 2017-04-14 10:43:13 · 2055 阅读 · 0 评论 -
435. Non-overlapping Intervals
Given a collection of intervals, find the minimum number of intervals you need to remove to make the rest of the intervals non-overlapping. Note:You may assume the interval’s end point is always bigge原创 2017-04-16 16:02:20 · 353 阅读 · 0 评论 -
203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6Return: 1 –> 2 –> 3 –> 4 –> 5Credits:Special thanks to @mithmatt for add原创 2017-04-16 16:49:16 · 396 阅读 · 0 评论 -
496. Next Greater Element I
You are given two arrays (without duplicates) nums1 and nums2 where nums1’s elements are subset of nums2. Find all the next greater numbers for nums1’s elements in the corresponding places of nums2. Th原创 2017-04-17 10:19:39 · 339 阅读 · 0 评论 -
349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example:Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2].Note:Each element in the result must be unique.The result can be in原创 2017-04-17 19:04:53 · 455 阅读 · 0 评论