Leetcode
文章平均质量分 63
lym1108csu
这个作者很懒,什么都没留下…
展开
-
Leetcode NO.169 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 majo原创 2015-01-26 11:32:49 · 511 阅读 · 0 评论 -
Leetcode NO.147 Insertion Sort List
本题题目要求如下:Sort a linked list using insertion sort.题目相当之简单,而且本题都把算法限定为insertion sort,所以也没有改进的余地,基本来讲,本题就考一个小技巧以及基本功。。很可惜,我基本功很烂,当然跟没有想出那个小技巧简化问题也有关系。。。题目代码如下:/** * Definition for singly-lin原创 2015-01-28 08:39:59 · 487 阅读 · 0 评论 -
Leetcode NO.34 Search for a Range
题目要求如下:Given a sorted array of integers, find the starting and ending position of a given target value.Your algorithm's runtime complexity must be in the order of O(log n).If the target原创 2015-01-29 10:48:39 · 455 阅读 · 0 评论 -
Leetcode NO.81 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 tar原创 2015-01-29 11:40:51 · 333 阅读 · 0 评论 -
Leetcode NO.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 th原创 2015-01-29 11:16:26 · 327 阅读 · 0 评论 -
Leetcode NO.59 Spiral Matrix II
本题题目要求如下:Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ],原创 2015-02-02 05:25:26 · 435 阅读 · 0 评论 -
Leetcode NO.113 Path Sum II
本题题目要求如下:Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.For example:Given the below binary tree and sum = 22, 5原创 2015-02-03 01:13:46 · 373 阅读 · 0 评论 -
Leetcode NO.153 Find Minimum 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).Find the minimum element.You may assume no duplica原创 2015-01-14 12:25:14 · 377 阅读 · 0 评论 -
Leetcode NO.104 Maximum Depth of Binary Tree
题目要求如下:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.本题应该算是Leetcode里面最简单的题之一原创 2015-02-03 01:45:25 · 376 阅读 · 0 评论 -
Leetcode NO.77 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原创 2015-02-04 00:53:03 · 369 阅读 · 0 评论 -
Leetcode NO.167 Two Sum II - Input array is sorted
本题题目要求如下:Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.The function twoSum should return indices of原创 2015-01-17 05:23:46 · 681 阅读 · 0 评论 -
Leetcode NO.73 Set Matrix Zeroes
本题要求如下:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solution using O(mn) space is pro原创 2015-01-16 07:01:21 · 449 阅读 · 0 评论 -
Leetcode NO.142 Linked List Cycle II
题目要求如下:Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Follow up:Can you solve it without using extra space?其实就是原来做过的tow pointers问题,本题并不算难。原创 2015-01-16 09:28:11 · 381 阅读 · 0 评论 -
Leetcode NO.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 in原创 2015-01-16 07:49:21 · 397 阅读 · 0 评论 -
Leetcode NO.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 A = [1,1,1,2,2,3],Your function should return length = 5, and A i原创 2015-01-17 08:48:37 · 478 阅读 · 0 评论 -
Leetcode NO.86 Partition List
题目要求如下:Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nod原创 2015-01-18 06:15:37 · 411 阅读 · 0 评论 -
Leetcode NO.171 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-02-06 01:23:13 · 311 阅读 · 0 评论 -
Leetcode NO.16 3Sum Closest
本题要求如下:Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would原创 2015-01-19 09:06:00 · 470 阅读 · 0 评论 -
Leetcode NO.61 Rotate List
本题要求如下:Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.这题的思路很简单,但是我觉得我的方法并不好,原创 2015-01-18 10:32:20 · 368 阅读 · 0 评论 -
Leetcode NO.39 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原创 2015-02-06 01:04:36 · 440 阅读 · 0 评论 -
Leetcode NO.15 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 t原创 2015-01-19 08:14:47 · 501 阅读 · 0 评论 -
Leetcode NO.18 4Sum
本题要求如下:Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note:原创 2015-01-20 05:31:26 · 365 阅读 · 0 评论 -
Leetcode NO.90 Subsets II
本道题题目要求如下:Given a collection of integers that might contain duplicates, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must n原创 2015-02-08 07:00:01 · 413 阅读 · 0 评论 -
Leetcode NO.3 Longest Substring Without Repeating Characters
本题要求如下:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length原创 2015-01-20 08:06:52 · 431 阅读 · 0 评论 -
Leetcode NO.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?本题也是第二次做,不过这次更加了解原创 2015-02-07 10:06:45 · 364 阅读 · 0 评论 -
Leetcode NO.63 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 respectiv原创 2015-02-07 08:07:01 · 443 阅读 · 0 评论 -
Leetcode NO.78 Subsets
题目要求如下:Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.原创 2015-01-22 12:14:45 · 323 阅读 · 0 评论 -
Leetcode NO.47 Permutations II
本题题目要求如下:Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1],原创 2015-02-20 14:07:43 · 360 阅读 · 0 评论 -
Leetcode NO.62 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 tryin原创 2015-02-17 04:37:15 · 377 阅读 · 0 评论 -
Leetcode NO.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原创 2015-02-17 03:50:51 · 340 阅读 · 0 评论 -
Leetcode NO.128 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 seque原创 2015-02-17 06:56:21 · 348 阅读 · 0 评论 -
Leetcode NO.119 Pascal's Triangle II
本题题目要求如下:Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?原创 2015-02-17 14:32:26 · 380 阅读 · 0 评论 -
Leetcode NO.65 Valid Number
本题题目要求如下: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 statemen原创 2015-02-20 11:26:38 · 380 阅读 · 0 评论 -
Leetcode NO.118 Pascal's Triangle
本题题目要求如下:Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]本题就是考vector操作原创 2015-02-17 07:22:26 · 302 阅读 · 0 评论 -
Leetcode NO.50 Pow(x, n)
本题要求如下:Implement pow(x, n).就是实现C++自带的pow()函数,极其简单,就是用recursive的方法:pow(x,n) = pow(x*x, n/2)这样下来,当然有些情况(n的奇偶,正负),时间复杂度就是O(logN)。代码如下:class Solution {public: double pow(double x, int n) {原创 2015-01-23 07:07:42 · 380 阅读 · 0 评论 -
Leetcode NO.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 us原创 2015-02-26 09:47:28 · 609 阅读 · 0 评论 -
Leetcode NO.60 Permutation Sequence
题目要求如下:The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132原创 2015-01-23 06:10:07 · 353 阅读 · 0 评论 -
Leetcode NO.2 Add Two Numbers
题目要求如下:You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return原创 2015-01-24 06:51:47 · 368 阅读 · 0 评论 -
Leetcode NO.17 Letter Combinations of a Phone Number
本题题目要求如下:Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.原创 2015-02-25 12:36:32 · 339 阅读 · 0 评论 -
Leetcode NO.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.应该说这算是leetcode里面最简单的一道题了。。基本就是我天朝小学数学的水平。。。。数多少个0其实本质原创 2015-02-26 07:52:14 · 320 阅读 · 0 评论