
LeetCode题解
文章平均质量分 53
涛爸
勿在浮沙筑高台
展开
-
Reverse Words in a String--LeetCode
1.题目Reverse Words in a String Given an input string, reverse the string word by word. For example, Given s = “the sky is blue”, return “blue is sky the”. Update (2015-02-12): For C原创 2017-10-31 19:34:14 · 258 阅读 · 0 评论 -
String to Integer (atoi)--LeetCode
1.题目String to Integer (atoi)2.代码class Solution {public: int myAtoi(string str) { if (str.size() == 0) return 0; int sign = 1; int res = 0; int i = 0;原创 2017-11-29 19:43:49 · 226 阅读 · 0 评论 -
Palindrome Number--LeetCode
1.题目Palindrome Number2.代码class Solution {public: bool isPalindrome(int x) { if (x < 0) return false; int div = 1; while (x / div >= 10) div *= 10;原创 2017-11-30 10:23:37 · 194 阅读 · 0 评论 -
Container With Most Water--LeetCode
1.题目Container With Most Water2.代码class Solution {public: int maxArea(vector<int>& height) { int res = 0; int i = 0; int j = height.size() - 1; while (i < j) {原创 2017-11-30 15:20:57 · 198 阅读 · 0 评论 -
Integer to Roman--LeetCode
1.题目Integer to Roman2.代码class Solution {public: string intToRoman(int num) { string res = ""; char roman[] = {'M', 'D', 'C', 'L', 'X', 'V', 'I'}; int value[] = {1000, 500,原创 2017-11-30 16:58:05 · 302 阅读 · 0 评论 -
Roman to Integer--LeetCode
1.题目Roman to Integer2.代码class Solution {public: int romanToInt(string s) { int res = 0; unordered_map<char, int> m{{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500},原创 2017-11-30 17:00:50 · 254 阅读 · 0 评论 -
ZigZag Conversion--LeetCode
1.题目ZigZag Conversion2.代码class Solution {public: string convert(string s, int numRows) { if (numRows <= 1) return s; string res = ""; int size = 2 * numRows - 2原创 2017-11-29 19:38:27 · 216 阅读 · 0 评论 -
Combinations--LeetCode
1.题目Combinations Given two integers n and k, return all possible combinations of k numbers out of 1 … n. For example, If n = 4 and k = 2, a solution is: [ [2,4], [3,4], [2,3],原创 2017-11-29 19:31:52 · 225 阅读 · 0 评论 -
LeetCode-083. Remove Duplicates from Sorted List
1.题目Remove Duplicates from Sorted List 对有序链表去重,使每个元素只出现1次2.分析若链表长度小于2,直接结束 否则,遍历链表,当相邻元素相等时,删除后出现的重复元素3.代码class Solution {public: ListNode* deleteDuplicates(ListNode* head) { ...原创 2018-06-11 19:34:18 · 162 阅读 · 0 评论 -
Combination Sum II--LeetCode
1.题目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原创 2017-10-30 19:22:16 · 251 阅读 · 0 评论 -
Evaluate Reverse Polish Notation--LeetCode
1.题目Evaluate Reverse Polish Notation Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, *, /. Each operand may be an integer or another expressi原创 2017-10-31 20:21:09 · 284 阅读 · 0 评论 -
Pow(x, n)--LeetCode
1.题目Pow(x, n) Implement pow(x, n).2.题意实现pow(x, n)3.分析1)递归版 求x的n次方,for循环让x乘以自己n次,会因超时无法通过 用递归来折半计算,每次把n缩小一半,这样n最终会缩小到等于0 任何数的0次方都为1 这时再往回乘 如果n是偶数,直接把上次递归得到的值平方后返回 如果n是奇数,则还需要乘以x 注意对于n是负数的情况,可以原创 2017-10-15 15:19:37 · 413 阅读 · 0 评论 -
Binary Tree Inorder Traversal--LeetCode
1.题目Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes’ values. For example: Given binary tree [1,null,2,3], 1 \ 2 /原创 2017-10-15 16:29:48 · 239 阅读 · 0 评论 -
Binary Tree Preorder Traversal--LeetCode
1.题目Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3原创 2017-10-15 16:09:46 · 290 阅读 · 0 评论 -
Binary Tree Postorder Traversal--LeetCode
1.题目Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes’ values. For example: Given binary tree {1,#,2,3}, 1 \ 2 /原创 2017-10-15 16:55:42 · 238 阅读 · 0 评论 -
Max Points on a Line--LeetCode
1.题目Max Points on a Line Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.2.题意计算几何,给定一个点集合,求最大的共线点的个数3.分析找出相同点和相同斜率的点通过斜率来判断共线需要用到除法 用double表示的双精度小原创 2017-11-01 20:17:23 · 262 阅读 · 0 评论 -
LeetCode-082. Remove Duplicates from Sorted List II
1. 题目Remove Duplicates from Sorted List II 对有序链表去重,删掉所有的重复元素2. 分析若链表长度小于2,直接结束 否则,遍历链表,当相邻元素相等时,删除所有重复元素3. 代码class Solution {public: ListNode* deleteDuplicates(ListNode* head) {...原创 2018-06-11 19:38:22 · 202 阅读 · 0 评论 -
LeetCode-035. Search Insert Position
1. 题目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...原创 2018-06-20 20:08:04 · 224 阅读 · 0 评论 -
LeetCode-148. Sort List
1. 题目Sort List Sort a linked list in O(n log n) time using constant space complexity.2. 分析题目限定时间复杂度必须为O(nlogn) 只能用快速排序、归并排序、堆排序 根据单链表的特点,最适于用归并排序 可复用 Merge Two Sorted Lists中的代码注意第...原创 2018-06-16 12:56:53 · 302 阅读 · 0 评论 -
LeetCode-026. Remove Duplicates from Sorted Array
1.题目Remove Duplicates from Sorted Array 去除数组中的重复元素,返回新数组的长度 数组中的每个元素只允许出现1次 如[1,1,2,2,2,3],生成新数组为[1,2,3] 2.分析1: 数组已经有序,所以重复元素一定相邻 从[1,size-1]遍历数组,依次与新数组的最后1个元素比较即可 若数组长度&amp;amp;amp;amp;amp;amp;amp;amp;lt;=2,则返回...原创 2018-06-09 15:18:51 · 220 阅读 · 0 评论 -
LeetCode-080. Remove Duplicates from Sorted Array II
1.题目Remove Duplicates from Sorted Array II 在Remove Duplicates from Sorted Array 的基础上,允许每个元素重复出现2次 如[1,1,2,2,2,3],生成新数组为[1,1,2,2,3] 2.分析1:在[2, len)范围内遍历数组 将nums[i]与numsindex - 2作比较 若不...原创 2018-06-09 15:26:59 · 172 阅读 · 0 评论 -
LeetCode-001. Two Sum
1. 题目Two Sum 查找数组中和为特定值的两个数,返回其下标 假定每个输入都有一个解决方案,你不会使用同一元素两次2. 分析本题需要返回的是下标,而不是数字本身 所以不同于 S中是否存在两个其和刚好为x的元素 可以借助哈希表,保存数组中每个数的下标 遍历数组的同时在哈希表中查找即可3. 代码1)class Solution {publi...原创 2018-06-24 15:24:41 · 222 阅读 · 0 评论 -
LeetCode-018. 4Sum
1. 题目4Sum 查找数组中和为0的4个数 解决方案集不能含有重复项2. 分析1:传统k-sum问题,先排序,再左右夹逼 时间复杂度O(n^3),空间复杂度O(1) 2:借助unordered_multimap(允许重复key)缓存两数之和 时间复杂度O(n^2),空间复杂度O(n^2) 注意内层循环是for(int j = i + 1; j &amp;amp;amp;lt...原创 2018-06-29 20:30:50 · 221 阅读 · 0 评论 -
LeetCode-067. Add Binary
1. 题目Add Binary 给定两个二进制字符串,返回它们的总和 (也是二进制字符串) 输入字符串都是非空的,只包含字符1或0。2. 分析从尾到头遍历字符串 每次取出1个字符,转为数字,若无字符则按0处理 定义进位carry,初始值为0 将三者加起来,对2取余即为当前位的数字,对2取商即为进位的值 注意return时候,carry千万不要遗漏,如果c...原创 2018-06-30 14:21:53 · 427 阅读 · 0 评论 -
LeetCode-015. 3Sum
1. 题目3Sum 在数组中找到和为0的三个数,结果不重复2. 分析本题是Two Sum的扩展,brute force时间复杂度为O(n^3) 但这里使用哈希表的解法并不合适,因为结果数组中的元素可能出现重复 因此应该先排序,然后左右夹逼 用0减去[0,nums.size()-2)中的一个数得到一个sum 在剩余子数组中找到两数之和等于sum即可 将问题转...原创 2018-06-26 20:51:56 · 234 阅读 · 0 评论 -
LeetCode-016. 3Sum Closest
1. 题目3Sum Closest 求最接近target的三数之和 假定每个输入都只有一个解决方案2. 分析要保证三数和跟target差的绝对值最小 brute force时间复杂度为O(n^3) 优化的解法是先排序再左右夹逼 我们需要定义一个变量diff用来记录差的绝对值 先确定一个数,再滑动寻找另外两个数 每确定两个数,求出此三数之和 再算...原创 2018-06-28 20:16:57 · 264 阅读 · 0 评论 -
LeetCode-023. Merge k Sorted Lists
1. 题目Merge k Sorted Lists 合并k个升序链表2. 分析将Merge Two Sorted Lists从2个扩展到k个 1:最直接的思路就是两两合并,1,2先合并,合并好了再跟3/…/k合并 注意ListNode *p = lists[0];而非ListNode *p = &amp;amp;amp;amp;amp;amp;lists[0];i的下标从1开始p = mergeT...原创 2018-06-15 19:43:35 · 222 阅读 · 0 评论 -
LeetCode-283. Move Zeroes
1. 题目Move Zeroes 给定一个数组nums,写一个函数,将0移到数组尾,并保持非0元素的相对顺序。 要求: - 必须就地操作,不能复制数组; - 最小化操作总次数。2. 分析采用类似选择排序的思想 只是此时前移的不再是min_val而是03. 代码class Solution {public: void moveZer...原创 2018-06-08 19:31:26 · 208 阅读 · 0 评论 -
LeetCode-081. Search in Rotated Sorted Array II
1. 题目Search in Rotated Sorted Array II 在旋转有序数组中查找特定值 数组中有重复的数,查找特定值,若存在,返回true,若不存在,返回false 如[10,12,12,1,2,6,8,10] 10 返回true [10,12,12,1,2,6,8,10] 0 返回false2. 分析类似于 Search in Ro...原创 2018-06-23 16:07:16 · 245 阅读 · 0 评论 -
LeetCode-034. Search for a Range
1. 题目Search for a Range 给定升序数组nums,找到给定target的起始和结束位置 算法的运行时复杂度必须是O(log n) 若找不到target,返回[-1, -1]2. 分析属于变形的二分查找中的其中两种情况 即查找第一个等于或者大于value的元素 和最后一个等于或者小于value的元素 因此可在找出l后,在[l,last...原创 2018-06-20 20:19:50 · 220 阅读 · 0 评论 -
LeetCode-088. Merge Sorted Array
1. 题目Merge Sorted Array 合并两个有序数组 假设nums1有足够的空间(&amp;amp;amp;gt;=m+n)来保存nums2中的附加元素2. 分析采用归并排序的思想 1:两数组都有序,所有按顺序比较大小即可。 建立一个长度为m+n的total数组,逐个比较nums1、nums2数组中的元素, 先将较小元素放入total,再考虑两数组剩余元素, 最后再把to...原创 2018-06-13 20:09:44 · 272 阅读 · 0 评论 -
LeetCode-069. Sqrt(x)
1. 题目Sqrt(x) 计算并返回x的平方根,其中x保证为非负整数。 由于返回类型是整数,所以小数位数被截断,只返回结果的整数部分。2. 分析1:二分查找 取定左值和右值,每次砍掉不符合条件的一半,直到左值和右值相遇 2:也可采用牛顿迭代法3. 代码class Solution {public: int mySqrt(int x) { ...原创 2018-06-21 18:57:43 · 217 阅读 · 0 评论 -
LeetCode-147. Insertion Sort List
1. 题目Insertion Sort List Sort a linked list using insertion sort. 使用插入排序对链表排序。2. 分析插入排序的基本思想3. 代码 // Definition for singly-linked list. struct ListNode { int val; ...原创 2018-06-07 18:40:27 · 222 阅读 · 0 评论 -
LeetCode-021. Merge Two Sorted Lists
1. 题目Merge Two Sorted Lists2. 分析采用归并排序的思想3. 代码1)class Solution {public: ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) { if(l1 == NULL) return l2; i...原创 2018-06-14 18:40:01 · 453 阅读 · 0 评论 -
LeetCode-074. Search a 2D Matrix
1. 题目Search a 2D Matrix 编写一个搜索m*n矩阵中的值的有效算法,该矩阵具有以下性质: 每行中的整数从左到右排序。 每行的第一个整数大于前一行的最后一个整数。2. 分析1:给定矩阵是有序的,可以采用两次二分查找,先找出行,再找出列 2:将该矩阵看成是一个一维数组,数组值为 matrix[ i / n ][ i % n ]3. 代码1...原创 2018-06-22 20:08:56 · 274 阅读 · 0 评论 -
LeetCode-240. Search a 2D Matrix II
1. 题目Search a 2D Matrix II 编写一个搜索m*n矩阵中的值的有效算法,该矩阵具有以下性质: 每行中的整数从左到右排序。 每列中的整数从上到下排序。2. 分析以数组右上角的数为参考, 若target等于右上角数,则查找成功; 若target小于右上角数,则右上角数所在列不在查找范围; 若target大于右上角数,则右上角数所在...原创 2018-06-23 14:08:45 · 302 阅读 · 0 评论 -
LeetCode-033. Search in Rotated Sorted Array
1. 题目Search in Rotated Sorted Array 在旋转有序数组中查找特定值 数组中没有重复的数,查找特定值,若存在,返回其下标,若不存在,返回-1 如[10,12,1,2,6,8] 12 返回1 [10,12,1,2,6,8] 0 返回-1 2. 分析数组中没有重复的数 若nums[start]&amp;amp;amp;amp;lt;=nums[mid...原创 2018-06-23 15:28:11 · 213 阅读 · 0 评论 -
LeetCode-053. Maximum Subarray
1. 题目Maximum Subarray 给定一个数组,找出其最大连续子序列和 2. 分析动态规划,先找出局部最优解,再从局部最优解中找出全局最优解3. 代码class Solution {public: int maxSubArray(vector&amp;amp;amp;amp;amp;lt;int&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp; nums)原创 2018-07-05 19:51:33 · 223 阅读 · 0 评论 -
Divide Two Integers--LeetCode
1.题目Divide Two Integers Divide two integers without using multiplication, division and mod operator. If it is overflow, return MAX_INT.2.题意不使用乘法、除法和 mod 运算,进行两数的相除 如果溢出,则返回 MAX_INT3.分析数值处理问题,如Re原创 2017-10-14 12:47:05 · 253 阅读 · 0 评论 -
Reverse Nodes in k-Group--LeetCode
1.题目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. k is a positive integer and is less than or equal to the length of th原创 2017-09-29 19:42:55 · 201 阅读 · 0 评论