栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

c++入门99题41-50

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

c++入门99题41-50

解题报告 1.力扣剑指 Offer II 018. 有效的回文 原题链接

剑指 Offer II 018. 有效的回文

源码剖析
class Solution {
public:
    bool isPalindrome(string s) {
        for(int i = 0;i
            if(s[i]<='z'&&s[i]>='a'){
                s[i] = s[i] - 'a'+'A';
            }
        }
        int l = 0,r = s.size()-1;
        while(l
            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;
    }
};

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

2.力扣125 原题链接

125. 验证回文串

源码剖析
class Solution {
public:
    bool isPalindrome(string s) {
        for(int i = 0;i
            if(s[i]<='z'&&s[i]>='a'){
                s[i] = s[i] - 'a'+'A';
            }
        }
        int l = 0,r = s.size()-1;
        while(l
            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;
    }
};

同上。

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
            if(s[i]=='A'){
                x = cal(x,y);
            }else y = cal(y,x);
        }
        return x+y;
    }
};

简单遍历。
法二:不论是对于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< 
4.力扣2011 
原题链接 

2011. 执行操作后的变量值

源码剖析
class Solution {
public:
    int finalValueAfterOperations(vector& operations) {
        int x = 0;
        for(int i = 0;i
            if(operations[i][1]=='+'){
                x++;
            }else x--;
        }
        return x;
    }
};

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

解题报告剑指 Offer 05. 替换空格 5.力扣 原题链接

剑指 Offer 05. 替换空格

源码剖析
class Solution {
public:
    string replaceSpace(string s) {
        string ans="";
        for(int i = 0;i
            if(s[i]==32){
                ans+="%20";
            }else ans+=s[i];
        }
        return ans;
    }
};

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

6.力扣217 原题链接

217. 存在重复元素

源码剖析
class Solution {
public:
    bool containsDuplicate(vector& nums) {
        sort(nums.begin(),nums.end());
        for(int i = 1;i
            if(nums[i]==nums[i-1]){
                return true;
            }
        }
        return false;
    }
};

排序,比较相邻,结束。

7.力扣912 原题链接

912. 排序数组

源码剖析
class Solution {
public:
    vector sortArray(vector& nums) {
        sort(nums.begin(),nums.end());
        return nums;
    }
};

直接调用库函数。

8.力扣1480 原题链接

1480. 一维数组的动态和

源码剖析
class Solution {
public:
    vector runningSum(vector& nums) {
        for(int i = 1;i
            nums[i]+=nums[i-1];
        }
        return nums;
    }
};

使用一个循环计算。

解题报告 9.力扣344 原题链接

344. 反转字符串

源码剖析
class Solution {
public:
    void reverseString(vector& s) {
        char tmp;
        int l = 0,r = s.size()-1;
        while(l
            tmp = s[r];
            s[r] = s[l];
            s[l] = tmp ;
            l++;
            r--;
        }
    }
};

双指针。

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

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

源码剖析
class Solution {
public:
    vector twoSum(vector& numbers, int target) {
        int l = 0 , r = numbers.size()-1;
        while(l
            if(numbers[l]+numbers[r]==target) return {l,r};
            if(numbers[l]+numbers[r]>target){
                r--;
                continue;
            }else if(numbers[l]+numbers[r]
                l++;
                continue;
            }
        }
        return {l,r};
    }
};

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1030289.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号