
leetcode
文章平均质量分 57
Nightonke
学生而已,请多指教。
展开
-
LeetCode OJ Edit Distance
LeetCode OJ Edit Distance 题解。原创 2015-03-25 13:46:25 · 1061 阅读 · 0 评论 -
LeetCode OJ Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3原创 2015-03-17 00:27:28 · 457 阅读 · 0 评论 -
LeetCode OJ 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 element al原创 2015-03-17 00:26:25 · 391 阅读 · 0 评论 -
LeetCode OJ Excel Sheet Column Title
Given a positive integer, return its corresponding column title as appear in an Excel sheet.For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA原创 2015-03-17 00:26:51 · 381 阅读 · 0 评论 -
LeetCode OJ Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note: A solution using O(n) space is pretty straight forward. Could you devise原创 2015-03-21 08:56:40 · 398 阅读 · 0 评论 -
LeetCode OJ Convert Sorted Array to Binary Search
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.class Solution {public:vector Num;TreeNode *sortedArrayToBST(vector &num) {if (num.size() == 0) r原创 2015-03-21 08:56:47 · 648 阅读 · 0 评论 -
LeetCode OJ Regular Expression Matching
Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input st原创 2015-03-21 09:19:44 · 368 阅读 · 0 评论 -
LeetCode OJ Substring with Concatenation of All Words
https://oj.leetcode.com/discuss/366/better-solution-than-brute-force看了下面的评论。思路是:例如"foobarisbarfoobar" ["foo","bar"],那么将S划分为:1、foo, bar, isb, arf, oob, ar;2、oob, ari, sba, rfo, oba, r转载 2015-03-21 12:38:07 · 388 阅读 · 0 评论 -
LeetCode OJ Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.For example, Given [0,1,0,2,1,0,1,3,2,1,2,1]原创 2015-03-21 09:28:00 · 403 阅读 · 0 评论 -
LeetCode OJ Reverse Bits
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as00000010100101000001111010011100), return 964176192 (represented in binary as00111001011原创 2015-03-21 09:54:55 · 428 阅读 · 0 评论 -
LeetCode OJ Unique Paths II
Follow up for "Unique Paths":Now consider if some obstacles are added to the grids. How many unique paths would there be?An obstacle and empty space is marked as 1 and 0 respectively in the原创 2015-03-21 18:31:19 · 345 阅读 · 0 评论 -
LeetCode OJ Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right at原创 2015-03-21 18:32:45 · 367 阅读 · 0 评论 -
LeetCode OJ 3Sum
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-03-21 18:37:13 · 332 阅读 · 0 评论 -
LeetCode OJ Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4],]原创 2015-03-21 18:38:15 · 260 阅读 · 0 评论 -
LeetCode OJ Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.c原创 2015-03-21 18:38:26 · 322 阅读 · 0 评论 -
LeetCode OJ 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-03-21 18:39:52 · 380 阅读 · 0 评论 -
LeetCode OJ Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2015-03-21 18:40:30 · 378 阅读 · 0 评论 -
LeetCode OJ Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,原创 2015-03-21 18:43:25 · 360 阅读 · 0 评论 -
LeetCode OJ Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence.For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3原创 2015-03-21 18:43:29 · 389 阅读 · 0 评论 -
LeetCode OJ Word Ladder II
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that:Only one letter can be changed at a timeEach intermediate word must exi原创 2015-03-28 10:47:37 · 654 阅读 · 0 评论 -
LeetCode OJ Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked listk at a time and return its modified list.If the number of nodes is not a multiple of k then left-out nodes in the end should remain as it is.原创 2015-03-21 08:50:43 · 396 阅读 · 0 评论 -
LeetCode OJ Scramble String
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原创 2015-03-21 09:23:22 · 456 阅读 · 0 评论 -
LeetCode OJ Find Minimum in Rotated Sorted Array II
这套题的方法和它的前代是一模一样的,找到一个Gap,也就是分界点,这个分界点的前一个元素大于后一个元素。在前代中我们逐步缩小Gap的范围的方法是比较num[l], num[mid], num[h]的值,由于前代中确保不会出现重复的元素,所以这种方法是可行的,但如果有重复元素就会出现无法确定Gap所处范围的问题,比如:3, 1, 3, 3, 3;3, 3, 3,原创 2015-03-21 09:24:48 · 421 阅读 · 0 评论 -
LeetCode OJ Simplify Path
Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"click to show corner cases.Corner Cases:Di原创 2015-03-21 18:41:33 · 335 阅读 · 0 评论 -
LeetCode OJ Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the原创 2015-03-21 18:42:49 · 342 阅读 · 0 评论 -
LeetCode OJ 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-03-21 18:44:51 · 388 阅读 · 0 评论 -
LeetCode OJ 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", r原创 2015-03-21 08:53:08 · 381 阅读 · 0 评论 -
LeetCode OJ Candy
There are N children standing in a line. Each child is assigned a rating value.You are giving candies to these children subjected to the following requirements:Each child must have at least on转载 2015-03-21 09:22:13 · 372 阅读 · 0 评论 -
LeetCode OJ 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 000000原创 2015-03-21 09:38:33 · 410 阅读 · 0 评论 -
LeetCode OJ 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 yo转载 2015-03-21 10:22:29 · 385 阅读 · 0 评论 -
LeetCode OJ Wildcard Matching
https://oj.leetcode.com/discuss/10133/linear-runtime-and-constant-space-solutionclass Solution {public: bool isMatch(const char * s, const char * p) { int posOfStar = -1; // p中最近一次*号的位转载 2015-03-21 12:39:13 · 416 阅读 · 0 评论 -
LeetCode OJ Permutations II
class Solution {public: vector > permuteUnique(vector &num) { vector n = num; sort(n.begin(), n.end()); vector ori = n; vector > ans; while (1) {原创 2015-03-21 12:39:24 · 386 阅读 · 0 评论 -
LeetCode OJ Permutations
Given a collection of 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].class Solution {public:原创 2015-03-21 12:41:18 · 384 阅读 · 0 评论 -
LeetCode OJ Combination Sum
Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.The same repeated number may be chosen from C unlimited numb原创 2015-03-21 12:42:27 · 395 阅读 · 0 评论 -
LeetCode OJ Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible原创 2015-03-21 12:44:09 · 403 阅读 · 0 评论 -
LeetCode OJ Permutation Sequence
求第k个排列,下面给出一个可以直接求的方法(不用枚举)。http://blog.youkuaiyun.com/modiziri/article/details/22389303截取一段出来:康托展开的公式:(不用记,看形势就行,下面会有例子)X=an*(n-1)!+an-1*(n-2)!+...+ai*(i-1)!+...+a2*1!+a1*0!ai为整数,转载 2015-03-21 12:48:39 · 470 阅读 · 0 评论 -
LeetCode OJ Insert Interval
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原创 2015-03-21 09:28:58 · 404 阅读 · 0 评论 -
LeetCode OJ Jump Game II
class Solution {public: int jump(int A[], int n) { int * step = new int[n]; step[0] = 0; int pos = 1; for (int i = 0; i < n; i++) { int j;原创 2015-03-21 12:40:01 · 373 阅读 · 0 评论 -
LeetCode OJ 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]原创 2015-03-21 12:43:09 · 423 阅读 · 0 评论 -
LeetCode OJ Unique Paths
A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is trying to reach the原创 2015-03-21 18:32:07 · 396 阅读 · 0 评论