![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
weixin_28739073
这个作者很懒,什么都没留下…
展开
-
108. Convert Sorted Array to Binary Search Tree
题目:Given an array 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 the depth of t...原创 2019-01-04 05:59:46 · 63 阅读 · 0 评论 -
101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left...原创 2019-01-10 06:33:54 · 49 阅读 · 0 评论 -
Reverse Linkedlist
Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL 思路: 一个node指向前一个(需要用temp存储.next本来该指向的node),然后向后移动,pre=cur,cur=temp 算法: /** ...原创 2019-01-16 04:15:43 · 102 阅读 · 0 评论 -
int和Integer的区别
-----int和Integer的区别 1.从定义上来看 int 是基本类型,直接存数值(类似的还有float、double、String、char) Integer是对象,用一个引用指向这个对象(类似的有Float、Double、String) 2.从复杂度来看,Java 中的数据类型分为基本数据类型和复杂数据类型 int 是前者;Integer 是后者(也就是一个类) 3.初始化的方式不同 i...转载 2019-01-27 04:41:51 · 123 阅读 · 0 评论 -
136. Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one. Input: [2,2,1] Output: 1 class Solution { public int singleNumber(int[] nums) { //hashs...原创 2019-01-27 04:42:43 · 53 阅读 · 0 评论 -
170. Two Sum III - Data structure design
Design and implement a TwoSum class. It should support the following operations: add and find. add - Add the number to an internal data structure. find - Find if there exists any pair of numbers which...原创 2019-01-27 05:24:56 · 61 阅读 · 0 评论 -
75. Sort Colors
Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the int...原创 2019-01-22 04:38:33 · 55 阅读 · 0 评论 -
121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), ...原创 2019-02-01 03:25:37 · 65 阅读 · 0 评论 -
702. Search in a Sorted Array of Unknown Size
Given an integer array sorted in ascending order, write a function to search target in nums. If target exists, then return its index, otherwise return -1. However, the array size is unknown to you. Y...原创 2019-02-01 07:05:23 · 211 阅读 · 0 评论 -
283. Move Zeroes
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. class Solution { public void moveZeroes(int[] nums) { if(n...原创 2019-01-23 02:42:44 · 50 阅读 · 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 of...原创 2019-01-29 04:12:07 · 56 阅读 · 0 评论 -
110. Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree in which the depth of the two subtrees of every node never differ ...原创 2019-02-06 05:03:13 · 102 阅读 · 0 评论 -
133. Clone Graph
Given the head of a graph, return a deep copy (clone) of the graph. Each node in the graph contains a label (int) and a list (List[UndirectedGraphNode]) of its neighbors. There is an edge between the ...原创 2019-02-07 05:35:39 · 88 阅读 · 0 评论 -
429. N-ary Tree Level Order Traversal
Given an n-ary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example, given a 3-ary tree: /* // Definition for a Node. class Node { ...原创 2019-02-08 08:13:56 · 108 阅读 · 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 the...原创 2019-01-30 05:08:02 · 146 阅读 · 0 评论 -
64. Minimum Path Sum(DP)
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. Note: You can only move either down or right at an...原创 2019-01-31 06:17:30 · 76 阅读 · 0 评论 -
261. Graph Valid Tree(BFS版本)
Given n nodes labeled from 0 to n-1 and a list of undirected edges (each edge is a pair of nodes), write a function to check whether these edges make up a valid tree. class Solution { public boole...原创 2019-01-10 04:21:09 · 160 阅读 · 0 评论 -
102. Binary Tree Level Order Traversal(BFS模板背诵并记忆)
Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). class Solution { public List<List<Integer>> levelOrder(TreeNode ro...原创 2019-01-10 03:27:36 · 131 阅读 · 0 评论 -
46. Permutations
Given a collection of distinct integers, return all possible permutations. class Solution { public List<List<Integer>> permute(int[] nums) { List<List<Integer>> res...原创 2019-01-15 08:50:52 · 69 阅读 · 0 评论 -
257. Binary Tree Paths
Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. 解题思路:建立一个新的ArrayList,站在root,左右分别处理,然后左右两边分别定义一个path来存储左子树再加上根节点,最后判断是否为叶子节点。 class Solution { public Li...原创 2019-01-04 08:24:33 · 96 阅读 · 0 评论 -
104. Maximum Depth of Binary Tree(1.private 2.void)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. /** * Definition for a binary tree node...原创 2019-01-05 04:00:29 · 66 阅读 · 0 评论 -
207. Course Schedule(拓扑排序topological sort 高频题目)
There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:...原创 2019-01-11 04:32:45 · 102 阅读 · 0 评论 -
559. Maximum Depth of N-ary Tree
Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. /* // Definition for a Node. class Node {...原创 2019-01-11 05:38:47 · 59 阅读 · 0 评论 -
98. Validate Binary Search Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { ...原创 2019-01-06 05:43:36 · 67 阅读 · 0 评论 -
236. Lowest Common Ancestor of a Binary Tree
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1 Output: 3 Explanation: The LCA of nodes 5 and 1 is 3. /** * Definition for a binary tree node. * public class TreeNode { * int val; * ...原创 2019-01-06 13:19:41 · 58 阅读 · 0 评论 -
81. Search in Rotated Sorted Array II
需要消除重复的元素,跳过 while(nums[start]==nums[mid]){ start++; } 完整代码 class Solution { public boolean search(int[] nums, int target) { //check errors if(nums==null||nums.length==0){ return ...原创 2019-01-02 11:31:39 · 61 阅读 · 0 评论 -
287. Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fi...原创 2019-01-12 02:40:51 · 46 阅读 · 0 评论 -
Merge Two Sorted Arrays
Merge two given sorted integer array A and B into a new sorted integer array. 思路: 两个排好序的数组合并在一起,取两数组第一个比大小,小的出来组成result的第一个,然后循环再一个个比,直到A数组或者B数组有一个变成空的,然后把剩下的不空的加到result里。 算法:定义result的长度为A和B长度之和,设置三个变...原创 2019-01-18 04:43:34 · 137 阅读 · 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. Dummy node /** * Definition for singly-linked list. * pu...原创 2019-01-18 06:32:17 · 61 阅读 · 0 评论 -
39. Combination Sum
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target. The same repeate...原创 2019-01-13 08:27:11 · 157 阅读 · 0 评论 -
173. Binary Search Tree Iterator(背诵并记忆)
题目:Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling next() will return the next smallest number in the BST. Stack 在java B...原创 2019-01-08 04:15:39 · 84 阅读 · 0 评论 -
285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. The idea is to compare root’s value with p’s value if root is not null, and consider the following two...原创 2019-01-08 12:01:13 · 57 阅读 · 0 评论 -
349. Intersection of Two Arrays
Given two arrays, write a function to compute their intersection. Example: Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2] 三种方法: 1.sort&merge 2.hashset 3.binary search //binary search class ...原创 2019-01-19 04:04:12 · 57 阅读 · 0 评论 -
15. 3Sum(综合题)---unique pair
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. class Solution { public List<Li...原创 2019-01-20 06:39:15 · 136 阅读 · 0 评论 -
129. Rehashing(当hashtable不够时需要扩容)
/** * Definition for ListNode * public class ListNode { * int val; * ListNode next; * ListNode(int x) { * val = x; * next = null; * } * } */ public class Solu...原创 2019-01-25 03:57:57 · 217 阅读 · 0 评论 -
300. Longest Increasing Subsequence
Given an unsorted array of integers, find the length of longest increasing subsequence. Input: [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefor...原创 2019-01-31 06:34:53 · 82 阅读 · 0 评论