![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
github_34514750
这个作者很懒,什么都没留下…
展开
-
leetcode-java-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; } * } */ public class Solution {原创 2016-06-30 15:21:23 · 468 阅读 · 0 评论 -
leetcode-java-108. Convert Sorted Array to Binary Search Tree
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *//*二叉搜索树特点:小的值在左边,大的值在右边这原创 2016-08-16 14:45:18 · 917 阅读 · 0 评论 -
leetcode-java-217. Contains Duplicate
/*Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every elemen原创 2016-08-27 13:22:41 · 425 阅读 · 0 评论 -
leetcode-java-219. Contains Duplicate II
/*Given an array of integers and an integer k, find out whetherthere are two distinct indices i and j in the array such thatnums[i] = nums[j] and the difference between i and j is at most k. *//*原创 2016-08-27 14:53:50 · 517 阅读 · 0 评论 -
leetcode-java-189. Rotate Array
/*Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7]is rotated to [5,6,7,1,2,3,4].Note: Try to come up as many solutions as you can,原创 2016-08-27 15:19:31 · 502 阅读 · 0 评论 -
leetcode-java-257. Binary Tree Paths
/*思路:1.采用递归左子树递归找到路径(根节点+左子树)右子树递归找到路径(根节点+右子树)2.递归停止条件:递归到叶子节点时终止,即root.left==null && root.right == null时,将字符串add到result中。 */ public class Solution { public List<String> binaryTreePaths(T原创 2016-08-17 20:55:44 · 825 阅读 · 0 评论 -
leetcode-java-110. Balanced Binary Tree
/*平衡二叉树:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过11.递归判断二叉树是否是平衡二叉树2.递归找二叉树高度,求高度也是用递归 递归停止条件: 递归到叶子节点时终止,即root.left==null && root.right == null时,返回1;root==null时,返回0。 */ public class Solution { pub原创 2016-08-17 21:14:09 · 626 阅读 · 0 评论 -
leetcode-java-88. Merge Sorted Array
/*Given two sorted integer arrays nums1 and nums2, merge nums2 intonums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addit原创 2016-08-28 10:10:49 · 964 阅读 · 0 评论 -
leetcode-java-229. Majority Element II
/*Given an integer array of size n, find all elements that appearmore than ⌊ n/3 ⌋ times.The algorithm should run in linear time and in O(1) space. */ public class Solution { public List<Integ原创 2016-08-28 13:27:19 · 448 阅读 · 0 评论 -
leetcode-java-105. Construct Binary Tree from Preorder and Inorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *//*1.根据preorder确定根节点2.在in原创 2016-08-15 15:54:54 · 251 阅读 · 0 评论 -
leetcode-java-107. Binary Tree Level Order Traversal II
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *//*先层次遍历出list再让list倒置 */原创 2016-08-15 15:56:10 · 356 阅读 · 0 评论 -
leetcode-java-58. Length of Last Word
思路: 1.先处理最后的空格,跳过空格 2.倒着遍历字符串,不为空是conut++,但是空则breakpublic class Solution { public int lengthOfLastWord(String s) { int len = s.length(), i = len - 1, count = 0;原创 2016-06-14 21:54:42 · 781 阅读 · 0 评论 -
leetcode-java-101. Symmetric Tree
思路一: 递归:1ms 先判断左右子节点是否相等,不等就返回false 相等则判断左节点的左孩子和右节点的右孩子是否相等,且判断左节点的右孩子和右节点的左孩子是否相等。 思路二: 迭代public class Solution { public boolean isSymmetric(TreeNode root) { if(root == null) {原创 2016-06-30 21:21:04 · 257 阅读 · 0 评论 -
leetcode-java-225. Implement Stack using Queues
class MyStack { // Push element x onto stack. List<Integer> queue1 = new LinkedList<Integer>(); List<Integer> queue2 = new LinkedList<Integer>(); public void push(int x) { if(!qu原创 2016-06-15 22:04:03 · 575 阅读 · 0 评论 -
leetcode-java-94. Binary Tree Inorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ /* 使用循环来解决,递归和迭代都是循环 递归原创 2016-08-15 15:51:48 · 214 阅读 · 0 评论 -
leetcode-java-95. Unique Binary Search Trees II
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } *//*二叉搜索树BST:从微观上来讲,BST的每个原创 2016-08-15 15:52:30 · 1064 阅读 · 0 评论 -
leetcode-java-102. Binary Tree Level Order Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } * Given a binary tree, retur原创 2016-08-15 15:53:19 · 301 阅读 · 0 评论 -
leetcode-java-103. Binary Tree Zigzag Level Order Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } * 仿照102的思路: * 但是因为是zigzag的方法原创 2016-08-15 15:54:07 · 374 阅读 · 0 评论 -
leetcode-java-106. Construct Binary Tree from Inorder and Postorder Traversal
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ /* 1.根据postorder确定根节点 2原创 2016-08-15 15:55:35 · 223 阅读 · 0 评论 -
leetcode-java-199. Binary Tree Right Side View
/*思路:从右边看,并不是指右节点--也就是每层最右边的节点,则考虑层次遍历只取最右 */ public class Solution { public List<Integer> rightSideView(TreeNode root) { List<Integer> list = new LinkedList<Integer>();原创 2016-08-24 14:51:46 · 552 阅读 · 0 评论 -
leetcode-java-39. Combination Sum
/*Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number原创 2016-08-28 18:06:43 · 369 阅读 · 0 评论 -
leetcode-java-40. Combination Sum II
/*Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number原创 2016-08-28 18:07:26 · 383 阅读 · 0 评论 -
leetcode-java-12. Integer to Roman
/*Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999. *//*基本字符和阿拉伯数字的对应I-1V-5X-10L-50C-100D-500M-1000(1)相同的数字连写、所表示的数等于这些数字相加得到的数、如:Ⅲ=原创 2016-08-22 10:54:12 · 242 阅读 · 0 评论 -
leetcode-java-111. Minimum Depth of Binary Tree
/*最小深度就是根节点到最近叶子节点的路径长度和找二叉树的最大深度类似,但是区别如下:递归终止条件:1.根节点为null时,返回02.根节点左右都null时,返回13.大返回终止条件:返回较小值+1但是需要注意的是:这样就无法保证root是否是叶子节点了, */ public class Solution { public int minDepth(TreeNode原创 2016-08-24 10:33:19 · 974 阅读 · 0 评论 -
leetcode-java-230. Kth Smallest Element in a BST
/*Given a binary search tree, write a function kthSmallest to find the kth smallest element in it.Note: You may assume k is always valid, 1 ≤ k ≤ BST's total elements.Follow up: What if the BST is原创 2016-08-24 11:20:34 · 355 阅读 · 0 评论 -
leetcode-java-162. Find Peak Element
/*A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak elementand return its index.The array may contain multiple peaks, in that原创 2016-09-04 12:45:38 · 316 阅读 · 0 评论 -
leetcode-java-167. Two Sum II - Input array is sorted
/*Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers原创 2016-09-04 12:46:17 · 382 阅读 · 0 评论 -
leetcode-java-289. Game of Life
/*According to the Wikipedia's article: "The Game of Life, also knownsimply as Life, is a cellular automaton devised(发明) by the Britishmathematician John Horton Conway in 1970."Given a board with m原创 2016-09-04 12:47:14 · 482 阅读 · 0 评论 -
leetcode-java-75. Sort Colors
/*Given an array with n objects colored red, white or blue, sort them so thatobjects of the same color are adjacent, with the colors in the order red,white and blue.Here, we will use the integers 0原创 2016-09-04 14:00:58 · 452 阅读 · 0 评论 -
leetcode-java-116. Populating Next Right Pointers in Each Node
/*Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node. If ther原创 2016-08-25 11:08:20 · 294 阅读 · 0 评论 -
leetcode-java-279. Perfect Squares
public class Solution { public int numSquares(int n) { int[] dp = new int[n+1]; // 确保后边选min的正确 Arrays.fill(dp,Integer.MAX_VALUE); for(int i = 0;i*i <= n;i++) {原创 2016-08-22 10:12:34 · 298 阅读 · 0 评论 -
leetcode-java-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 not ugly since it原创 2016-08-21 19:11:58 · 410 阅读 · 0 评论 -
leetcode-java-216. Combination Sum III
/*Given a set of candidate numbers (C) and a target number (T),find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited number原创 2016-08-28 18:08:05 · 432 阅读 · 0 评论 -
leetcode-java-48. Rotate Image
/*You are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up: Could you do this in-place? *//*思路:本题要求不开辟新的空间,且矩阵行列相同其实就是1.先对角线做转置(左下到右上)2.在竖直原创 2016-08-29 14:31:36 · 387 阅读 · 0 评论 -
leetcode-java-54. Spiral Matrix
/*Given a matrix of m x n elements (m rows, n columns), return all elementsof the matrix in spiral(螺旋) order.For example, Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]Yo原创 2016-08-29 16:33:05 · 412 阅读 · 0 评论 -
leetcode-java-59. Spiral Matrix II
/*Given an integer n, generate a square matrix filled with elements from 1 to n^2in spiral order.For example, Given n = 3,You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7原创 2016-08-29 16:33:52 · 399 阅读 · 0 评论 -
leetcode-java-377. Combination Sum IV
/*Given an integer array with all positive numbers and no duplicates,find the number of possible combinations that add up to a positive integer target.nums = [1, 2, 3]target = 4The possible combina原创 2016-08-20 11:46:54 · 802 阅读 · 0 评论 -
leetcode-java-326. Power of Three
/*Given an integer, write a function to determine if it is a power of three.1.递归2.数学方法x = log(n)/log(3),x是整数就可以了--那么判断整数方法:double类型,则绝对值差小于10e-15;floor类型,则绝对值差小于10e-6 */ public class Solution {原创 2016-08-21 10:42:11 · 251 阅读 · 0 评论 -
leetcode-java-367. Valid Perfect Square
/*Given a positive integer num, write a function whichreturns True if num is a perfect square else False.Example 1:Input: 16Returns: TrueExample 2:Input: 14Returns: False本题就是取平方根,然后判断得到的平方根是否是整数原创 2016-08-21 18:08:23 · 444 阅读 · 0 评论 -
leetcode-java-357. Count Numbers with Unique Digits
/*Given a non-negative integer n, count all numbers with unique digits, x, where 0 ≤ x < 10^n.Example: Given n = 2, return 91. (The answer should be the total numbers in the range of 0 ≤ x < 100,原创 2016-08-21 18:53:37 · 450 阅读 · 0 评论