LeetCode
文章平均质量分 75
hellochenlu
这个作者很懒,什么都没留下…
展开
-
24 - Swap Nodes in Pairs
不说废话,先贴上代码#include "stdafx.h"#include #include using namespace std;struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} };class Solution_024_SwapNodesinPair原创 2015-08-18 21:30:56 · 342 阅读 · 1 评论 -
87 - Scramble String(DP)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr原创 2016-03-22 09:05:15 · 463 阅读 · 0 评论 -
43 - Multiply Strings
Given two numbers represented as strings, return multiplication of the numbers as a string.Note: The numbers can be arbitrarily large and are non-negative.Subscribe to see which companies原创 2016-03-22 14:29:04 · 301 阅读 · 0 评论 -
65 - Valid Number(DFA-Deterministic Finite Automaton)
Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambiguo原创 2016-03-10 15:17:41 · 568 阅读 · 0 评论 -
206 - Reverse Linked List
Reverse a singly linked list.click to show more hints.Subscribe to see which companies asked this question思路分析:此题较容易。我们可以设置一个新的指针newHead。初始化为NULL。此时,迭代原始的链表。每一次把原始的链表的节点从头插入newHead原创 2016-03-10 10:44:35 · 289 阅读 · 0 评论 -
105 - Construct Binary Tree from Preorder and Inorder Traversal
Given preorder and inorder traversal of a tree, construct the binary tree.Note:You may assume that duplicates do not exist in the tree.Subscribe to see which companies asked this questio原创 2016-03-09 15:55:55 · 320 阅读 · 0 评论 -
69 - Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.Subscribe to see which companies asked this question思路分析:牛顿迭代法求解平方根。此时,令f(x) = x^2 - a, 此时迭代格式为:class Soluti原创 2016-03-09 11:22:48 · 440 阅读 · 0 评论 -
102 - Binary Tree Level Order Traversal(BFS)
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原创 2016-03-08 16:11:21 · 264 阅读 · 0 评论 -
102 - Binary Tree Level Order Traversal(DFS)
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原创 2016-03-08 15:27:29 · 291 阅读 · 0 评论 -
44 - Wildcard Matching(C)
Implement wildcard pattern matching with support for '?' and '*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover t原创 2016-03-22 15:33:53 · 425 阅读 · 0 评论 -
94 - Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solutio原创 2016-03-11 09:30:09 · 385 阅读 · 0 评论 -
96 - Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \原创 2016-03-11 10:44:37 · 413 阅读 · 0 评论 -
176 - Second Highest Salary
Write a SQL query to get the second highest salary from the Employee table.+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 |+----+--------+For exa原创 2016-03-15 15:09:02 · 616 阅读 · 0 评论 -
177 - Nth Highest Salary
Write a SQL query to get the nth highest salary from the Employee table.+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 |+----+--------+For exampl原创 2016-03-15 14:46:58 · 410 阅读 · 0 评论 -
175 - Combine Two Tables
Table: Person+-------------+---------+| Column Name | Type |+-------------+---------+| PersonId | int || FirstName | varchar || LastName | varchar |+-------------+---------+Per原创 2016-03-15 14:24:20 · 314 阅读 · 0 评论 -
192 - Word Frequency
Write a bash script to calculate the frequency of each word in a text file words.txt.For simplicity sake, you may assume:words.txt contains only lowercase characters and space ' ' characters原创 2016-03-15 11:18:32 · 825 阅读 · 0 评论 -
110 - 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原创 2016-03-14 15:28:57 · 307 阅读 · 0 评论 -
109 - Convert Sorted List to Binary Search Tree
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.Subscribe to see which companies asked this question思路分析:这题的关键是能找出当前链表的中间节点,然后再原创 2016-03-14 14:34:47 · 278 阅读 · 0 评论 -
108 - Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.Subscribe to see which companies asked this question思路分析:递归。class Solution {public:原创 2016-03-14 14:09:34 · 252 阅读 · 0 评论 -
97 - 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原创 2016-03-14 09:39:47 · 1229 阅读 · 0 评论 -
193 - Valid Phone Numbers
Given a text file file.txt that contains list of phone numbers (one per line), write a one liner bash script to print all valid phone numbers.You may assume that a valid phone number must appear原创 2016-03-15 10:19:01 · 776 阅读 · 0 评论 -
50 - Pow(x, n)
用二分法,O(logn)。注意n /**/#include "stdafx.h"#include using namespace std;class Solution_050_PowXn{public: double power(double x, int n) { if (n == 0) { return 1; } double v = pow转载 2016-03-04 14:52:53 · 525 阅读 · 0 评论 -
49 - Group Anagrams
Given an array of strings, group anagrams together.For example, given: [“eat”, “tea”, “tan”, “ate”, “nat”, “bat”], Return:[ [“ate”, “eat”,”tea”], [“nat”,”tan”], [“bat”] ] Note: For原创 2016-03-04 13:28:28 · 438 阅读 · 0 评论 -
48 - RotateImage
Rotate ImageYou are given an n x n 2D matrix representing an image.Rotate the image by 90 degrees (clockwise).Follow up:Could you do this in-place?原创 2016-03-04 10:12:13 · 695 阅读 · 0 评论 -
75 - Sort Colors
Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the integers原创 2016-03-16 10:39:08 · 310 阅读 · 0 评论 -
74 - Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each原创 2016-03-16 09:57:21 · 264 阅读 · 0 评论 -
46 - Permutations(全排列和递归)
Given a collection of distinct numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].Subscrib原创 2016-03-03 10:37:44 · 1249 阅读 · 1 评论 -
LeetCode题目难度分布(含面试频率及使用的数据结构与算法)
ID Question Diff Freq Data StructureAlgorithms转载 2016-03-02 11:14:55 · 3308 阅读 · 0 评论 -
深度优先遍历算法求解 Letter Combinations of a Phone Number
mark,等下更新原创 2015-08-21 09:27:37 · 349 阅读 · 0 评论 -
80 - Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first fi原创 2016-03-17 14:51:13 · 255 阅读 · 0 评论 -
76 - Minimum Window Substring
Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "BAN原创 2016-03-17 15:35:11 · 404 阅读 · 0 评论 -
33 - Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array retur原创 2016-03-17 16:13:49 · 291 阅读 · 0 评论 -
195 - Tenth Line
How would you print just the 10th line of a file?For example, assume that file.txt has the following content:Line 1Line 2Line 3Line 4Line 5Line 6Line 7Line 8Line 9Line 10Your script原创 2016-03-15 09:10:40 · 890 阅读 · 0 评论 -
70 - 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?Subscribe to see which c原创 2016-03-08 11:35:40 · 256 阅读 · 0 评论 -
68 - Text Justification
Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified.You should pack your words in a greedy approach; that i原创 2016-03-08 09:52:34 · 647 阅读 · 0 评论 -
66 - Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.Subscribe to see原创 2016-03-07 14:43:23 · 353 阅读 · 0 评论 -
58 - Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is原创 2016-03-07 14:20:44 · 295 阅读 · 0 评论 -
56 - Merge Intervals
Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary).You may assume that the intervals were initially sorted according to their start times.E原创 2016-03-07 13:27:20 · 288 阅读 · 0 评论 -
87 - Scramble String(recursive)
Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.Below is one possible representation of s1 = "great": great / \ gr原创 2016-03-18 15:14:12 · 335 阅读 · 0 评论 -
53 - 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]原创 2016-03-07 10:24:45 · 483 阅读 · 0 评论