
LeetCode
聚水弥香
这个作者很懒,什么都没留下…
展开
-
513:find bottom left tree value
思考:本题比较简单,是运用广度优先算法,通过队列存储同一深度元素。 public int findBottleLeftValue(TreeNode root){ Queue q = new LinkedList(); q.offer(root); int result = 0; while (!q.isEmpty()) { int size = q.size();原创 2017-02-16 16:48:46 · 302 阅读 · 0 评论 -
204:Count Primers
target:Count the number of prime numbers less than a non-negative number n我第一次编写的解决代码,时间复杂度是O(n2),在40多万时就挂了。 //O(n2),cost too much time public int simplecountPrimes(int n) { int num = 0; for原创 2017-02-16 17:09:45 · 187 阅读 · 0 评论 -
8:String to Integer
思考:本题的难点体现在对各种情况能否全部覆盖,已经对细节的把握上。可能出现的各种特殊情况:1.null or empty input 2.+ & - 3.white space 4.max and min of integerpublic class atoi { public int luoAtoi(String str){ if(str==n原创 2017-02-16 16:07:19 · 398 阅读 · 0 评论 -
263.Ugly Number
@target 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原创 2017-02-17 17:26:22 · 192 阅读 · 0 评论 -
26.Ugly Number2
Write a program to find the n-th ugly number.思路:一开始,我考虑的是遍历查找,逐个判断是否为丑数,果断耗时太长。//TLE,1600th cost 17.148s public static int nthUglyNumber(int n){ if(n <= 0){ return 0; } int count = 0原创 2017-02-17 17:50:55 · 271 阅读 · 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)). 提到log(m),自然会想到二分查找。 本题相比一个原创 2017-02-24 14:52:00 · 233 阅读 · 0 评论