• c++入门99题41-50


    解题报告

    1.力扣剑指 Offer II 018. 有效的回文

    原题链接

    剑指 Offer II 018. 有效的回文

    源码剖析

    class Solution {
    public:
        bool isPalindrome(string s) {
            for(int i = 0;i<s.size();++i){
                if(s[i]<='z'&&s[i]>='a'){
                    s[i] = s[i] - 'a'+'A';
                }
            }
            int l = 0,r = s.size()-1;
            while(l<r){
                if(!(s[l]>='A'&&s[l]<='Z'||s[l]<='9'&&s[l]>='0')){
                    l++;
                    continue;
                }
                if(!(s[r]>='A'&&s[r]<='Z'||s[r]<='9'&&s[r]>='0')){
                    r--;
                    continue;
                }
                if(s[l]!=s[r]){
                    return false;
                }
                l++;
                r--;
            }
            return true;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    对字符串进行预处理后直接使用双指针遍历。

    2.力扣125

    原题链接

    125. 验证回文串

    源码剖析

    class Solution {
    public:
        bool isPalindrome(string s) {
            for(int i = 0;i<s.size();++i){
                if(s[i]<='z'&&s[i]>='a'){
                    s[i] = s[i] - 'a'+'A';
                }
            }
            int l = 0,r = s.size()-1;
            while(l<r){
                if(!(s[l]>='A'&&s[l]<='Z'||s[l]<='9'&&s[l]>='0')){
                    l++;
                    continue;
                }
                if(!(s[r]>='A'&&s[r]<='Z'||s[r]<='9'&&s[r]>='0')){
                    r--;
                    continue;
                }
                if(s[l]!=s[r]){
                    return false;
                }
                l++;
                r--;
            }
            return true;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    同上。

    3.力扣LCP 17

    LCP 17. 速算机器人

    源码剖析

    class Solution {
    public:
        int cal(int x,int y){
            return 2*x+y;
        }
        int calculate(string s) {
            int x = 1,y = 0;
            for(int i = 0;i<s.size();++i){
                if(s[i]=='A'){
                    x = cal(x,y);
                }else y = cal(y,x);
            }
            return x+y;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    简单遍历。
    法二:不论是对于x进行操作还是对于y操作,(这里假设对x进行操作 x+y(对x进行操作)->2x+y+y = 2(x+y) 对y操作同理,即不论对x还是y进行操作都会使得 x+y 的值翻倍,因此不需要对其进行遍历。

    class Solution {
    public:
        int calculate(string s) {
            if(s.size()==0) return 1;
            return 1<<s.size();
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    4.力扣2011

    原题链接

    2011. 执行操作后的变量值

    源码剖析

    class Solution {
    public:
        int finalValueAfterOperations(vector<string>& operations) {
            int x = 0;
            for(int i = 0;i<operations.size();++i ){
                if(operations[i][1]=='+'){
                    x++;
                }else x--;
            }
            return x;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    只需检测每一行的第二个位置是什么符号即可。

    解题报告剑指 Offer 05. 替换空格

    5.力扣

    原题链接

    剑指 Offer 05. 替换空格

    源码剖析

    class Solution {
    public:
        string replaceSpace(string s) {
            string ans="";
            for(int i = 0;i<s.size();++i){
                if(s[i]==32){
                    ans+="%20";
                }else ans+=s[i];
            }
            return ans;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    遍历原字符串,然后在空格的时候填入 %20 即可。

    6.力扣217

    原题链接

    217. 存在重复元素

    源码剖析

    class Solution {
    public:
        bool containsDuplicate(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            for(int i = 1;i<nums.size();++i){
                if(nums[i]==nums[i-1]){
                    return true;
                }
            }
            return false;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    排序,比较相邻,结束。

    7.力扣912

    原题链接

    912. 排序数组

    源码剖析

    class Solution {
    public:
        vector<int> sortArray(vector<int>& nums) {
            sort(nums.begin(),nums.end());
            return nums;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    直接调用库函数。

    8.力扣1480

    原题链接

    1480. 一维数组的动态和

    源码剖析

    class Solution {
    public:
        vector<int> runningSum(vector<int>& nums) {
            for(int i = 1;i<nums.size();++i){
                nums[i]+=nums[i-1];
            }
            return nums;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    使用一个循环计算。

    解题报告

    9.力扣344

    原题链接

    344. 反转字符串

    源码剖析

    class Solution {
    public:
        void reverseString(vector<char>& s) {
            char tmp;
            int l = 0,r = s.size()-1;
            while(l<r){
                tmp = s[r];
                s[r] = s[l];
                s[l] = tmp ;
                l++;
                r--;
            }
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    双指针。

    10.力扣剑指 Offer II 006. 排序数组中两个数字之和

    原题链接

    剑指 Offer II 006. 排序数组中两个数字之和

    源码剖析

    class Solution {
    public:
        vector<int> twoSum(vector<int>& numbers, int target) {
            int l = 0 , r = numbers.size()-1;
            while(l<r){
                if(numbers[l]+numbers[r]==target) return {l,r};
                if(numbers[l]+numbers[r]>target){
                    r--;
                    continue;
                }else if(numbers[l]+numbers[r]<target){
                    l++;
                    continue;
                }
            }
            return {l,r};
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    因为只有一组解,所以可以直接使用双指针进行遍历。

  • 相关阅读:
    论文笔记:TMN: Trajectory Matching Networks for PredictingSimilarity
    touchGFX综合学习十四、基于cubeMX、正点原子H750开发版、RGB4.3寸屏移植touchGFX完整教程+工程(二)
    MySQL锁 浅述
    git实战-多分支开发-2022新项目
    The Best of Many Worlds_ Dual Mirror Descent for Online Allocation Problems
    【C语言】字符函数和字符串函数(含模拟)
    非堵塞I/O
    游戏架构设计——高性能并行编程
    定制SQLmap和WAF绕过
    细聊C# AsyncLocal如何在异步间进行数据流转
  • 原文地址:https://blog.csdn.net/smallwind256/article/details/126032296