- 博客(137)
- 收藏
- 关注
原创 Remove Duplicates from Sorted Array
public class Solution { public int removeDuplicates(int[] A) { if (A == null || A.length == 0) { return 0; } int size = 0; for (int i = 0; i < A.length; i
2016-04-13 13:56:39
256
原创 双指针---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 constant memory.The order
2016-04-13 13:45:41
221
原创 Best Time to Buy and Sell Stock
public class Solution { public int maxProfit(int[] prices) { if(prices.length == 0) return 0; int low = prices[0]; int ans = 0; for(int i=1; i<prices.length; i++){ if(prices[i] <
2016-04-13 13:22:58
259
原创 Power of Two
public class Solution { public boolean isPowerOfTwo(int n) { return n>0&&(n&(n-1))==0; }}
2016-04-12 19:11:01
202
原创 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?public class Solution { public in
2016-04-12 18:43:06
233
原创 位操作---Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11’ has binary representation 0000000000000
2016-04-12 18:04:50
224
原创 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.public class Solution { public int majorityElement(int[] num) { if
2016-04-12 13:39:29
184
原创 HashSet---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 element is
2016-04-12 13:23:42
220
原创 26进制---Excel Sheet Column Number
For example:A -> 1B -> 2C -> 3...Z -> 26AA -> 27AB -> 28 public class Solution { public int titleToNumber(String s) { int length = s.length(); int num = 0; for(int i
2016-04-11 14:35:12
264
原创 String ---Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example, s = “anagram”, t = “nagaram”, return true. s = “rat”, t = “car”, return false.public class Solution {
2016-04-11 12:21:22
206
原创 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.For example, given nums = [0, 1, 0, 3, 12], after calling your funct
2016-04-10 15:31:30
214
原创 Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, r
2016-04-10 14:20:33
207
原创 DP---Best Time to Buy and Sell Stock
Given an example [3,2,3,1,2], return 1public class Solution { public int maxProfit(int[] prices) { if (prices == null || prices.length == 0) { return 0; } int min
2016-04-10 11:12:33
186
原创 JAVA---Unique Characters
Implement an algorithm to determine if a string has all unique characters. Given “abc”, return true.Given “aab”, return false.public class Solution { /** * @param str: a string * @return
2016-04-08 16:55:15
279
原创 DFS---Number of Islands
Given a boolean 2D matrix, find the number of islands.Given graph:[ [1, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 0, 0, 1, 1], [0, 0, 0, 0, 0], [0, 0, 0, 0, 1] ] return 3.public class Solution
2016-04-08 16:06:36
257
原创 DP--- Minimum Subarray
Given an array of integers, find the subarray with smallest sum.Return the sum of the subarray.For [1, -1, -2, 1], return -3public class Solution { public int minSubArray(ArrayList<Integer> nums){
2016-04-08 14:43:53
182
原创 JAVA---Merge Intervals
Given a collection of intervals, merge all overlapping intervals.Given intervals => merged intervals:[ [ [1, 3], [1, 6], [2, 6], => [8, 10], [8,
2016-04-08 14:03:00
528
原创 DP---Maximum Subarray
Given an array of integers, find a contiguous subarray which has the largest sum.Given the array [−2,2,−3,4,−1,2,1,−5,3], the contiguous subarray [4,−1,2,1] has the largest sum = 6.public class Solutio
2016-04-08 13:20:16
299
原创 Longest Words
Given a dictionary, find all of the longest words in the dictionary.Given{ “dog”, “google”, “facebook”, “internationalization”, “blabla” } the longest words are(is) [“internationaliza
2016-04-06 15:56:55
342
原创 Insert Interval
Given a non-overlapping interval list which is sorted by start point.Insert a new interval into it, make sure the list is still in order and non-overlapping (merge intervals if necessary). Insert [2,
2016-04-06 15:53:09
280
原创 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
2016-04-06 15:50:10
260
原创 Math---Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial.11! = 39916800, so the out should be 2class Solution { /* * param n: As desciption * return: An integer, deno
2016-04-06 00:07:22
200
原创 位操作--- Flip Bits
Determine the number of bits required to flip if you want to convert integer n to integer m. Given n = 31 (11111), m = 14 (01110), return 2.class Solution { /** *@param a, b: Two integer
2016-04-03 22:10:24
511
原创 递归---Flatten Binary Tree to Linked List
Flatten a binary tree to a fake “linked list” in pre-order traversal.Here we use the right pointer in TreeNode as the next pointer in ListNode.public class Solution { private TreeNode lastNode = nu
2016-04-03 21:51:29
170
原创 Binary Tree---Subtree
You have two every large binary trees: T1, with millions of nodes, and T2, with hundreds of nodes. Create an algorithm to decide if T2 is a subtree of T1.public class Solution{ public boolean isSubtr
2016-04-03 21:04:47
201
原创 Binary Search---Wood Cut
Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get
2016-04-03 18:36:58
273
原创 Binary Search---First Position of Target
给定一个排序的整数数组(升序)和一个要查找的整数target,用O(logn)的时间查找到target第一次出现的下标(从0开始),如果target不存在于数组中,返回-1。在数组 [1, 2, 3, 3, 4, 5, 10] 中二分查找3,返回2。class Solution { /** * @param nums: The integer array. * @para
2016-04-03 18:05:01
300
原创 LinkedList---Delete Node in the Middle of Singly Linked List
Implement an algorithm to delete a node in the middle of a singly linked list, given only access to that node.Have you met this question in a real interview? Yes Example Tags Related Problems Note
2016-03-02 00:08:26
408
原创 Partition Array by Odd and Even
Partition Array by Odd and Even15:00 Start Partition an integers array into odd number first and even number second.Have you met this question in a real interview? Yes Example Challenge Tags Rel
2016-03-01 23:21:53
266
原创 binary search---Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.Have you met this question in a real interview? Yes Example sqrt(3) = 1sqrt(4) = 2sqrt(5) = 2sqrt(10) = 3Challenge O(log(x))Tags Exp
2016-02-22 15:23:26
212
原创 HashMap---Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the target, where in
2016-02-22 14:42:54
258
原创 First Missing Positive
Given an unsorted integer array, find the first missing positive integer.Have you met this question in a real interview? Yes Example Given [1,2,0] return 3, and [3,4,-1,1] return 2.Challenge Your a
2016-02-22 13:55:45
173
原创 Product of Array Exclude Itself
Given an integers array A.Define B[i] = A[0] * … * A[i-1] * A[i+1] * … * A[n-1], calculate B WITHOUT divide operation.Have you met this question in a real interview? Yes Example For A = [1, 2, 3], re
2016-02-21 21:29:16
185
原创 字符串---Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11.11 is read off as “two 1s” or 21.21 is read off as “one 2, then one
2016-01-31 02:38:59
191
原创 Binary Tree Paths
Given a binary tree, return all root-to-leaf paths.Given the following binary tree:1 / \ 2 3 \ 5 All root-to-leaf paths are:[ “1->2->5”, “1->3” ]/** * Definition of TreeNode: *
2016-01-31 02:32:19
155
原创 Add Two Numbers
You have two numbers represented by a linked list, where each node contains a single digit. The digits are stored in reverse order, such that the 1’s digit is at the head of the list. Write a function
2016-01-31 02:21:16
156
原创 字符串---Add Binary
Given two binary strings, return their sum (also a binary string).public class Solution { /** * @param a a number * @param b a number * @return the result */ public String a
2016-01-31 01:29:42
198
原创 dummy-----Remove Linked List Elements
Remove all elements from a linked list of integers that have value val./** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x)
2016-01-31 01:01:08
225
原创 Longest Common Prefix
Given k strings, find the longest common prefix (LCP).Have you met this question in a real interview? Yes Example For strings “ABCD”, “ABEF” and “ACEF”, the LCP is “A”For strings “ABCDEFG”, “ABCEFG”
2016-01-30 23:36:38
181
原创 暴力匹配---Longest Common Substring
Given two strings, find the longest common substring.Return the length of it.Example Given A = “ABCD”, B = “CBCE”, return 2.Note The characters in substring should occur continuously in original stri
2016-01-30 23:23:45
266
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人