摘要: 题目地址:https://leetcode-cn.com/problems/3sum-closest/ 解题思路:和上一题三数之和类似(https://www.cnblogs.com/cc-xiao5/p/13392624.html),唯一的区别就是通过差的绝对值判断最接近 class Soluti 阅读全文
posted @ 2020-07-29 19:25 CCxiao5 阅读(128) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/3sum/ 解题思路:笔者采用map判断重复,结果超时,原因是map是对数级时间复杂度,放在循环中就超时了。最佳思路是采用双指针加排序。 /**超时代码 * 原因:循环中使用map */ class Solution { p 阅读全文
posted @ 2020-07-28 18:00 CCxiao5 阅读(126) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/longest-common-prefix 解题思路:暴力 class Solution { private: string getAns(string a,string b) { int len = min(a.size( 阅读全文
posted @ 2020-07-28 16:18 CCxiao5 阅读(84) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/roman-to-integer/ 解题思路: 同理,写的比较丑。 class Solution { public: int romanToInt(string s) { map<char,int > mp; map<cha 阅读全文
posted @ 2020-07-24 16:36 CCxiao5 阅读(151) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/integer-to-roman/ 解题思路: 打一个模板然后分情况讨论。写的比较丑。 map<int, string> mp; map<int, string> ::iterator it; class Solution 阅读全文
posted @ 2020-07-24 16:13 CCxiao5 阅读(114) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 解题思路: 很明显这类题目不能用暴力,很容易超时;可以采用贪心的想法:最大的体积肯定要么很宽,要么很高。所以从两边开始计算,然后较小的边界舍去,边界向左或者向右移动一个, 阅读全文
posted @ 2020-07-20 19:34 CCxiao5 阅读(96) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/regular-expression-matching/ 解题思路:首先很明显字符串的匹配适合动态规划,所以建立dp[i][j]表示s的前i个字符与p中的前j个字符是否能够匹配。然后就是判断各种情况。笔者在写的时候情况考虑不 阅读全文
posted @ 2020-07-20 18:50 CCxiao5 阅读(205) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/palindrome-number/ 解题思路: 栈操作。 class Solution { public: int getLength(int x) { int len = 0; if (x == 0) return 1; 阅读全文
posted @ 2020-07-09 16:52 CCxiao5 阅读(119) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/string-to-integer-atoi/ 解题思路:注意所有情况。官方提供了类似状态机的一种算法——自动机(https://leetcode-cn.com/problems/string-to-integer-atoi 阅读全文
posted @ 2020-07-09 16:04 CCxiao5 阅读(162) 评论(0) 推荐(0) 编辑
摘要: 题目地址:https://leetcode-cn.com/problems/reverse-integer/ 解题思路:注意题目给的“32 位的有符号整数”信息。我们在反转计算的过程中,可能会溢出。题目的范围是[-2^31,2^31]。 int reverse(int x) { long retur 阅读全文
posted @ 2020-07-08 18:02 CCxiao5 阅读(110) 评论(0) 推荐(0) 编辑