c++编程问题求正确解答

问题遇到的现象和发生背景

img

img

用代码块功能插入代码,请勿粘贴截图

T1

#include<iostream>
using namespace std;
bool cmp(char x){
    if(x>='A' && x<='Z') return true;
    if(x>='a' && x<='z') return true;
    return false;
}
int main()
{
    string s;
    getline(cin,s);
    int head=0,tail=s.size(),flag=0;

    while (head<=tail){
        if(flag==0 && cmp(s[head])) flag = 1;
        else if(flag==1 && cmp(s[tail]))
        {
            flag = 0;
            char temp = s[head];
            s[head] = s[tail];
            s[tail] = temp;
        }
        flag == 0 ? head++ : tail--;
    }

    cout << s << endl;
    return 0;
}

T2 (更优化,只有长度大于1才显示前方数字)

#include<iostream>
using namespace std;
string ZIP(string s){
    string result = "";
    long num = 1,length = s.size();
    char flag = s[0];

    for(int z=1;z<length;z++){
        if(flag==s[z]) num++;
        else
        {
            result += flag;
            result += to_string(num);
            flag = s[z];
            num = 1;
        }
    }
    result += flag;
    result += to_string(num);
    return result.size() < s.size() ? result : s;
}
//  aaaaaba -> a5b1a1
int main()
{
    string s;
    cin >> s;
    cout << ZIP(s);
    return 0;
}

第一题:

class Solution {
public:
    string reverseOnlyLetters(string S) {
        int i=0,j=S.size()-1;
        while(i<=j){
            if((S[i]<'A'||S[i]>'Z')&&(S[i]<'a' || S[i]>'z')){
                i++;
                continue;
            }
            if((S[j]<'A'||S[j]>'Z')&&(S[j]<'a' || S[j]>'z')){
                j--;
                continue;
            }
            char tmp = S[i];
            S[i] = S[j];
            S[j] = tmp;
            i++;
            j--;
        }
        return S;
    }
};

第二题:

#include <iostream>
#include <string>
using namespace std;

//效果:84.03%   76.65%
string compress(string str)
{
    if(str.empty())return str;
    int count = '1';
    string ans;
    for(int i = 0; i < str.length(); i += count)
    {
        count = 1;
        int j = i+1;
        while(str[j] == str[i])
        {
            count++;
            j++;
        }
        ans += str[i] + to_string(count);
    }
    return ans.length() < str.length() ? ans : str;
}

int main()
{
    cout << compress("aaaaabbbcdee");
    return 0;
}

题一:

#include <iostream> 
#include <string>
using namespace std;
class Solution {
public:
    bool IsLetter(char tmp){
        return (tmp >= 'a' && tmp <= 'z') || (tmp >= 'A' && tmp <= 'Z');
    }
    string reverseOnlyLetters(string S) {
        int left = 0, right = S.size() - 1;
        for (; left < right; left++, right--){
            while(IsLetter(S[left]) == 0){
                if(left == right){
                    return S;
                }
                left++;
            }
            while(IsLetter(S[right]) == 0){
                right--;
            }
            swap(S[left], S[right]);
        }
        return S;
    }
};
int main(void){
    Solution s;
    cout<<s.reverseOnlyLetters("a-bC-dEf-ghlj");
}

img

题二:

#include <iostream> 
#include <string>
using namespace std;
class Zipper {
public:
    string zipString(string iniString) {
        // write code here
        int length = iniString.size();
        string result = "";
        
        char index = iniString[0]; 
        int number = 0;
        for(int i = 0; i < length; i++)
        {
            if(index == iniString[i])
            {
                number++;
            }
            else
            {
                //当重复字符改变时,才会插入字符
                result += index;
                result += to_string(number);//number + '0';
                if(i + 1 < length)
                {
                    index = iniString[i];
                }
                else
                {
                    break;
                }
                number = 1;
            }
        }
 
        //还需在函数末尾更新字符串,因为最后一组重复字符还未放入压缩字符串中
        result += index;
        result += std::to_string(number);
        
        if(result.size() < length)
        {
            return result;
        }
        else
        {
            return iniString;
        }
    }
};
int main(void){
    Zipper s;
    cout<<s.zipString("aabcccccaaa");
}

img

有帮助的话采纳一下哦!