
LeetCode算法--Java
蜗牛要好好学习呀
虽然蜗牛爬的慢,但是有赶超兔子的决心。
展开
-
LeetCode172. Factorial Trailing Zeroes
172.Factorial Trailing Zeroes题意:Given an integer n, return the number of trailing zeroes in n!. 给定一个正整数n,返回 n!的末尾的 0 的个数。 Note: Your solution should be in logarithmic time complexity.我的想法:就只想到了用暴力求解,原创 2017-12-16 14:35:55 · 300 阅读 · 0 评论 -
LeetCode121. Best Time to Buy and Sell Stock
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 (ie, buy one a原创 2017-11-05 15:05:10 · 344 阅读 · 0 评论 -
**LeetCode137. Single Number II_M
137.Single Number II_M Given an array of integers, every element appears three times except for one, which appears exactly once. Find that single one.Note: Your algorithm should have a linear runtime原创 2017-11-13 21:12:04 · 335 阅读 · 0 评论 -
LeetCode104. Maximum Depth of Binary Tree
104.Maximum Depth of Binary Tree 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.public cl原创 2017-10-24 22:00:42 · 196 阅读 · 0 评论 -
LeetCode119. Pascal's Triangle II
119.Pascal’s Triangle II Given an index k, return the kth row of the Pascal’s triangle.For example, given k = 3, Return [1,3,3,1].Note: Could you optimize your algorithm to use only O(k) extra space原创 2017-11-01 16:39:56 · 291 阅读 · 0 评论 -
LeetCode2. Add Two Numbers
2.Add Two Numbers You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two nu原创 2017-11-09 21:42:52 · 237 阅读 · 0 评论 -
LeetCode118. Pascal's Triangle
118.Pascal’s Triangle 帕斯卡尔三角形也就是杨辉三角形 发现规律: * 每一行中数的个数与行号相等; * 每一行中除了第一个和最后一个元素,其他元素是上一行中相邻两个元素之和; 由这两个规律可以得到杨辉三角形。 方法一: 这里 i 代表层数,从 1 开始,List中先插入非 1 元素,最后插入首尾两个1。public List<List<Integer>> gen原创 2017-10-31 11:45:19 · 266 阅读 · 0 评论 -
LeetCode142. Linked List Cycle II
142.Linked List Cycle IIGiven a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up: Can you solve it without using ext原创 2017-11-08 17:27:25 · 291 阅读 · 0 评论 -
LeetCode141. Linked List Cycle
141.Linked List Cycle Given a linked list, determine if it has a cycle in it.Follow up: Can you solve it without using extra space? class ListNode { int val; ListNode next; ListNode(int原创 2017-11-08 16:18:27 · 238 阅读 · 0 评论 -
LeetCode112. Path Sum
112.Path Sum 使用递归的方法,要注意题意只要有一个叶子节点符合要求就返回 true,所以return 语句回一个或语句.public boolean hasPathSum(TreeNode root, int sum) { if(root == null) return false; if(root.left == null && root.原创 2017-10-30 22:01:11 · 244 阅读 · 0 评论 -
LeetCode122. Best Time to Buy and Sell Stock II
122.Best Time to Buy and Sell Stock II Say you have an array for which the ith element is the price of a given stock on day i.Design an algorithm to find the maximum profit. You may complete as many t原创 2017-11-05 17:13:01 · 605 阅读 · 0 评论 -
LeetCode125. Valid Palindrome
125.Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example, “A man, a plan, a canal: Panama” is a palindrome. “race原创 2017-11-05 20:05:55 · 342 阅读 · 0 评论 -
LeetCode169. Majority Element
169.Majority Element题意:Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the m原创 2017-12-16 14:34:53 · 310 阅读 · 0 评论 -
LeetCode109. Convert Sorted List to Binary Search Tree
109.Convert Sorted List to Binary Search TreeGiven a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 给定一个单元链表,元素按照升序排列,将其转换为高度平衡的BST。 平衡二叉查找树的特点是原创 2017-11-27 17:28:07 · 308 阅读 · 0 评论 -
LeetCode530. Minimum Absolute Difference in BST
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. BST 的最小绝对差值。Example:Input: 1 \原创 2017-11-27 15:41:55 · 359 阅读 · 0 评论 -
LeetCode167. Two Sum II - Input array is sorted
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. 给一个已经按照升序排列的整数数组,找到两个原创 2017-12-12 22:05:43 · 274 阅读 · 0 评论 -
LeetCode160. Intersection of Two Linked Lists
160.Intersection of Two Linked Lists题意:Write a program to find the node at which the intersection of two singly linked lists begins. 找出两条链表相交的第一个节点。 For example, the following two linked lists:A:原创 2017-12-12 17:15:58 · 260 阅读 · 0 评论 -
LeetCode671. Second Minimum Node In a Binary Tree
671.Second Minimum Node In a Binary Tree题意:Given a non-empty special binary tree consisting of nodes with the non-negative value, where each node in this tree has exactly two or zero sub-node. If the n原创 2017-12-11 10:34:47 · 407 阅读 · 0 评论 -
LeetCode687. Longest Univalue Path
687.Longest Univalue Path题意:Given a binary tree, find the length of the longest path where each node in the path has the same value. This path may or may not pass through the root. 给定一棵二叉树,寻找符合条件的最长路原创 2017-12-10 18:31:06 · 330 阅读 · 0 评论 -
LeetCode113. Path Sum II
113.Path Sum II 题意Given a binary tree and a sum, find all root-to-leaf paths where each path’s sum equals the given sum. 给定一个二叉树和一个数字,找到所有从根结点到叶节点的路径,满足和和给定数字相等。 For example: Given the below binary原创 2017-12-10 15:32:28 · 278 阅读 · 0 评论 -
LeetCode155. Min Stack
155.Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. 本题设计一个栈,包括4个功能,入栈push(),出栈pop(),返回栈顶元素top(),返回栈的最小值getMin();push(x) -- Push element x原创 2017-11-29 10:08:46 · 252 阅读 · 0 评论 -
LeetCode111. Minimum Depth of Binary Tree
111.Minimum Depth of Binary Tree Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. public原创 2017-10-30 21:01:45 · 265 阅读 · 0 评论 -
LeetCode110. Balanced Binary Tree
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 e原创 2017-10-30 20:28:00 · 412 阅读 · 0 评论 -
LeetCode 38. Count and Say
38.Count and Say The count-and-say sequence is the sequence of integers with the first five terms as following: 1. 1 2. 11 3. 21 4. 1211 5. 1112211 is read off as “one 1” or 1原创 2017-09-14 22:10:41 · 250 阅读 · 0 评论 -
LeetCode 28. Implement strStr()
28.Implement strStr()Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.题意:判断一个字符串是否为另一个字符串的子串,如果是,则返回第一个字符串出现的索引位置,如果不是,则返回 -1。第一种方法://该方法直接利用字符串的原创 2017-09-14 15:34:35 · 251 阅读 · 0 评论 -
LeetCode35. Search Insert Position
35.Search Insert PositionGiven a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no dupl原创 2017-09-14 15:31:33 · 224 阅读 · 0 评论 -
LeetCode27. Remove Element
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 cons原创 2017-09-12 23:09:04 · 340 阅读 · 0 评论 -
LeetCode69. Sqrt(x)
69.Sqrt(x) 题意: 实现 int sqrt(int x), 返回根号下x。 Implement int sqrt(int x). Compute and return the square root of x.我的思路: 用折半查找的方法。public int mySqrt(int x) { int first = 1; int last = x; int原创 2017-09-24 22:01:26 · 244 阅读 · 0 评论 -
LeetCode 67. Add Binary
67.Add Binary 题意: Given two binary strings, return their sum (also a binary string). For example, a = “11” b = “1” Return “100”. 我的思路: 这道题要理解二进制加法的机制。public String addBinary(String a, String b) {原创 2017-09-23 22:29:06 · 253 阅读 · 0 评论 -
LeetCode 66. Plus One
Plus One 题意: 用一个非空数组来代替一个非负数,给这个整数+1.假设这个非空数组第一位不为0,除非0本身。 原题意: Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not co原创 2017-09-23 15:36:50 · 204 阅读 · 0 评论 -
**LeetCode 70. Climbing Stairs
70.Climbing Stairs 题意: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Giv原创 2017-09-28 15:51:48 · 335 阅读 · 0 评论 -
LeetCode83. Remove Duplicates from Sorted List
83.Remove Duplicates from Sorted List题意: 给一个已排序的链表,删除重复的元素,使得所有元素只出现一次。 Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return原创 2017-10-01 15:58:11 · 308 阅读 · 0 评论 -
LeetCode101. Symmetric Tree
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:原创 2017-10-22 13:44:28 · 291 阅读 · 0 评论 -
LeetCode108. Convert Sorted Array to Binary Search Tree
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. BST:Binary sorted tree(二叉排序树).即按照中序遍历的方式遍历这棵树,结果值按照升序排列原创 2017-10-29 21:51:33 · 498 阅读 · 0 评论 -
LeetCode136. SingleNumber
136.SingleNumberGiven an array of integers, every element appears twice except for one. Find that single one.Note: Your algorithm should have a linear runtime complexity. Could you implement it with原创 2017-11-06 22:34:27 · 233 阅读 · 0 评论 -
LeetCode100. Same Tree
100.Same Tree Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 给定两个原创 2017-10-19 21:23:40 · 355 阅读 · 0 评论 -
LeetCode107. Binary Tree Level Order Traversal II
107.Binary Tree Level Order Traversal II public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; }}方法一: 受上一题104.MaximumDepthofBinaryTree的启发,使用队列来做,原创 2017-10-27 16:00:25 · 243 阅读 · 0 评论 -
LeetCode 88. Merge Sorted Array
88.Merge Sorted Array Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note: You may assume that nums1 has enough space (size that is greater or equal to m原创 2017-10-19 11:17:33 · 239 阅读 · 0 评论 -
LeetCode58. Length of Last Word
58.Length of Last Word 题意: 给一个字符串,这个字符串包括大小写字母和空格,返回这个字符串的最后一个单词的长度。如果最后一个单词不存在,则返回0. 一个单词是指没有空格的字母串。 For example, Given s = “Hello World”, return 5. 我的思路: 这个题很简单,如果输入字符串为空,则返回0; 如果不为空,从最后一位向前数原创 2017-09-22 17:11:16 · 265 阅读 · 0 评论 -
LeetCode53. Maximum Subarray
53.Maximum Subarray 题意:在一个给定数组中找一个和最大的子数组(至少含有一个元素)。 For example, given the array [-2,1,-3,4,-1,2,1,-5,4], the contiguous subarray [4,-1,2,1] has the largest sum = 6. 我的思路: 1、如果加上当前位的和<当前位的值,则舍弃前面原创 2017-09-22 16:19:49 · 317 阅读 · 0 评论