Leetcode题解(十四)
摘要:39、Combination Sum题目题目要求找出和为target的数字组合,并且每个整数可以多次使用。仔细思考可以发现,这道题目可以采用递归的方法来完成,比如举的例子,target=7,一开始可以选中2,并且2> combinationSum(vector& candidates, int ta...
阅读全文
Leetcode题解(十三)
摘要:36、Valid Sudoku题目代码如下: 1 class Solution { 2 public: 3 bool isValidSudoku(vector > &board) { 4 // Note: The Solution object is instantiated...
阅读全文
Leetcode题解(十二)
摘要:33、Search in Rotated Sorted Array题目这道题目如果没有其他要求,直接遍历一遍就可以知道答案,但是,题目给出了是排序了数组,但是数组有可能经过了旋转得到,其解题思路仍然是二分查找,只不过在处理的时候需要添加一下逻辑处理;在做逻辑判断的时候,主要判断的是target可能位...
阅读全文
Leetcode题解(十一)
摘要:31、Next Permutation题目这道题目的意思是给定一个序列,找出其按照字典顺序的下一个顺序,如果给定顺序是字典中的最后一个顺序,其下一个顺序则为字典中的第一个顺序。比如:1,2,3,4,5-----注,后面的顺序去掉逗号123541243512453125341254313245通过上面...
阅读全文
Leetcode题解(十)
摘要:29、Divide Two Integers题目题目要求不用乘除和取模运算,实现两个整数相除;我的第一想法就是把除法变成减法来做,这也是最初除法的定义,其实现代码如下: 1 class Solution { 2 public: 3 int divide(int dividend, int d...
阅读全文
Leetcode题解(九)
摘要:28、Implement strStr()-------KMP算法(*)题目这道题目其实就是实现KMP算法,并且该算法也是比较经典的算法,需要很好的掌握:贴上几个介绍字符串匹配的算法说明链接http://www.cnblogs.com/Su-30MKK/archive/2012/09/17/2688...
阅读全文
Leetcode题解(八)
摘要:26、Remove Duplicates from Sorted Array题目直接上代码,方法很简单:class Solution {public: int removeDuplicates(vector& nums) { const int size = nums.size(...
阅读全文
Leetcode题解(七)
摘要:24、Swap Nodes in Pairs题目看到此题,第一想法是利用两个指针,分别将其所指向的节点的value交换。然后同时向后移动2个节点,代码如下: 1 struct ListNode { 2 int val; 3 ListNode *next; 4 Li...
阅读全文
Leetcode题解(六)
摘要:21、Merge Two Sorted Lists题目直接上代码:class Solution {public: ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) { ListNode *helper=new ListNode...
阅读全文
Leetcode题解(五)
摘要:17、Letter Combinations of a Phone Number题目针对输入的数字串,每一个数字都对应对个字符可以选择。因此可以直接采用递归的思想,依次遍历数字串的每一个数字,处理到当前数字时,余下的数字可以看出一个规模更小的子串来处理,这正符合递归的思想,将问题逐步化小。代码如下:...
阅读全文
Leetcode题解(四)
摘要:12/13、Integer to Roman/RomantoInteger题目罗马数字规则:符号IVXLCDM数字1510501005001000代码如下: 1 class Solution { 2 public: 3 string intToRoman(int num) { 4 ...
阅读全文
Leetcode题解(三)
摘要:8、String to Integer (atoi)题目这道题目关键是要考虑清楚各种输入用例。针对每一种情况,函数都能处理,也就是函数鲁棒性很高。代码如下: 1 class Solution { 2 public: 3 int myAtoi(string str) { 4 i...
阅读全文
Leetcode题解(二)
摘要:4、Median of Two Sorted Arrays(*)题目题目要求找到两个排序数组的中位数。中位数的定义:当n为奇数时,median = array[n/2];当n为偶数时,median = (array[n/2]+array[n/2+1])/2.暴力算法,两个数组归并排序,对合并的数组求...
阅读全文
Leetcode题解(一)
摘要:1、Two Sum题目此题第一解题思路,就是最常见的方法,两个指针嵌套遍历数组,依次判断当前指针所指向的值是否满足条件。代码如下; 1 class Solution { 2 public: 3 vector twoSum(vector& nums, int target) { 4 ...
阅读全文
Leetcode 172 Factorial Trailing Zeroes
摘要:1、题目要求Given an integern, return the number of trailing zeroes inn!.Note:Your solution should be in logarithmic time complexity.题目意思是求n的阶乘后面末尾0的个数,并且时间...
阅读全文
Leetcode 199 Binary Tree Right Side View
摘要:1.题目要求Given a binary tree, imagine yourself standing on therightside of it, return the values of the nodes you can see ordered from top to bottom.For ...
阅读全文
Leetcode 198 House Robber
摘要:1.题目要求You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopp...
阅读全文
Leetcode 15 3sum
摘要:1.题目要求Given an arraySofnintegers, are there elementsa,b,cinSsuch thata+b+c= 0? Find all unique triplets in the array which gives the sum of zero.Note:...
阅读全文
Leetcode 3 Longest Substring Without Repeating Characters
摘要:1.题目要求Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating lett...
阅读全文
Leetcode 4 Median of Two Sorted Arrays
摘要:1.题目要求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 shou...
阅读全文