- 博客(46)
- 资源 (1)
- 收藏
- 关注
原创 45 - Anagrams
Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.solution: 遍历strs,排序每一个strs的字符串,用一个map>保存排序后的结果为key,value以vector形式存储排序前的字符串,然后构成
2013-10-22 07:46:43 428
原创 44 - Rotate Image
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?solution: 实地画一个矩阵,顺时针翻转90度,结果可以以这样两步得到,1,以(0,
2013-10-22 06:46:15 405
原创 43 - 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], and [2,1,1].
2013-10-22 05:53:47 444
原创 42 - 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].solut
2013-10-15 08:01:52 360
原创 41 - 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
2013-10-15 07:17:07 335
原创 40 - 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
2013-10-13 13:59:53 372
原创 39 - 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.solution:按位做乘法,然后相加得到最后结果,注意下标的
2013-10-13 11:09:37 345
原创 38 - 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]
2013-10-12 08:44:13 334
原创 37 - 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
2013-10-11 08:51:02 343
原创 36 - Combination Sum II
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 once in the combina
2013-10-09 08:14:46 411
原创 35 - 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
2013-10-09 07:01:53 531
原创 34 - Count and Say
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.21 is read off as
2013-10-08 16:12:51 785
原创 33 - 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
2013-10-08 07:28:33 592
原创 32 - 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 filled sudo
2013-10-07 09:54:09 612
原创 31 - 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-10-06 08:23:32 448
原创 30 - 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
2013-10-06 07:52:55 456
原创 29 - 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
2013-10-06 06:39:09 409
原创 28 - Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()",
2013-10-05 09:42:08 485
原创 27 - 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
2013-10-04 06:33:19 382
原创 26 - Substring with Concatenation of All Words
You are given a string, S, and a list of words, L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without
2013-09-27 07:41:17 385
原创 25 - Divide Two Integers
solution:此题的解法是不断左移divisor和dividend比较大小,先减掉最接近dividend的值,然后右移divisor再减次接近的值,直到dividend为0(表示整除)或者有余数。需要注意的地方是:1,保存除数与被除数的正负值,然后取绝对值进行循环;2,由于INT_MIN=-2147483648的存在,我们会发现abs(INT_MIN) == INT_MIN,
2013-09-25 07:23:55 372
原创 24 - Implement strStr()
Implement strStr().Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.solution: 方法一,最直观和最没效率的自然是一遍又一遍的遍历比对;方法二,在方法一的实现过程中,我们能遇
2013-09-24 10:15:50 436
原创 23 - 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.
2013-09-22 06:58:54 467
原创 22 - Remove Duplicates from Sorted Array
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 place with
2013-09-22 06:37:00 389
原创 21 - 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.
2013-09-21 15:27:38 431
原创 18 - generate parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.For example, given n = 3, a solution set is:"((()))", "(()())", "(())()", "()(())", "()()
2013-09-21 13:06:58 430
原创 20 - 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
2013-09-21 13:06:23 454
原创 19 - Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.solution: 第一种方法是打算先两两排序,再递归两两排序合并直到只有一个list。时间复杂度为nlog(k),但是曝出内存不够,所以该方法没通过。(后来发现方法一没通过是因为在m
2013-09-21 10:19:08 493
原创 17 - Merge Two Sorted Lists
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.solution : 非常简单,小心遇到NULL即可。
2013-09-21 06:45:56 481
原创 16 - Valid Parentheses
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 all va
2013-09-12 15:26:37 365
原创 15 - 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, t
2013-09-12 14:45:20 436
原创 14 - 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:
2013-09-12 11:39:43 516
原创 13 - 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.Input:Digi
2013-09-12 09:10:37 548
原创 12 - 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 have exa
2013-09-12 08:12:54 605
原创 11 - 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
2013-09-12 07:22:33 432
原创 10 - Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.solution: 从字符串的第一个字母开始往后比较直至出现不同。class Solution {public: bool compareAllChar(vector &strs, int in
2013-09-11 11:30:00 517
原创 9 - Integer to Roman
Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.solution:唯一想说的是罗马数字真蛋疼,对于各位的数字而言,123是一个处理方法,叠加而已,4逆反5,5678是一个情况,9是另一种情况。用switch处理比较方便。
2013-09-11 11:29:54 776
原创 8 - 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). F
2013-09-11 11:29:51 360
原创 7 - Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.Some hints:Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to string
2013-09-11 11:29:50 478
原创 6 - 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
2013-09-11 11:29:45 350
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人