随笔分类 - C/C++
摘要:题目地址:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/ 解题思路:遍历一遍,累计求和 class Solution { public: vector<int> smalle
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/complement-of-base-10-integer/ 解题思路:按位取反计算。注意不要想当然使用~,~10等于-11,而不是5。数据是以补码存储的,在计算机中,10的原码和补码为00...1010。取反后,补码变成1
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/long-pressed-name/ 解题思路:暴力,判断返回false的几种情况。 class Solution { public: bool isLongPressedName(string name, string t
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/divide-two-integers/ 解题思路:按照计算机实现除法运算来进行求解。这题需要注意的是:只能存储 32 位有符号整数,最小整型数值的相反数会越界,所以需要使用longlong类型。 注意:如果用VS编译调试,
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/implement-strstr/ 解题思路:KMP算法。该题注意的是string类的size()方法返回的是无符号的值。 简单说一下KMP算法。KMP算法就是在暴力匹配的算法中引入next数组,匹配的时候如果不匹配只更新模
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/remove-element/ 解题思路:设置新的下标 class Solution { public: int removeElement(vector<int>& nums, int val) { if (nums.si
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array/ 解题思路:设计标记。 class Solution { public: int removeDuplicates(vector<int>& nums)
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/4sum/ 解题思路:和之前三数求和类似,只不过多了层循环。 class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int targe
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/letter-combinations-of-a-phone-number/ 解题思路:暴力求解,很明显循环的个数根据digits的个数有关,所以我们可以采用递归来进行这种动态的多重循环。 class Solution { 
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/3sum-closest/ 解题思路:和上一题三数之和类似(https://www.cnblogs.com/cc-xiao5/p/13392624.html),唯一的区别就是通过差的绝对值判断最接近 class Soluti
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/3sum/ 解题思路:笔者采用map判断重复,结果超时,原因是map是对数级时间复杂度,放在循环中就超时了。最佳思路是采用双指针加排序。 /**超时代码 * 原因:循环中使用map */ class Solution { p
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/longest-common-prefix 解题思路:暴力 class Solution { private: string getAns(string a,string b) { int len = min(a.size(
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/roman-to-integer/ 解题思路: 同理,写的比较丑。 class Solution { public: int romanToInt(string s) { map<char,int > mp; map<cha
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/integer-to-roman/ 解题思路: 打一个模板然后分情况讨论。写的比较丑。 map<int, string> mp; map<int, string> ::iterator it; class Solution 
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/container-with-most-water/ 解题思路: 很明显这类题目不能用暴力,很容易超时;可以采用贪心的想法:最大的体积肯定要么很宽,要么很高。所以从两边开始计算,然后较小的边界舍去,边界向左或者向右移动一个,
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/regular-expression-matching/ 解题思路:首先很明显字符串的匹配适合动态规划,所以建立dp[i][j]表示s的前i个字符与p中的前j个字符是否能够匹配。然后就是判断各种情况。笔者在写的时候情况考虑不
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/palindrome-number/ 解题思路: 栈操作。 class Solution { public: int getLength(int x) { int len = 0; if (x == 0) return 1;
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/string-to-integer-atoi/ 解题思路:注意所有情况。官方提供了类似状态机的一种算法——自动机(https://leetcode-cn.com/problems/string-to-integer-atoi
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/reverse-integer/ 解题思路:注意题目给的“32 位的有符号整数”信息。我们在反转计算的过程中,可能会溢出。题目的范围是[-2^31,2^31]。 int reverse(int x) { long retur
        阅读全文
                
摘要:题目地址:https://leetcode-cn.com/problems/zigzag-conversion/ 解题思路:简单暴力求解 char * convert(char * s, int numRows) { char map[1005][1005]; memset(map, 0, size
        阅读全文
                
                    
                
浙公网安备 33010602011771号