自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+

leetcode 解题思路

FLAG必经之路

  • 博客(38)
  • 收藏
  • 关注

原创 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-30 10:37:32 285

原创 Paint House II

There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two ad

2016-10-30 05:22:33 249

原创 Paint House

There are a row ofnhouses, each house can be painted with one of the three colors: red, blue or green. The cost of painting each house with a certain color is different. You have to paint all the ho

2016-10-30 02:20:05 826

原创 Add Two Numbers II

You are given two linked lists representing two non-negative numbers. The most significant digit comes first and each of their nodes contain a single digit. Add the two numbers and return it as a link

2016-10-29 09:44:13 360

原创 Path Sum III

You are given a binary tree in which each node contains an integer value.Find the number of paths that sum to a given value.The path does not need to start or end at the root or a leaf, but it ...

2016-10-28 07:04:48 528

原创 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-27 11:19:03 358

原创 Implement HashMap in Java

Implement Hashmap in Java. 原理: Hashmap就是array of linkedlist,就是利用hash算法,算index之后,然后冲突了,就看后面有没有 Key Value pair 相等,如果相同,则覆盖,如果没有,则加在bucket的beginning。 源代码: http://developer.classpath.org/doc/java/util/H

2016-10-24 02:39:59 3985

原创 Water and Jug Problem

You are given two jugs with capacities x and y litres. There is an infinite amount of water supply available. You need to determine whether it is possible to measure exactly z litres using these two

2016-10-22 11:36:58 362

原创 Add Strings

Given two non-negative numbersnum1andnum2represented as string, return the sum ofnum1andnum2.Note:The length of bothnum1andnum2is Bothnum1andnum2contains only digits0-9.B

2016-10-22 09:51:42 273

原创 FizzBuzz

Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”.

2016-10-21 05:34:47 264

原创 Insert Delete GetRandom O(1) - Duplicates allowed

Design a data structure that supports all following operations inaverageO(1)time.Note: Duplicate elements are allowed.insert(val): Inserts an item val to the collection. remove(val): Removes a...

2016-10-20 11:00:18 236

原创 Top K Frequent Elements

Given a non-empty array of integers, return thekmost frequent elements.For example,Given[1,1,1,2,2,3]and k = 2, return[1,2].Note:You may assumekis always valid, 1 ≤k≤ number of uni...

2016-10-20 02:00:52 228

原创 Find K Pairs with Smallest Sums

You are given two integer arraysnums1andnums2sorted in ascending order and an integerk.Define a pair(u,v)which consists of one element from the first array and one element from the second a

2016-10-20 01:02:05 247

原创 Remove Invalid Parentheses

Remove the minimum number of invalid parentheses in order to make the input string valid. Return all possible results.Note: The input string may contain letters other than the parentheses(and)...

2016-10-19 12:43:18 618

原创 Coins in a Line II

There are n coins with different value in a line. Two players take turns to take one or two coins fromleft sideuntil there are no more coins left. The player who take the coins with the most value w...

2016-10-19 11:11:05 540

原创 Reverse Words in a String II

Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are alway

2016-10-18 01:23:50 281

原创 Valid Parentheses

Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct order,"()"and"()[]{}"are all valid...

2016-10-17 02:40:44 201

原创 Best Time to Buy and Sell Stock with Cooldown

Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy on

2016-10-16 12:22:42 262

原创 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]has ...

2016-10-16 11:24:35 230

原创 Best Time to Buy and Sell Stock III

Say you have an array for which theithelement is the price of a given stock on dayi.Design an algorithm to find the maximum profit. You may complete at mosttwotransactions.Note:You may

2016-10-16 09:39:04 168

原创 Best Time to Buy and Sell Stock I

Say you have an array for which theithelement is the price of a given stock on dayi.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock)...

2016-10-16 02:25:06 200

原创 3sum

Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:The solution set must not contain ...

2016-10-15 13:01:28 223

原创 Quick Sort

The basic step of sorting an array are as follows:Select a pivot, normally the middle one From both ends, swap elements and make left elements < pivot and all right > pivot Recursively so...

2016-10-15 09:56:39 681 1

原创 Add, Search, Delete Node in BST.

Add Node, Search Node, Delete Node, 的基本操作,被问了两次了。写出来。http://quiz.geeksforgeeks.org/binary-search-tree-set-1-search-and-insertion/  // add the node; public TreeNode addNode(TreeNode root, int v...

2016-10-14 07:37:33 261

原创 Design Circular Queue

用circular array去implement queue,这个题目被面到两次了。昨天onsite被问到了。今天看了下正确答案,确实不难,是自己复习不到位。算法就是:用front 和rear分别表示头和尾,最重要的考点就是用count来表示里面有多少个元素。这样 front和rear只需要一直向前走就可以了。每次enqueue和dequeue的时候,都用size,也就是count来...

2016-10-14 05:39:58 1512

原创 Reconstruct Itinerary

Given a list of airline tickets represented by pairs of departure and arrival airports[from, to], reconstruct the itinerary in order. All of the tickets belong to a man who departs fromJFK. Thus, the ...

2016-10-10 08:58:54 440

原创 Generalized Abbreviation

Write a function to generate the generalized abbreviations of a word.Example:Given word = "word", return the following list (order does not matter):["word", "1ord", "w1rd", "wo1d", "wor1", "

2016-10-10 06:57:35 254

原创 Longest Increasing Path in a Matrix

Given an integer matrix, find the length of the longest increasing path.From each cell, you can either move to four directions: left, right, up or down. You may NOT move diagonally or move outside o

2016-10-10 05:55:29 389

原创 Palindrome Permutation II

Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empty list if no palindromic permutation could be form.For example:Given s = "aabb", return ["abba",

2016-10-07 05:20:10 477

原创 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 co...

2016-10-06 04:15:39 272

原创 House robber II

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 houses at this place arearranged in a ci...

2016-10-04 11:50:28 249

原创 House Robber III

The thief has found himself a new place for his thievery again. There is only one entrance to this area, called the "root." Besides the root, each house has one and only one parent house. After a tour...

2016-10-04 08:56:55 258

原创 Remove Duplicate Letters

Given a string which contains only lowercase letters, remove duplicate letters so that every letter appear once and only once. You must make sure your result is the smallest in lexicographical order a

2016-10-04 06:42:40 230

原创 Verify Preorder Sequence in Binary Search Tree

Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary search tree.You may assume each number in the sequence is unique.Follow up:Could you do it usi

2016-10-03 13:52:04 262

原创 Closest Binary Search Tree Value II

Given a non-empty binary search tree and a target value, findkvalues in the BST that are closest to the target.ExampleExample 1:Input:{1}0.0000001Output:[1]Explanation:Binary tree {1},...

2016-10-03 06:00:49 454

原创 Power of Two

Given an integer, write a function to determine if it is a power of two.Credits:Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.思路1:计算bit数,记得把符号位计算一下

2016-10-02 09:48:16 180

原创 Missing Number

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm should run...

2016-10-02 06:19:10 199

原创 Find Leaves of Binary Tree

Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.Example:Given binary tree 1 / \ ...

2016-10-01 12:36:10 270

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除