随笔分类 - LeetCode
摘要:"46. Permutations" "47. Permutations II" 和 "Combination Sum II" 是类似的,只用递归重复数字的第一个数字,后面都是重复的解。
阅读全文
摘要:"39. Combination Sum" 这道题就是简单的回溯,需要注意的是为了避免重复结果以及降低时间复杂度,可以先对cans数组进行排序,然后每次回溯的时候从chs数组的最后一个元素在cans数组中的位置开始进行,这样就保证了解序列是升序,从而避免了重复回溯,因此也减少了时间。
阅读全文
摘要:16. 3Sum Closest 和之前3Sum那道题思路一样,刚开始想的是不用每次计算误差,只需要在小于目标和大于目标的转折点出计算两次然后临界的时候计算一次即可,可是实际上和每次都计算误差速度一样的。。。 class Solution { public: int threeSumClosest(
阅读全文
摘要:8. String to Integer (atoi)class Solution {public: int myAtoi(string str) { int Max = (1<<31)-1, Min = -1<<31; int i ...
阅读全文
摘要:15. 3Sumclass Solution {public: vector> threeSum(vector& nums) { sort(nums.begin(),nums.end()); vector> res; i...
阅读全文
摘要:11. Container With Most Water设置两个指针i j ,分别指向首尾两块板,然后向中间移动,那么宽度变小的情况下为了增大面积只有让高度变大,所以每次移动所指板较低的指针,然后计算面积,重复以上过程。int max(int a,int b){ ...
阅读全文
摘要:14. Longest Common Prefixchar s[100000];int min(int a,int b){ return a < b ? a : b;}char* longestCommonPrefix(char** strs, int strs...
阅读全文
摘要:12. Integer to Romanchar s[100];char* intToRoman(int num) { char rs[] = "MDCLXVI"; int ts[] = { 1000,500,100,50,10,5,1 }; int...
阅读全文
摘要:13. Roman to Integerint romanToInt(char* s) { int dic[26]; dic['I'-'A'] = 1; dic['V'-'A'] = 5; dic['X'-'A'] = 10; dic['...
阅读全文
摘要:Implement regular expression matching with support for '.' and '*'.'.' Matches any single character.'*' Matches zero or more of the pr...
阅读全文
摘要:Determine whether an integer is a palindrome. Do this without extra space.click to show spoilers.Some hints:Could negative integers be...
阅读全文
摘要:Given a 32-bit signed integer, reverse digits of an integer.Example 1:Input: 123Output: 321Example 2:Input: -123Output: -321Example 3...
阅读全文
摘要:The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern ...
阅读全文
摘要:Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.Example:Input: "bab...
阅读全文
摘要:There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The overall run tim...
阅读全文
摘要:Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", ...
阅读全文
摘要:You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of th...
阅读全文
摘要:Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each inpu...
阅读全文