Leetcode
文章平均质量分 74
YMHH
Enjoy the coding, enjoy the life together
展开
-
Leetcode: 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 it as a link原创 2013-12-26 22:49:16 · 729 阅读 · 0 评论 -
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""213""231""3原创 2014-01-01 15:58:37 · 908 阅读 · 0 评论 -
Leetcode: 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原创 2014-01-01 16:49:34 · 619 阅读 · 0 评论 -
Leetcode: Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the原创 2013-12-29 16:21:19 · 600 阅读 · 0 评论 -
Leetcode: String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input ca原创 2013-12-25 23:22:11 · 585 阅读 · 0 评论 -
Leetcode: Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array.原创 2013-12-21 15:53:54 · 602 阅读 · 0 评论 -
Leetcode: Divide Two Integers
Divide two integers without using multiplication, division and mod operator.需要很多边界检查,要注意时间复杂度。class Solution {public: int divide(int dividend, int divisor) { if (divisor == 0) {原创 2013-12-30 21:17:57 · 717 阅读 · 0 评论 -
Leetcode: Sort List - 快排
Sort a linked list in O(n log n) time using constant space complexity.写呀写,一次又一次,总是搞不定,要么运行出错,要么超时。网上看了一下,原来需要考虑重复pivot的情况。总算过了。/** * Definition for singly-linked list. * struct ListNode { *原创 2013-12-29 14:38:14 · 1214 阅读 · 0 评论 -
Leetcode: Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321class Solution {public: int reverse(int x) { bool negative = false; if (x原创 2013-12-17 23:00:28 · 615 阅读 · 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原创 2013-12-28 11:46:58 · 736 阅读 · 0 评论 -
Leetcode: Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y原创 2014-01-02 23:58:15 · 569 阅读 · 0 评论 -
Leetcode: Merge Intervals
Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].比较简单,排序之后遍历即可。注意比较函数需要是global或者static的,cplusplus上说原创 2014-01-02 20:30:41 · 684 阅读 · 0 评论 -
Leetcode: First Missing Positive
Given an unsorted integer array, find the first missing positive integer.For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2.Your algorithm should run in O(n) time and uses constant原创 2014-01-05 19:50:41 · 595 阅读 · 0 评论 -
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.Eac原创 2014-01-11 21:22:16 · 691 阅读 · 0 评论 -
Leetcode: N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.用I的vector过不了,用数组速度应该会快一些,再加速一下同一列上是否合法的判断,过了。class Solution {原创 2014-01-11 22:48:18 · 634 阅读 · 0 评论 -
Leetcode: Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.可以做Trie树,不过实现起来比较复杂,貌似时间复杂度也不低。干脆直接比较,也能通过。class Solution {public: string longestCommonPrefix(vector &str原创 2014-01-12 20:32:22 · 640 阅读 · 0 评论 -
Leetcode: Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ]]原创 2014-01-13 22:29:53 · 635 阅读 · 0 评论 -
Leetcode: 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 ], [ 8, 9, 4 ], [原创 2014-01-13 23:35:29 · 754 阅读 · 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原创 2014-01-14 21:23:22 · 801 阅读 · 0 评论 -
Leetcode: 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原创 2014-01-14 23:06:46 · 680 阅读 · 0 评论 -
Leetcode: 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原创 2014-01-14 23:59:44 · 658 阅读 · 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原创 2014-01-16 22:17:26 · 631 阅读 · 0 评论 -
Leetcode: 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]原创 2014-01-26 00:19:28 · 751 阅读 · 0 评论 -
Leetcode: Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.比较简单,DP,不过貌似不能用vec原创 2014-01-18 18:22:59 · 726 阅读 · 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原创 2014-02-01 20:56:00 · 710 阅读 · 0 评论 -
Leetcode: 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原创 2014-02-02 17:10:14 · 777 阅读 · 0 评论 -
Leetcode: 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原创 2014-02-01 14:33:46 · 701 阅读 · 0 评论 -
Leetcode: 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 devis原创 2014-02-01 23:00:01 · 609 阅读 · 0 评论 -
Leetcode: Roman to Integer
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.顺序扫描,注意IV这种情况。class Solution {public: int romanToInt(string s) { int原创 2014-02-02 21:16:29 · 641 阅读 · 0 评论 -
Leetcode: Container With Most Water
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fin原创 2014-01-31 15:15:36 · 643 阅读 · 0 评论 -
Leetcode: Median of Two Sorted Arrays
There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).折腾了将近四个小时,最后还是。。。向牛们学习大概的思路就是折半原创 2014-01-31 22:11:58 · 748 阅读 · 0 评论 -
Leetcode: 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 statement to be ambiguo原创 2014-02-01 10:29:20 · 627 阅读 · 0 评论 -
Leetcode: Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.类似于胜者树的思想,用一个优先队列来实现K路归并,时间复杂度O(nlogk),k为归并的路数。比较函数需要注意,用函数指针过不了,为什么呢?/** * Definition for sing原创 2014-02-01 17:22:11 · 650 阅读 · 0 评论 -
Leetcode: Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.写的比较复杂,有简单的方法,比如用table来保存特殊的组合,然后遍历。class Solution {public: string intToRoman原创 2014-02-02 22:08:47 · 579 阅读 · 0 评论 -
Leetcode: Reverse Nodes in k-Group
Given a linked list, reverse the nodes of a linked list k 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原创 2014-02-04 22:47:33 · 613 阅读 · 0 评论 -
Leetcode: Wildcard Matching
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原创 2014-02-21 23:54:15 · 972 阅读 · 0 评论 -
Leetcode: Sudoku Solver
Write a program to solve a Sudoku puzzle by filling the empty cells.Empty cells are indicated by the character '.'.You may assume that there will be only one unique solution.A sudoku原创 2014-02-23 00:14:24 · 685 阅读 · 0 评论 -
Leetcode: 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 is not found原创 2014-02-11 23:59:20 · 680 阅读 · 0 评论 -
Leetcode: Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Determine i原创 2014-02-15 21:42:40 · 690 阅读 · 0 评论 -
Leetcode: Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the array.Each element in the array represents your maximum jump length at that position.Your goal i原创 2014-02-16 13:47:56 · 710 阅读 · 0 评论