You are given an integer array nums containing positive integers. We define a function encrypt such that encrypt(x) replaces every digit in
x with the largest digit in x. For example, encrypt(523) = 555 and
encrypt(213) = 333.
For each number, find its largest digit and replace every digit with this value to form the encrypted number. The sum of all such encrypted numbers is the answer.
classSolution {
public:int sumOfEncryptedInt(vector<int>& nums) {
int ans =0;
for (int x : nums) {
int mx =0, y = x, len =0;
while (y) { mx = max(mx, y %10); y /=10; len++; }
int enc =0;
for (int i =0; i < len; ++i) enc = enc *10+ mx;
ans += enc;
}
return ans;
}
};
classSolution {
publicintsumOfEncryptedInt(int[] nums) {
int ans = 0;
for (int x : nums) {
int mx = 0, y = x, len = 0;
while (y > 0) {
mx = Math.max(mx, y % 10);
y /= 10;
len++;
}
int enc = 0;
for (int i = 0; i < len; ++i) enc = enc * 10 + mx;
ans += enc;
}
return ans;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
classSolution {
funsumOfEncryptedInt(nums: IntArray): Int {
var ans = 0for (x in nums) {
var mx = 0var y = x
var len = 0while (y > 0) {
mx = maxOf(mx, y % 10)
y /=10 len++ }
var enc = 0 repeat(len) { enc = enc * 10 + mx }
ans += enc
}
return ans
}
}
1
2
3
4
5
6
7
8
9
classSolution:
defsumOfEncryptedInt(self, nums: list[int]) -> int:
ans =0for x in nums:
s = str(x)
mx = max(s)
enc = int(mx * len(s))
ans += enc
return ans
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
impl Solution {
pubfnsum_of_encrypted_int(nums: Vec<i32>) -> i32 {
letmut ans =0;
for x in nums {
letmut y = x;
letmut mx =0;
letmut len =0;
while y >0 {
mx = mx.max(y %10);
y /=10;
len +=1;
}
letmut enc =0;
for _ in0..len { enc = enc *10+ mx; }
ans += enc;
}
ans
}
}