当前位置:网站首页>1844. replace all numbers with characters

1844. replace all numbers with characters

2022-06-24 08:52:00 Drag is me

leetcode Force button to brush questions and punch in

1844. Replace all numbers with characters
describe : I'll give you a subscript from 0 Starting string s , its even numbers Small letters at the subscript , Odd number The subscript is a number .

Define a function shift(c, x) , among c Is a character and x It's a number , Function returns to the alphabet c Next x Characters .

For example ,shift(‘a’, 5) = ‘f’ and shift(‘x’, 0) = ‘x’ .
For each Odd number Subscript i , You need to put the numbers s[i] use shift(s[i-1], s[i]) Replace .

Please replace all the numbers later , The string s return . subject Guarantee shift(s[i-1], s[i]) Not more than ‘z’ .

Their thinking

1、 The topic looks complicated , In fact, the water forced .

Source code ##

class Solution {
    
public:
    string replaceDigits(string s) {
    
        for (int i = 0; i < s.size(); ++i) {
    
            if (i % 2 == 1) {
    
                s[i] = s[i - 1] + s[i] - '0';
            }
        }
        return s;
    }
};
原网站

版权声明
本文为[Drag is me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240627022929.html