Likou 1812, LCP17, 2011, 1876, 520, 709, 1704, 1844, 1805 problem solutions

1812. Determine the color of a square in a chess board

topic link

find rules

Clearly this question has strong patterns, so let's find patterns.
insert image description here
It is observed that the white grids of odd-numbered and even-numbered rows are always wrong by one, which adds a strong parity feature to this question.

class Solution {
    
    
public:
    bool squareIsWhite(string coordinates) {
    
    
        if((coordinates[0]+coordinates[1]-'a'-'0')%2==0) return true;
        return false;
    }
};

LCP 17. Quick Calculation Robot

topic link

simulation

Simulation according to the meaning of the question

class Solution {
    
    
public:
    int calculate(string s) {
    
    
        int x=1,y=0;
        for(int i=0;i<s.size();i++)
        {
    
    
            if(s[i]=='A') x=2*x+y;
            else y=2*y+x;
        }
        return x+y;
    }
};

2011. Variable value after an operation is performed

topic link

simulation

Simulation according to the meaning of the question

class Solution {
    
    
public:
    int finalValueAfterOperations(vector<string>& operations) {
    
    
        int x=0;
        for(auto t:operations){
    
    
            if(t=="--X"||t=="X--") x--;
            else x++;
        }
        return x;
    }
};

1876. Substrings of length three with different characters

topic link

enumerate substrings

Traverse the original string once, cut and remove all substrings of length three, and determine whether there are duplicate elements.

class Solution {
    
    
public:
    int countGoodSubstrings(string s) {
    
    
        if(s.size()<3) return 0;
        int ans=0;
        for(int i=0;i<s.size()-2;i++){
    
    
            string t=s.substr(i,3);
            if(t[0]!=t[1]&&t[1]!=t[2]&&t[0]!=t[2]) ans++;
        }
        return ans;
    }
};

520. Detect capital letters

topic link

Loop judgment

The first letter can be uppercase or lowercase, so we don't judge the first letter first. The case of the second letter to the last letter at the end of the string must be the same, so we use the case of the second letter to define the rules, and then compare the rules in turn. Another point is that it is illegal for the first letter after the lowercase letter to be all uppercase, so if the first letter is lowercase, our rules need to be changed, that is to say, the first letter after the lowercase Letters must be all lowercase.

class Solution {
    
    
public:
    bool detectCapitalUse(string word) {
    
    
        bool st=word[1]>='A'&&word[1]<='Z';
        if(word[0]>='a'&&word[0]<='z') st=false;
        for(int i=1;i<word.size();i++){
    
    
            if(st!=(word[i]>='A'&&word[i]<='Z')) return false;
        }
        return true;
    }
};

709. Convert to lowercase

topic link

Loop judgment

The loop judges the entire string, and if the current character is found to be uppercase, it will be converted to lowercase.
The difference between the upper and lower case letters of the ASCLL value is 32, and the characters can be directly added and subtracted. The essence is the addition and subtraction of the ASCLL code.
insert image description here

class Solution {
    
    
public:
    string toLowerCase(string s) {
    
    
        for(int i=0;i<s.size();i++){
    
    
            if(s[i]>='A'&&s[i]<='Z') s[i]+=32;
        }
        return s;
    }
};

1704. Determine if the two halves of a string are similar

topic link

Loop judgment

Loop the entire string, define two variables to store the number of vowels in the two halves

class Solution {
    
    
public:
    bool halvesAreAlike(string s) {
    
    
        int ans1=0,ans2=0;
        for(int i=0;i<s.size();i++){
    
    
            if(i<s.size()/2){
    
    
                if(s[i]=='a'||s[i]=='e'||s[i]=='o'||s[i]=='i'||s[i]=='u'||s[i]=='A'||s[i]=='O'||s[i]=='E'||s[i]=='I'||s[i]=='U') ans1++;
            }
            else{
    
    
                if(s[i]=='a'||s[i]=='e'||s[i]=='o'||s[i]=='i'||s[i]=='u'||s[i]=='A'||s[i]=='O'||s[i]=='E'||s[i]=='I'||s[i]=='U') ans2++;
            }
        }
        return ans1==ans2;
    }
};

1844. Replace all numbers with characters

topic link

Loop judgment

class Solution {
    
    
public:
    char shift(char c,int x)
    {
    
    
        return c+x;
    }
    
    string replaceDigits(string s) {
    
    
        for(int i=1;i<s.size();i+=2)
        {
    
    
            s[i]=shift(s[i-1],s[i]-'0');
        }
        return s;
    }
};

1805. Number of distinct integers in a string

topic link

Loop judgment

Loop through the entire string to find the segment where the number is in it. If the number has not been seen before, add it to the set (remove the leading 0 before adding) and add one to the number of answers

class Solution {
    
    
public:
    int numDifferentIntegers(string word) {
    
    
        set<string>cnt;
        int ans=0;
        int res=0;
        string ch;
        for(int i=0;i<word.size();i++)
        {
    
    
            if(word[i]>='0'&&word[i]<='9') 
            {
    
    
                ch+=word[i];
            }
            else 
            {
    
    
                int t=0;
                while(ch[t]=='0') t++;
                if(ch.size()&&cnt.find(ch.substr(t))==cnt.end())
                {
    
    
                    
                    cnt.insert(ch.substr(t));
                    ans++;
                }
                ch.clear();
            }
        }
        if(ch.size())
        {
    
    
            int t=0;
            while(ch[t]=='0') t++;
            if(cnt.find(ch.substr(t))==cnt.end()) ans++;
        }
        
        return ans;
    }
};

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324146523&siteId=291194637