Leetcode
文章平均质量分 76
moomin的小粉丝
姜饼的学习记录
展开
-
572. Subtree of Another Tree
题目Given two non-empty binary trees s and t, check whether tree t has exactly the same structure and node values with a subtree of s. A subtree of s is a tree consists of a node in s and all of this n...原创 2019-12-09 00:14:58 · 158 阅读 · 0 评论 -
560. Subarray Sum Equals K
题目Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k.Example 1:Input:nums = [1,1,1], k = 2Output: 2Note:The length of th...原创 2019-12-08 23:33:39 · 176 阅读 · 0 评论 -
543. Diameter of Binary Tree
题目Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or ma...原创 2019-12-07 06:45:08 · 128 阅读 · 0 评论 -
503. Next Greater Element II
题目Given a circular array (the next element of the last element is the first element of the array), print the Next Greater Number for every element. The Next Greater Number of a number x is the first ...原创 2019-12-07 05:47:09 · 128 阅读 · 0 评论 -
347. Top K Frequent Elements
题目Given a non-empty array of integers, return the k most frequent elements.Example 1:Input: nums = [1,1,1,2,2,3], k = 2Output: [1,2]我的想法用HashMap存频率,再根据频数进行快速排序或者归并排序。但不知道怎样存元素,class Solution {...原创 2019-12-07 00:57:12 · 140 阅读 · 0 评论 -
350. Intersection of Two Arrays II
题目Given two arrays, write a function to compute their intersection.Example 1:Input: nums1 = [1,2,2,1], nums2 = [2,2]Output: [2,2]Example 2:Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]Output: [4,...原创 2019-12-06 08:40:43 · 80 阅读 · 0 评论 -
148. Sort List
题目Sort a linked list in O(n log n) time using constant space complexity.Example 1:Input: 4->2->1->3Output: 1->2->3->4我的想法感觉用归并排序不会太复杂,但是实际写的时候越写越晕。以下代码并不对,只是为了记录class Solutio...原创 2019-12-06 01:21:23 · 112 阅读 · 0 评论 -
912. Sort an Array
题目Given an array of integers nums, sort the array in ascending order.我的想法快排模板注意!!!为了使左右指针错开,以防止死循环,快排的所有判断都要带等号!class Solution { public List<Integer> sortArray(int[] nums) { qui...原创 2019-12-05 10:37:39 · 90 阅读 · 0 评论 -
138. Copy List with Random Pointer
题目A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.Return a deep copy of the list.我的想法在循环建立新链表的时候,用一个hashmap存入所有新建...原创 2019-12-05 01:30:05 · 90 阅读 · 0 评论 -
116. Populating Next Right Pointers in Each Node
题目You are given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:struct Node { int val; Node *left; ...原创 2019-12-05 01:04:25 · 101 阅读 · 0 评论 -
109. Convert Sorted List to Binary Search Tree
题目Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which th...原创 2019-12-04 12:06:38 · 94 阅读 · 0 评论 -
103. Binary Tree Zigzag Level Order Traversal
题目Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).我的想法层级遍历二叉树,逢偶倒转listclass...原创 2019-12-04 08:47:09 · 97 阅读 · 0 评论 -
76. Minimum Window Substring
题目Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).Example:Input: S = “ADOBECODEBANC”, T = “ABC”Output: “BANC”Note:If t...原创 2019-12-04 07:51:57 · 85 阅读 · 0 评论 -
101. Symmetric Tree
题目Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3B...原创 2019-12-04 06:18:52 · 83 阅读 · 0 评论 -
240. Search a 2D Matrix II
题目Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in eac...原创 2019-12-02 00:35:44 · 96 阅读 · 0 评论 -
448. Find All Numbers Disappeared in an Array
题目Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.Find all the elements of [1, n] inclusive that do not appear in this array.Cou...原创 2019-11-28 04:42:12 · 94 阅读 · 0 评论 -
212. Word Search II
题目Given a 2D board and a list of words from the dictionary, find all words in the board.Each word must be constructed from letters of sequentially adjacent cell, where “adjacent” cells are those hor...原创 2019-11-27 08:58:51 · 106 阅读 · 0 评论 -
215. Kth Largest Element in an Array
题目Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.我的想法没太懂,感觉就是个排序的问题,为什么能算mediumclass Solution { pu...原创 2019-11-27 05:51:15 · 102 阅读 · 0 评论 -
139. Word Break
题目Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words.Note:Th...原创 2019-11-27 01:00:10 · 88 阅读 · 0 评论 -
472. Concatenated Words
题目Given a list of words (without duplicates), please write a program that returns all concatenated words in the given list of words.A concatenated word is defined as a string that is comprised entir...原创 2019-11-26 06:12:17 · 90 阅读 · 0 评论 -
545. Boundary of Binary Tree
题目Given a binary tree, return the values of its boundary in anti-clockwise direction starting from root. Boundary includes left boundary, leaves, and right boundary in order without duplicate nodes....原创 2019-11-26 01:34:58 · 116 阅读 · 0 评论 -
155. Min Stack
题目Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get t...原创 2019-11-24 08:37:30 · 93 阅读 · 0 评论 -
173. Binary Search Tree Iterator
题目Design an iterator over a binary search tree with the following rules:Elements are visited in ascending order (i.e. an in-order traversal)next() and hasNext() queries run in O(1) time in average....原创 2019-11-24 08:01:42 · 77 阅读 · 0 评论 -
272. Closest Binary Search Tree Value II
题目Given a non-empty binary search tree and a target value, find k values in the BST that are closest to the target.Example 1:Input:{1}0.0000001Output:[1]我的想法跟二分法的658. Find K Closest Elements...原创 2019-11-24 06:51:33 · 127 阅读 · 0 评论 -
680. Valid Palindrome II
题目Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome.Example 1:Input: “aba”Output: TrueNote:The string will only contain lowercase chara...原创 2019-11-16 01:21:17 · 79 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
题目Write a program to find the node at which the intersection of two singly linked lists begins.Notes:If the two linked lists have no intersection at all, return null.The linked lists must retain ...原创 2019-11-15 08:45:00 · 98 阅读 · 0 评论 -
202. Happy Number
题目Write an algorithm to determine if a number is “happy”.A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares...原创 2019-11-14 21:46:23 · 89 阅读 · 0 评论 -
56. Merge Intervals
题目Given a collection of intervals, merge all overlapping intervals.Example 1:Input: [[1,3],[2,6],[8,10],[15,18]]Output: [[1,6],[8,10],[15,18]]Explanation: Since intervals [1,3] and [2,6] overlaps...原创 2019-11-12 23:35:10 · 90 阅读 · 0 评论 -
238. Product of Array Except Self
题目Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Example:Input: [1,2,3,4]Output: ...原创 2019-11-12 05:07:43 · 96 阅读 · 0 评论 -
21. Merge Two Sorted Lists
题目Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.我的想法注意是把旧的node连接起来,不能new新的点。l1和l2长度不一定相等,最后记得把l1或者l2剩下...原创 2019-11-11 23:25:25 · 81 阅读 · 0 评论 -
15. 3Sum
题目Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not con...原创 2019-11-11 22:59:39 · 88 阅读 · 0 评论 -
53. Maximum Subarray
题目Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.Example:Input: [-2,1,-3,4,-1,2,1,-5,4],Output: 6Explanati...原创 2019-11-11 11:42:56 · 93 阅读 · 0 评论 -
4. Median of Two Sorted Arrays
题目There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).You may assume nums1 and ...原创 2019-11-11 05:21:35 · 76 阅读 · 0 评论 -
146. LRU Cache
题目Design and implement a data structure for Least Recently Used (LRU) cache. It should support the following operations: get and put.get(key) - Get the value (will always be positive) of the key if ...原创 2019-11-08 10:20:35 · 107 阅读 · 0 评论 -
270 Closest Binary Search Tree Value
题目Given a non-empty binary search tree and a target value, find the value in the BST that is closest to the target.我的想法最开始的想法是,当前结点的差值与左右子结点的差值比较,如果当前最小则输出当前,否则将当前结点设为较小的那个点,在继续循环。但是这样有个问题,当前最小并不一定...原创 2019-10-22 10:28:01 · 109 阅读 · 0 评论 -
114. Flatten Binary Tree to Linked List
题目Given a binary tree, flatten it to a linked list in-place.我的想法定义一个全局变量lastNode来存储前序遍历的最后一个结点,每次只需要把右子树的点接在lastNode后面即可。实现起来觉得很绕,写不出。class Solution { TreeNode lastNode; public void flatte...原创 2019-10-22 00:36:14 · 103 阅读 · 0 评论 -
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: “The lowest common ancestor is defined between two nodes ...原创 2019-10-19 01:13:54 · 88 阅读 · 0 评论 -
257. Binary Tree Paths
题目Given a binary tree, return all root-to-leaf paths.我的想法遍历法,需要注意不能在root == null时把结果加入list,这样会重复计算,而且并没有走到真正的叶子结点(缺少一边的子树也会输出,但实际上这并不是叶子结点)public class Solution { public List<String> bin...原创 2019-10-18 22:59:20 · 129 阅读 · 0 评论 -
Lintcode 596. Minimum Subtree
题目Given a binary tree, find the subtree with minimum sum. Return the root of the subtree.我的想法结合分治和遍历。利用遍历中的全局变量来记录最小值,利用分治的向上汇报来计算每一个结点的数值和class Result { TreeNode root; int sum; Result...原创 2019-10-16 22:10:24 · 234 阅读 · 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 sam...原创 2019-03-26 12:25:49 · 203 阅读 · 0 评论