
LeetCode
文章平均质量分 72
Yadoer
这个作者很懒,什么都没留下…
展开
-
LeetCode-N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2015-08-22 17:29:00 · 1106 阅读 · 0 评论 -
LeetCode-Flatten Binary Tree to Linked List
Given a binary tree, flatten it to a linked list in-place.For example,Given 1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like: 1原创 2015-08-15 16:11:26 · 1387 阅读 · 0 评论 -
LeetCode-Maximum/Minimum Depth of Binary Tree
iven a binary tree, find its maximum/minimum depth.The maximum/minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.两个很相似的题,但是解法完全不怎么原创 2015-08-14 15:39:02 · 850 阅读 · 0 评论 -
LeetCode-Interleaving String
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2.For example,Given:s1 = "aabcc",s2 = "dbbca",When s3 = "aadbbcbcac", return true.When s3 = "aadbbbaccc", ret原创 2015-08-28 17:48:18 · 1058 阅读 · 0 评论 -
LeetCode-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.此题是Majority Element的继续,关于此题可以移步至Majority Elem原创 2015-08-06 11:03:29 · 1259 阅读 · 0 评论 -
LeetCode-Distinct Subsequences
Given a string S and a string T, count the number of distinct subsequences of T in S.A subsequence of a string is a new string which is formed from the original string by deleting some (can be non原创 2015-08-28 15:21:03 · 947 阅读 · 0 评论 -
LeetCode-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原创 2015-08-13 20:57:10 · 721 阅读 · 0 评论 -
LeetCode - Edit Distance
Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a word:原创 2015-08-27 20:02:41 · 845 阅读 · 0 评论 -
LeetCode-Triangle
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.For example, given the following triangle[ [2], [3,4], [原创 2015-08-27 14:34:47 · 906 阅读 · 0 评论 -
LeetCode-Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) -- Push element x onto stack.pop() -- Removes the element on top of the stack.top() -- Get原创 2015-08-13 15:29:44 · 891 阅读 · 0 评论 -
LeetCode-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.empt原创 2015-08-13 20:08:16 · 1065 阅读 · 0 评论 -
LeetCode-Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 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原创 2015-08-27 17:45:34 · 873 阅读 · 0 评论 -
LeetCode-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原创 2015-07-31 10:18:18 · 907 阅读 · 0 评论 -
LeetCode-Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total nu原创 2015-08-25 19:57:08 · 1066 阅读 · 0 评论 -
LeetCode-Ugly Number
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly si原创 2015-08-27 17:05:39 · 978 阅读 · 0 评论 -
LeetCode-Number of Digit One(编程之美-1的数目)
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.For example:Given n = 13,Return 6, because digit 1 occurred in the followin原创 2015-08-29 18:05:33 · 2338 阅读 · 1 评论 -
LeetCode-Same Tree & Symmetric Tree
两道题放一块了,一个是判断两棵树是否相同:Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have原创 2015-02-03 17:50:48 · 1118 阅读 · 0 评论 -
LeetCode-Gray Code
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 total number of bits in the code, print the sequence of原创 2015-08-21 11:43:47 · 920 阅读 · 0 评论 -
LeetCode-Generate Parentheses & Letter Combinations of a Phone Number
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()原创 2015-08-20 14:20:53 · 1094 阅读 · 0 评论 -
LeetCode-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原创 2015-08-16 18:48:04 · 1577 阅读 · 0 评论 -
LeetCode-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.You may assume that the array is non-empty and the majority原创 2015-01-27 10:31:47 · 887 阅读 · 0 评论 -
LeetCode-Single NumberI II III
I)Given an array of integers, every element appears twice except for one. Find that single one.Note:Your algorithm should have a linear runtime complexity. Could you implement it without usi原创 2015-08-17 20:30:34 · 992 阅读 · 0 评论 -
LeetCode-3SUM(数组中三数之和为0)及其 closest
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c原创 2015-02-07 15:44:08 · 2009 阅读 · 0 评论 -
LeetCode-Path Sum II
题目: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原创 2015-01-30 21:11:27 · 642 阅读 · 0 评论 -
LeetCode - Populating Next Right Pointers in Each Node II 及其变形题
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *next; }Populate each next pointer to point to its next right node.原创 2015-08-15 18:10:11 · 1042 阅读 · 0 评论 -
LeetCode-Validate Binary Search Tree
Given a binary tree, determine if it is a valid binary search tree (BST).Assume a BST is defined as follows:The left subtree of a node contains only nodes with keys less than the node's key.Th原创 2015-08-15 16:10:26 · 1112 阅读 · 0 评论 -
LeetCode-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,原创 2015-08-30 13:23:37 · 1144 阅读 · 0 评论 -
LeetCode-Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2015-08-14 16:53:10 · 803 阅读 · 0 评论 -
LeetCode-Binary Tree Level Order Traversal
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20原创 2015-08-14 17:28:44 · 947 阅读 · 0 评论 -
LeetCode-Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.没什么可说的,直接上代码:原创 2015-08-14 21:16:15 · 698 阅读 · 0 评论 -
LeetCode-Best Time to Buy and Sell Stock I II III IV
此处的三个题跟Maximum Subarray,可以先看此题I)Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, b原创 2015-08-25 17:46:27 · 1105 阅读 · 0 评论 -
LeetCode-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] ha原创 2015-08-25 15:59:19 · 1205 阅读 · 0 评论 -
LeetCode-Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.跟题目http://blog.youkuaiyun.com/my_jobs/article/details/431原创 2015-02-04 22:15:22 · 590 阅读 · 0 评论 -
LeetCode-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原创 2015-02-04 21:25:45 · 900 阅读 · 0 评论 -
LeetCode-Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".自己的解答:public String addBinary(String a, String b) { if (a == null ||原创 2015-02-04 20:37:50 · 591 阅读 · 0 评论 -
LeetCode-Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially fille原创 2015-02-04 18:34:30 · 829 阅读 · 0 评论 -
编程之美-分层遍历二叉树
问题:给定一个二叉树,要求按分层遍历该二叉树,即从上到下按层次访问该二叉树(每一层将单独输出一行),每一层要求访问的顺序为从左到右,并将节点依次编号。那么分层遍历如图的二叉树,正确的输出应该为:原创 2015-02-03 17:10:08 · 2017 阅读 · 0 评论 -
LeetCode-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 int strStr(String haystack, String needle原创 2015-02-04 10:21:32 · 924 阅读 · 0 评论 -
二叉树遍历(前序、中序、后序、层次遍历、深度优先、广度优先)
二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树,有深度遍历和广度遍历,深度遍历有前序、中序以及后序三种遍历方法,广度遍历即我们平常所说的层次遍历。因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易理解而且代码很简洁,而对于广度遍历来说,需要其他数据结构的支撑,比如堆了。所以,对于一段代码来说,可读性有时候要比代码本身的效率要重要的原创 2015-02-03 16:10:52 · 423908 阅读 · 41 评论 -
LeetCode-Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2015-02-03 18:25:10 · 852 阅读 · 0 评论