LeetCode
文章平均质量分 79
hustyangju
这个作者很懒,什么都没留下…
展开
-
LeetCode题解 || Two Sum问题
question:Given an array of integers, find two numbers such that they add up to a specific target number.The function twoSum should return indices of the two numbers such that they add up to the原创 2015-01-13 10:18:19 · 801 阅读 · 0 评论 -
LeetCode题解 || Add Two Numbers 问题
problem: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原创 2015-01-13 16:55:43 · 745 阅读 · 0 评论 -
leetcode 题解 || Letter Combinations of a Phone Number 问题
problem: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 belo原创 2015-03-19 11:11:24 · 816 阅读 · 0 评论 -
leetcode 题解 || Search Insert Position 问题
problem: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 dupl原创 2015-03-25 10:42:02 · 605 阅读 · 0 评论 -
leetcode 题解 || Count and Say 问题
problem: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.原创 2015-03-25 17:28:06 · 1018 阅读 · 0 评论 -
leetcode 题解 || Search for a Range 问题
problem: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 ta原创 2015-03-25 09:52:00 · 714 阅读 · 0 评论 -
leetcode 题解 || Valid Sudoku 问题
problem: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原创 2015-03-25 15:13:12 · 1217 阅读 · 0 评论 -
leetcode 题解 || 3Sum Closest 问题
problem: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-03-19 10:05:20 · 635 阅读 · 0 评论 -
求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
(一)前言做过leetcode的人都知道, 里面有2sum, 3sum(closest), 4sum等问题, 这些也是面试里面经典的问题, 考察是否能够合理利用排序这个性质, 一步一步得到高效的算法. 经过总结, 本人觉得这些问题都可以使用一个通用的K sum求和问题加以概括消化, 这里我们先直接给出K Sum的问题描述和算法(递归解法), 然后将这个一般性的方法套用到具体的K, 比如leet原创 2015-03-19 16:35:18 · 2633 阅读 · 0 评论 -
leetcode 题解 || Remove Nth Node From End of List 问题
problem: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原创 2015-03-19 19:01:48 · 868 阅读 · 0 评论 -
leetcode 题解 || Merge Two Sorted Lists 问题
problem: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.将两个已序链表合并,新链表需要拆分组合原来链表的结点,言外之意,不需要新建结点。原创 2015-03-19 21:48:31 · 504 阅读 · 0 评论 -
leetcode 题解 || Valid Parentheses 问题
problem:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are原创 2015-03-19 20:34:56 · 743 阅读 · 0 评论 -
leetcode题解||Median of Two Sorted Arrays问题
problem: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)).thinking:(1)求中位数,就是求已原创 2015-03-16 19:22:26 · 746 阅读 · 0 评论 -
leetcode || 39、Combination Sum
problem: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 f原创 2015-03-26 15:34:08 · 698 阅读 · 0 评论 -
leetcode || 40、Combination Sum II
problem:Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.Each number in C may only be used原创 2015-03-26 16:45:03 · 418 阅读 · 0 评论 -
leetcode || 41、First Missing Positive 问题
problem: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原创 2015-03-26 20:00:09 · 614 阅读 · 0 评论 -
LeetCode题解 || Longest Substring Without Repeating Characters (O(n)算法)问题
problem: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-03-16 15:52:32 · 1265 阅读 · 0 评论 -
leetcode || 42、 Trapping Rain Water
problem: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 [原创 2015-03-27 10:46:28 · 1169 阅读 · 0 评论 -
leetcode || 43、Multiply Strings
problem: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.Hide Tags Math原创 2015-03-27 16:07:14 · 860 阅读 · 0 评论 -
leecode 题解 || Merge k Sorted Lists 问题
problem:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.Tags Divide and Conquer Linked List Heap合并K个已序单链表thinking: (1)题目没有要求不可以新开ListN原创 2015-03-20 15:44:05 · 835 阅读 · 0 评论 -
leetcode 题解 || Swap Nodes in Pairs 问题
problem: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 spa原创 2015-03-20 17:51:06 · 840 阅读 · 0 评论 -
leetcode题解||ZigZag Conversion问题
problem:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA原创 2015-03-17 14:18:08 · 1179 阅读 · 0 评论 -
leetcode题解||Palindrome Number问题
problem:Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of conv原创 2015-03-17 20:13:00 · 729 阅读 · 0 评论 -
leetcode题解||Reverse Integer 问题
problem:Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321thinking:(1)整型反转直观很容易理解。如正负,尾数为0等问题还好处理。(2)反转溢出问题要仔细处理。code:class Solution {public:原创 2015-03-17 16:37:05 · 704 阅读 · 0 评论 -
leetcode || 48、Rotate Image
problem:You 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?Hide Tags Array题意:将一个矩阵顺时针旋原创 2015-03-30 16:44:29 · 683 阅读 · 0 评论 -
leetcode || 49、Anagrams
problem:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.Hide Tags Hash Table String题意:给定多余两组的字符串,找出其中所有的满足以下条原创 2015-03-30 17:19:09 · 672 阅读 · 0 评论 -
leetcode || 45、 Jump Game II
problem: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 posi原创 2015-03-30 10:49:25 · 625 阅读 · 0 评论 -
leetcode || 46、Permutations
problem: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,原创 2015-03-30 15:32:01 · 684 阅读 · 0 评论 -
leetcode || 47、 Permutations II
problem: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-03-30 16:17:55 · 629 阅读 · 0 评论 -
leetcode 题解 || Remove Duplicates from Sorted Array 问题
problem: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 pl原创 2015-03-23 10:56:05 · 612 阅读 · 0 评论 -
leetcode || 60、Permutation Sequence
problem: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):"1原创 2015-04-01 21:43:36 · 460 阅读 · 0 评论 -
leetcode题解||Regular Expression Matching 问题
problem: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 entir原创 2015-03-18 11:15:43 · 1115 阅读 · 0 评论 -
leetcode || 50、Pow(x, n)
problem:Implement pow(x, n).Hide Tags Math Binary Search题意:求x的n次幂thinking:(1)最简单想到的是直观上的数学幂函数求法,测试通过。算法时间复杂度为O(n)(2)按照标签提示,使用二分搜索法。pow(x,n) = po原创 2015-03-31 09:15:00 · 725 阅读 · 0 评论 -
leetcode题解 || Roman to Integer问题
problem:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.将罗马数字转为整数thinking:(1)罗马数字规则:罗马数字共有7个,即I(1)、V(5)、X(10)、L(50)、C(100)原创 2015-03-18 16:25:46 · 1053 阅读 · 0 评论 -
leetcode || 64、Minimum Path Sum
problem: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 e原创 2015-04-02 15:47:03 · 764 阅读 · 0 评论 -
leetcode 题解 || 3Sum 问题
problem: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原创 2015-03-18 19:11:27 · 470 阅读 · 0 评论 -
leetcode题解||Container With Most Water问题
problem: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)原创 2015-03-18 15:28:39 · 791 阅读 · 0 评论 -
leetcode || 61、Rotate List
problem: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.Hide Tags L原创 2015-04-02 09:25:51 · 641 阅读 · 0 评论 -
leetcode || 62、Unique Paths
problem: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原创 2015-04-02 11:24:55 · 842 阅读 · 0 评论 -
leetcode题解||Integer to Roman问题
problem:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.将1-3999的整数转换为罗马数字thinking:(1)对照举例个位数举例Ⅰ,1 】Ⅱ,2】 Ⅲ,3】 Ⅳ,4 】Ⅴ,5 】Ⅵ原创 2015-03-18 16:11:56 · 759 阅读 · 0 评论