LeetCode(C++)
文章平均质量分 82
用C++实现LeetCode中的题目。
feliciafay
开发工程师
展开
-
LeetCode(50)Pow
题目如下:Implement pow(x, n).分析如下:看几个例子,pow(3,4) = pow(3,2)*pow(3,2) = (pow(3,1)*pow(3,1)) * (pow(3,1)*pow(3,1))pow(3,5) = pow(3,2)*pow(3,2)*pow(3,1) = (pow(3,1)*pow(3,1)) * (pow(3,原创 2014-02-06 08:43:28 · 1118 阅读 · 0 评论 -
LeetCode(49)Anagram
题目如下:Given an array of strings, return all groups of strings that are anagrams.Note: All inputs will be in lower-case.分析如下:之前遇到过类似的题目。处理的方法是,把每个词都sort一下,sort之后的结果作为key存入map中,比如ate在sort之后是a原创 2014-01-23 06:03:50 · 1755 阅读 · 0 评论 -
LeetCode(48)RotateImage
题目如下: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?分析如下:比较简单的一道题目。先沿着对角线(左上到右下)做对称变换,然后沿着竖直中心轴做对称变原创 2014-02-15 15:21:26 · 1775 阅读 · 0 评论 -
LeetCode(47) PermutaionsII
题目如下:分析如下:我的代码:参考资料:原创 2014-11-21 16:07:19 · 1951 阅读 · 0 评论 -
LeetCode(46) Permutations
题目如下:分析如下:我的代码:原创 2014-11-21 15:54:19 · 2315 阅读 · 0 评论 -
LeetCode(45) 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原创 2015-01-16 17:37:29 · 1791 阅读 · 0 评论 -
LeetCode(44) Wildcard Matching
题目如下:'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function pro原创 2015-01-04 15:26:39 · 2068 阅读 · 0 评论 -
LeetCode(43) 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.分析如下:以 1234 * 567 = 69978为原创 2015-01-06 07:09:48 · 764 阅读 · 0 评论 -
LeetCode(42) Trapping Rain Water
占位。。。原创 2015-03-30 13:47:36 · 758 阅读 · 0 评论 -
LeetCode(41) First Missing Positive
题目如下:A peak element is an element that is greater than its neighbors.Given an input array where num[i] ≠ num[i+1], find a peak element and return its index.The array may contain multiple peaks,原创 2014-12-31 08:08:19 · 3606 阅读 · 0 评论 -
LeetCode(40) Combination Sum II
题目如下:My Submissions Question Solution 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原创 2014-12-18 16:27:48 · 1098 阅读 · 0 评论 -
LeetCode(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 C unlimited n原创 2014-12-18 14:28:00 · 973 阅读 · 0 评论 -
LeetCode(38) 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原创 2014-12-23 15:19:41 · 932 阅读 · 0 评论 -
LeetCode(37) 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原创 2015-03-23 07:55:02 · 1778 阅读 · 0 评论 -
LeetCode(36) 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 f原创 2015-03-28 02:31:33 · 610 阅读 · 0 评论 -
Leetcode(35) Search Insert Position
代码如下class Solution {public: int searchInsert(int A[], int n, int target) { int i = 0; int j = n - 1; int mid = 0; while (i <= j) { mid = i + ((j - i)原创 2014-10-07 05:27:11 · 822 阅读 · 0 评论 -
LeetCode(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 is not fou原创 2014-03-07 04:58:55 · 1852 阅读 · 0 评论 -
LeetCode(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 the array retu原创 2014-02-05 03:36:22 · 1108 阅读 · 0 评论 -
LeetCode(32)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 "()",原创 2014-03-07 08:31:54 · 2639 阅读 · 0 评论 -
LeetCode(31) Next Permutation
先占位to be continued原创 2015-03-28 02:20:55 · 604 阅读 · 0 评论 -
LeetCode(30) Substring with Concatenation of All Words (java)
先占位to be continued原创 2015-03-28 02:19:36 · 1879 阅读 · 0 评论 -
LeetCode(29)Divide Two Integers
题目内容Divide two integers without using multiplication, division and mod operator.题目分析最容易的想到的办法,是把除法转化为减法,就像把乘法转化为加法一样,提交后发现,这个做法超时了,比如遇到2147483647/3这种时候,基本只能超时。所以应该要使用一个特别的方法来提高效率。我想应该是位运算了。但是不原创 2014-01-03 07:24:18 · 4213 阅读 · 1 评论 -
LeetCode(28)Implement Strstr()
题目如下Implement strStr() Returns a pointer to the first occurrence of needle in haystack, or null if needle is not part of haystack.题目分析就是把实现strstr()一遍,如果不使用KMP,那么就老老实实地逐个比较。class Solution {原创 2014-01-02 04:34:45 · 1962 阅读 · 0 评论 -
LeetCode(27)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-12-31 05:19:12 · 1163 阅读 · 0 评论 -
LeetCode(26)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原创 2013-12-31 08:37:38 · 1523 阅读 · 0 评论 -
LeetCode(25)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 i原创 2013-12-31 02:29:55 · 1208 阅读 · 0 评论 -
LeetCode(24)Swap Nodes in Pairs
题目如下:Given>/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public:原创 2013-12-26 04:10:42 · 1976 阅读 · 0 评论 -
LeetCode(23)Merge K Sorted Lists
题目如下:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.分析:题目意思明确。可以仿照归并排序的思路来进行。需要考虑一些情况,比如lists[i]如果在作为函数的输入,可能会在一开始就是NULL。处理到中途,可能lists[i]对应原创 2013-12-21 16:27:23 · 4662 阅读 · 0 评论 -
LeetCode(22)GenerateParentheses
题目如下: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-12-19 14:32:10 · 2363 阅读 · 0 评论 -
LeetCode(21)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.分析如下:比较简单,试了一下,10分钟在leetcode网页上敲完且检查了一下bug,提交发原创 2014-01-25 15:53:38 · 1223 阅读 · 0 评论 -
LeetCode(20) Valid Parentheses
题目如下Given a string containing justthe characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all v原创 2013-12-19 06:04:40 · 3611 阅读 · 0 评论 -
LeetCode(19) 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-18 14:30:51 · 2166 阅读 · 1 评论 -
LeetCode(17) Letter Combinations of a Phone Number
题目如下,输入一串数字,根据电话键盘布局输出此数字可能代表的字母组合。Given a digit string, return all possible letter combinations that the number could represent.Input:Digit string "23"Output: ["ad", "ae", "af", "bd", "be", "bf原创 2013-12-17 09:06:14 · 1167 阅读 · 0 评论 -
LeetCode(14) Longest Common Prefix
题目:Write a function to find the longest common prefix string amongst an array of strings.我的做法是,首先,先扫描一遍字符串数组,找到最短的字符串,时间复杂度为O(N)。然后把这个字符串的第1个字符作为标杆,在循环中依次检查数组中剩下的元素,看看是否第1个字符相等。如果循环完了数组的所有字符串,都发现它原创 2013-12-13 03:35:40 · 1063 阅读 · 0 评论 -
LeetCode(13) RomanToInteger
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.本题是把一个罗马数字转化为阿拉伯数字,和前道题目正好相反。我按照千位、百位、十位、个位的顺序依次处理罗马数字。在前一到题目的解答中,可以找到一个办法去简化代码,本题好像不怎么方原创 2013-12-11 10:42:45 · 3255 阅读 · 0 评论 -
LeetCode(12) IntegerToRoman
题目:Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.比较基础的题目,没有用到什么数据结构和算法。使用了维基百科了解了罗马数字的书写规则。一开始按照个十百千的位数老老实实地写转换程序,写出来一看,啰嗦得囧。//啰嗦版原创 2013-12-10 08:36:43 · 2528 阅读 · 0 评论 -
LeetCode(11) ContainerWithMostWater
题目如下: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). Fi原创 2013-12-09 08:23:48 · 4029 阅读 · 1 评论 -
LeetCode(10) Regular Expression Matching
题目如下:'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:b原创 2015-01-10 03:37:37 · 1080 阅读 · 0 评论 -
LeetCode(9) Palindrome Number
题目如下: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原创 2015-01-08 12:56:34 · 1409 阅读 · 0 评论 -
LeetCode(8) String To Integer(atoi)
题目如下:String to Integer (atoi) Total Accepted: 29609 Total Submissions: 214853 My Submissions Question Solution Implement atoi to convert a string to an integer.Hint: Carefully consider all pos原创 2015-01-08 12:52:36 · 2484 阅读 · 1 评论