Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- string maxValue(string n, int x) {
- string ans;
- if(n[0] == '-'){
- ans += '-';
- bool added = false;
- for(char ch: n){
- if(ch == '-') continue;
- if(ch-'0' > x && !added){
- ans += ('0'+x);
- added = true;
- }
- ans += ch;
- }
- if(!added)
- ans += ('0'+x);
- }
- else{
- bool added = false;
- for(char ch: n){
- if(ch-'0' < x && !added){
- ans += ('0'+x);
- added = true;
- }
- ans += ch;
- }
- if(!added)
- ans += ('0'+x);
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment