- 博客(270)
- 收藏
- 关注
原创 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 nodes v and
2017-02-11 15:19:06 153
原创 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?s思路: 1. 套路题。先快慢指针找到中点,然后把后半部分reverse,然后两边都从头开始遍历,看是否相等。class Solution {public:
2017-02-11 13:24:11 131
原创 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()
2017-02-11 12:53:42 165
原创 231. Power of Two
Given an integer, write a function to determine if it is a power of two.s思路: 1. 是否指数,一看就是binary search的问题。 2. 还不一定是,自己想多了,就是bit manipulation.看是否n的二进制表达只有一个1即可!class Solution {public: bool isPowe
2017-02-11 12:29:37 123
原创 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 BST is mod
2017-02-11 12:21:38 164
原创 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.s思路: 1. 之前遇到是出现次数大于n/2,这次是出现次数n/3,出现次数越少,要找到就越难,相当于信号
2017-02-11 10:14:38 278
原创 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”].s思路; 1. 看起来挺简单的,one pass. 比较当前数和前一个数,如果连续,则从vector< strin
2017-02-11 09:21:20 228
原创 227. Basic Calculator II
Implement a basic calculator to evaluate a simple expression string.The expression string contains only non-negative integers, +, -, *, / operators and empty spaces . The integer division should trunca
2017-02-11 09:01:16 161
原创 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1Trivia: This problem was inspired by this original tweet by Max Howell: Google
2017-02-11 05:07:19 146
原创 223. Rectangle Area
Find the total area covered by two rectilinear rectangles in a 2D plane.Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Rectangle Area Assume that t
2017-02-11 04:31:25 155
原创 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 whether
2017-02-11 03:15:10 154
原创 (未完成!)Leetcode 222. Count Complete Tree Nodes
Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and
2017-02-10 15:16:12 263
原创 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 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Ret
2017-02-10 14:48:13 242
原创 Leetcode 220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indices i and j in the array such that the absolute difference between nums[i] and nums[j] is at most t and the absolute difference b
2017-02-10 14:08:46 1861
原创 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 absolute difference between i and j is at most k.s思
2017-02-10 10:38:32 149
原创 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 is
2017-02-10 10:00:17 177
原创 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:Input: k = 3, n =
2017-02-10 09:47:12 119
原创 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.Note:
2017-02-10 09:18:51 254
原创 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, all
2017-02-10 08:10:47 157
原创 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 letters a
2017-02-10 05:33:04 246
原创 Leetcode 210. Course Schedule II
There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
2017-02-10 04:37:34 336
原创 Leetcode 209. Minimum Size Subarray Sum
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn’t one, return 0 instead.For example, given the array
2017-02-09 12:37:17 324
原创 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.s思路: 1. trie,前缀树。用来搜索string很方便快速。每个节点包括26个指针数组,对应26个字母,如果child[0
2017-02-09 12:06:52 138
原创 Leetcode 207. Course Schedule
There are a total of n courses you have to take, labeled from 0 to n - 1.Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair:
2017-02-09 09:43:51 316
原创 Leetcode 206. Reverse Linked List
Reverse a singly linked list. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both?s思路: 1. 要求iterative和recursive. 2. 先来iterative。iterative写过几次了,简单的就是用dumm
2017-02-09 08:40:48 206
原创 Leetcode 205. Isomorphic Strings
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with another chara
2017-02-09 07:58:51 376
原创 Leetcode 204. Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.s思路: 1. 先看看这道题的边界是什么。肯定需要检查每个数i是否质数,而检测是否质数则需要检测小于i的数和i能否整除。这样,复杂度就是o(n*n). 2. 这么做如何?只能说这个边界太粗了,没有压缩得很仔细,还有空间可以压缩,或者说有
2017-02-09 05:58:52 148
原创 Leetcode 203. Remove Linked List Elements
Remove all elements from a linked list of integers that have value val.Example Given: 1 –> 2 –> 6 –> 3 –> 4 –> 5 –> 6, val = 6 Return: 1 –> 2 –> 3 –> 4 –> 5s思路: 1. 操作链表,要删除数。首先想想链表的边界是什么?左边是头节点,是边界,
2017-02-09 05:13:38 191
原创 Leetcode 202. 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 of i
2017-02-09 04:06:00 226
原创 Leetcode 201. Bitwise AND of Numbers Range
Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive.For example, given the range [5, 7], you should return 4.s思路: 1. 简单粗暴的做法,就是把所有数都遍历一
2017-02-09 03:51:59 161
原创 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 assume
2017-02-08 14:52:45 169
原创 Leetcode 199. Binary Tree Right Side View
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.For example: Given the following binary tree, 1
2017-02-08 14:21:02 175
原创 Leetcode 198. House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses
2017-02-08 13:58:33 134
原创 Leetcode 191. 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
2017-02-08 12:54:35 153
原创 Leetcode 190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110
2017-02-08 12:50:50 126
原创 Leetcode 189. Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note: Try to come up as many solutions as you can, ther
2017-02-08 12:09:52 128
原创 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.Write a
2017-02-08 10:00:11 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 very large,
2017-02-08 08:41:33 575
原创 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() and hasN
2017-02-08 04:31:54 150
原创 Leetcode 172. Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity.s思路: 1. n!中零的个数是因为有2,5这两个因子。我们就去找有多少个5,多少个2,然后这两个数的最小者决定了0的个数。严格的说,必须都计算,但是从
2017-02-08 03:50:20 135
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人