
leetcode
文章平均质量分 62
zhuguorong11
这个作者很懒,什么都没留下…
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
leetcode中两道关于中位数的题295和480
两道题的思路是一样的,都是建立2个堆,一个是最小堆,一个是最大堆295题/* * Design a data structure that supports the following two operations:void addNum(int num) - Add a integer number from the data stream to the data structur原创 2017-06-12 23:03:15 · 1053 阅读 · 0 评论 -
Combination Sum III
import java.util.ArrayList;import java.util.List;/* * Find all possible combinations of k numbers that add up to a number n, * given that only numbers from 1 to 9 can be used and each combinatio转载 2016-09-04 16:49:38 · 236 阅读 · 0 评论 -
Lowest Common Ancestor of a Binary Search Tree
给定两个节点,找出他们的最小公共节点,给定的是一个二叉排序树,难度简单。/* * Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The原创 2016-08-13 12:40:43 · 181 阅读 · 0 评论 -
Reverse Integer
要注意int的溢出/* * Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321 * */int res = 0; while(x!=0) { int weibu = x%10; int newres = res*10+wei原创 2016-08-12 23:38:21 · 198 阅读 · 0 评论 -
Arrays.binarySearch以及Longest Increasing Subsequence
Arrays.binarySearch();的用法。public static int binarySearch(Object[] a,Object key) 使用二分搜索法来搜索指定数组,以获得指定对象。在进行此调用之前, 必须根据元素的自然顺序对数组进行升序排序(通过 sort(Object[]) 方 法)。如果没有对数组进行排序,则结果是不确定的。(如果数组包含原创 2016-09-15 18:47:37 · 265 阅读 · 0 评论 -
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转载 2016-09-14 21:51:57 · 211 阅读 · 0 评论 -
Maximum Product of Word Lengths
Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume that each word will contain only lower case le转载 2016-09-03 12:35:23 · 291 阅读 · 0 评论 -
Ransom Note
/* * Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be c原创 2016-08-11 22:12:07 · 389 阅读 · 0 评论 -
Counting Bits
/* * Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.Example:For num =原创 2016-08-23 19:14:07 · 269 阅读 · 0 评论 -
Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.Example:Given nums = [-2, 0, 3, -5, 2, -1]sumRange(0, 2) -> 1sumRange(2, 5) -> -1sumRan原创 2016-08-10 23:33:33 · 266 阅读 · 0 评论 -
整数操作中判断是否会超出其范围
在很多题中会遇到例如字符串转换整型,整型反转,这就涉及到Integer类型的范围,INT_MAX (2147483647) or INT_MIN (-2147483648),在逐位进行计算中,要对其结果进行判断,例如以下leetocde第八题 自己写一个atoi函数,字符串转整型 if(( Integer.MAX_VALUE/10 这个判断就是关键所在,在每一次sum*10+digit之原创 2016-08-22 23:57:49 · 8707 阅读 · 0 评论 -
Compare Version Numbers
public class Solution { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(Solution.compareVersion("1.1", "1.0.45")); } public static int compareVer原创 2016-08-21 23:54:22 · 289 阅读 · 0 评论 -
Valid Palindrome
import java.util.LinkedList;import java.util.Queue;import java.util.Stack;/* * Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For e原创 2016-08-10 13:25:53 · 251 阅读 · 0 评论 -
Palindrome Linked List
/* * Given a singly linked list, determine if it is a palindrome. * * * This can be solved by reversing the 2nd half and compare the two halves. Let's start with an example [1, 1, 2, 1].In the转载 2016-08-10 13:24:02 · 187 阅读 · 0 评论 -
Pascal's Triangle
import java.util.LinkedList;import java.util.List;/* * Given numRows, generate the first numRows of Pascal's triangle. * [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]] * */publi原创 2016-08-10 13:23:24 · 312 阅读 · 0 评论 -
Minimum Depth of Binary Tree
/* * The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. * */public class Solution { public static void main(String[] args) { // T原创 2016-08-10 13:21:43 · 251 阅读 · 0 评论 -
Search for a Range
The problem can be simply broken down as two binary searches for the begining and end of the range, respectively:First let's find the left boundary of the range. We initialize the range to [i=0, j转载 2016-09-16 19:11:27 · 260 阅读 · 0 评论 -
Binary Tree Level Order Traversal II
import java.util.LinkedList;import java.util.List;import java.util.Queue;/* * Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level原创 2016-08-14 00:04:46 · 368 阅读 · 0 评论 -
Product of Array Except Self
Given an array of n integers where n > 1, nums, return an array output such that output[i] is equal to the product of all the elements of nums except nums[i].Solve it without division and in O转载 2016-08-25 23:58:16 · 260 阅读 · 0 评论 -
两个有序数组的中位数
/* * 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)).Example 1:nums1 = [原创 2017-06-10 18:30:26 · 324 阅读 · 0 评论 -
Search in Rotated Sorted Array
/*Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return it转载 2016-11-09 23:22:47 · 252 阅读 · 0 评论 -
Reconstruct Original Digits from English
/* * * Given a non-empty string containing an out-of-order English representation of digits 0-9, * output the digits in ascending order. * Example 1:Input: "owoztneoer"Output: "012"Example 2转载 2016-10-19 08:45:58 · 651 阅读 · 0 评论 -
Integer Replacement
What is not so obvious is what to do with odd numbers. One may think that you just need to remove as many 1's as possible to increase the evenness of the number. Wrong! Look at this example:111011 -转载 2016-10-04 21:41:50 · 319 阅读 · 0 评论 -
判断字符串能否组成前序遍历的二叉树
/* * One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #.转载 2016-11-08 14:16:31 · 822 阅读 · 0 评论 -
Convert a Number to Hexadecimal
Given an integer, write an algorithm to convert it to hexadecimal. For negative integer, two’s complement method is used.Note:All letters in hexadecimal (a-f) must be in lowercase.The hexade转载 2016-10-03 20:51:30 · 264 阅读 · 0 评论 -
Binary Watch
bitCount方法——获取二进制补码中1位的数量bitCount方法返回指定int值的二进制补码表示形式的1位的数量。原创 2016-10-03 19:28:37 · 603 阅读 · 0 评论 -
中序遍历,递归+迭代
中序遍历,使用递归和迭代两种方法import java.util.ArrayList;import java.util.List;import java.util.Stack;public class Solution { public static void main(String[] args) { // TODO 自动生成的方法存根 } //中序遍历 publ原创 2016-11-05 20:36:44 · 548 阅读 · 0 评论 -
Unique Binary Search Trees
The problem can be solved in a dynamic programming way. I’ll explain the intuition and formulas in the following.Given a sequence 1…n, to construct a Binary Search Tree (BST) out of the sequence,转载 2016-10-13 22:57:15 · 307 阅读 · 0 评论 -
Maximum Subarray
/* * Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1]转载 2016-09-08 18:49:40 · 264 阅读 · 0 评论 -
Permutations
import java.util.ArrayList;import java.util.List;/* * Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[ [1,2,3],转载 2016-09-07 19:36:25 · 294 阅读 · 0 评论 -
Lexicographical Numbers
import java.util.ArrayList;import java.util.List;/* * Given an integer n, return 1 - n in lexicographical order.For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9].Please optimize y转载 2016-09-20 15:00:32 · 321 阅读 · 0 评论 -
Gray Code
import java.util.ArrayList;import java.util.List;/* * The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the转载 2016-09-06 18:13:19 · 258 阅读 · 0 评论 -
Top K Frequent Elements
Given a non-empty array of integers, return the k most frequent elements.For example,Given [1,1,1,2,2,3] and k = 2, return [1,2].Note: You may assume k is always valid, 1 ≤ k ≤ number转载 2016-08-26 16:07:31 · 450 阅读 · 0 评论 -
Path Sum
/* * Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary tree and原创 2016-08-14 14:02:15 · 275 阅读 · 0 评论 -
Implement strStr()
/* * Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. * */public class Solution { public static void main(String[] ar原创 2016-08-10 13:19:43 · 183 阅读 · 0 评论 -
Bulb Switcher
There are n bulbs that are initially off. You first turn on all the bulbs. Then, you turn off every second bulb. On the third round, you toggle every third bulb (turning on if it's off or turning off原创 2016-08-31 18:44:06 · 260 阅读 · 0 评论 -
Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1第一种可以使用递归:public class Solution { public static void main(String[] ar原创 2016-08-09 23:32:55 · 322 阅读 · 0 评论 -
Ugly Num
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 includes another prime factor 7. public static boolean isU原创 2016-08-02 16:34:12 · 210 阅读 · 0 评论 -
位运算实现整数相加
其主要用了两个基本表达式:x^y //执行加法,不考虑进位。(x&y)//进位操作令x=x^y ;y=(x&y)1 进行迭代,每迭代一次进位操作右面就多一位0,最多需要“加数二进制位长度”次迭代就没有进位了,此时x^y的值就是结果。 public static int getSum(int a, int b) { if(b==0)原创 2016-08-02 15:36:40 · 439 阅读 · 0 评论 -
Nim Game
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2016-08-02 14:19:42 · 198 阅读 · 0 评论