- 博客(85)
- 收藏
- 关注
原创 LeetCode[419] Battleships in a Board
Given an 2D board, count how many different battleships are in it. The battleships are represented with 'X's, empty slots are represented with '.'s. You may assume the following rules:You receiv
2016-10-18 22:57:50
906
原创 LeetCode[394] Decode String
Given an encoded string, return it's decoded string.The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note thatk is
2016-10-18 09:27:36
261
原创 LeetCode[397] Integer Replacement
Given a positive integer n and you can do operations as follow:If n is even, replace n with n/2.If n is odd, you can replace n with either n + 1 or n - 1.What is the minimum number o
2016-10-17 21:56:21
243
原创 LeetCode[398] Random Pick Index
Given an array of integers with possible duplicates, randomly output the index of a given target number. You can assume that the given target number must exist in the array.Note:The array size c
2016-10-17 14:08:36
313
原创 LeetCode[402] Remove K Digits
Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.Note:The length of num is less than 10002 and will b
2016-10-17 10:10:41
316
原创 LeetCode[404] Sum of Left Leaves
Find the sum of all left leaves in a given binary tree.Example: 3 / \ 9 20 / \ 15 7There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24
2016-10-11 16:16:17
232
原创 LeetCode[405] 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-11 15:41:44
308
原创 LeetCode[406] Queue Reconstruction by Height
Suppose you have a random list of people standing in a queue. Each person is described by a pair of integers (h, k), where h is the height of the person and k is the number of people in front of t
2016-10-11 15:00:12
235
原创 LeetCode[409] Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.This is case sensitive, for example "Aa" is not con
2016-10-11 11:08:24
245
原创 LeetCode[415] Add Strings
Given two non-negative numbers num1 and num2 represented as string, return the sum of num1 and num2.Note:The length of both num1 and num2 is Both num1 and num2 contains only digits 0-9.B
2016-10-11 10:38:06
217
原创 LeetCode[200] Number of Islands
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assu
2016-10-11 09:40:44
179
原创 LeetCode[416] Partition Equal Subset Sum
Given a non-empty array containing only positive integers, find if the array can be partitioned into two subsets such that the sum of elements in both subsets is equal.Note:Both the array size a
2016-10-10 12:01:48
808
原创 LeetCode[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 element and return its index.The array may contain multiple peaks, in
2016-10-09 22:51:32
185
原创 LeetCode[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 number
2016-10-09 17:57:08
208
原创 LeetCode[173] Binary Search Tree Iterator
Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST.Calling next() will return the next smallest number in the BST.Note: next()
2016-10-09 12:12:34
154
原创 LeetCode[179] Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.For example, given [3, 30, 34, 5, 9], the largest formed number is 9534330.Note: The result may be ve
2016-10-08 19:43:51
210
原创 LeetCode[187] Repeated DNA Sequences
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACGAATTCCG". When studying DNA, it is sometimes useful to identify repeated sequences within the DNA.Wri
2016-10-08 15:46:50
250
原创 LeetCode[201] Bitwise AND of Numbers Range
Given a range [m, n] where 0 For example, given the range [5, 7], you should return 4.m到n中的数,只要有一个数某位是0,那么相与的结果那一位就是0. 于是问题转化为求m和n的相同前缀class Solution {public: int rangeBitwiseAnd(int m,
2016-10-08 10:04:44
176
原创 LeetCode[204] Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.class Solution {public: int countPrimes(int n) { vector isPrime(n, true); isPrime[0] = isPrime[1] = false
2016-10-06 11:07:26
192
原创 LeetCode[206] Reverse Linked List
Reverse a singly linked list./** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class So
2016-10-04 20:50:24
201
原创 LeetCode[208] Implement Trie (Prefix Tree)
Implement a trie with insert, search, and startsWith methods.Note:You may assume that all inputs are consist of lowercase letters a-z.class TrieNode {public: // Initialize your data struct
2016-10-04 20:35:59
166
原创 LeetCode[209] Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a subarray of which the sum ≥ s. If there isn't one, return 0 instead.For example, given the array [2,3
2016-10-04 19:56:05
260
原创 LeetCode[211] Add and Search Word - Data structure design
Design a data structure that supports the following two operations:void addWord(word)bool search(word)search(word) can search a literal word or a regular expression string containing only lett
2016-10-01 15:36:17
285
原创 LeetCode[213] House Robber II
Note: This is an extension of House Robber.After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time
2016-10-01 13:57:57
176
原创 LeetCode[215] Kth Largest Element in an Array
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.
2016-10-01 10:31:27
390
原创 LeetCode[216] Combination Sum III
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 combination should be a unique set of numbers.Example 1:Inpu
2016-09-28 10:15:22
211
原创 LeetCode[219] Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j]and the difference between i and j is at most k.
2016-09-26 10:21:01
159
原创 LeetCode[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 element
2016-09-26 09:25:31
175
原创 LeetCode[221] Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area.For example, given the following matrix:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0
2016-09-25 23:22:33
157
原创 LeetCode[225] Implement Stack using Queues
Implement the following operations of a stack using queues.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get the top element.empty() -- Return whet
2016-09-25 16:11:38
152
原创 LeetCode[226] Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1/** * Definition for a binary tree node. * struct TreeNode { * i
2016-09-25 14:12:33
145
原创 LeetCode[228] Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].class Solution {public: vector summaryRanges(vect
2016-09-25 13:13:58
168
原创 LeetCode[229] Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorithm should run in linear time and in O(1) space.出现次数超过n/3的最多有两个,于是用两个变量保存即可。用count计数,每当count回
2016-09-25 10:38:24
148
原创 LeetCode[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
2016-09-24 16:59:15
261
原创 LeetCode[231] Power of Two
Given an integer, write a function to determine if it is a power of two.2的幂二进制表示中只有一个1,所以一定满足 n & n - 1 == 0class Solution {public: bool isPowerOfTwo(int n) { if (n <= 0) return false;
2016-09-24 15:50:40
172
原创 LeetCode[232] Implement Queue using Stacks
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(
2016-09-24 15:36:38
164
原创 LeetCode[234] Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?将链表后半部分反转,头尾个分配一个指针,依次向中间移动,若发现不同的值就不是回文/** * Definition for singly-linked
2016-09-19 18:34:15
148
原创 LeetCode[235] 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 lowest common ancestor is defined betw
2016-09-19 16:44:04
127
原创 LeetCode[236] Lowest Common Ancestor of a Binary Tree
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two node
2016-09-18 21:08:14
123
原创 LeetCode[237] Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value
2016-09-18 20:37:27
146
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人