Reversing an integer means to reverse all its digits.
2021
gives 1202
. Reversing 12300
gives 321
as the leading zeros are not retained.Given an integer num
, reverse num
to get reversed1
, then reverse reversed1
to get reversed2
. Return true
if reversed2
equals num
. Otherwise return false
.
Example 1:
Input: num = 526 Output: true Explanation: Reverse num to get 625, then reverse 625 to get 526, which equals num.
Example 2:
Input: num = 1800 Output: false Explanation: Reverse num to get 81, then reverse 81 to get 18, which does not equal num.
Example 3:
Input: num = 0 Output: true Explanation: Reverse num to get 0, then reverse 0 to get 0, which equals num.
Constraints:
0 <= num <= 106
program main
implicit none
integer :: num, reversed1, reversed2
logical :: result
! Example 1
num = 526
call reverse(num, reversed1)
call reverse(reversed1, reversed2)
result = (reversed2 == num)
write (*, '(A, L1)') 'Example 1: ', result
! Example 2
num = 1800
call reverse(num, reversed1)
call reverse(reversed1, reversed2)
result = (reversed2 == num)
write (*, '(A, L1)') 'Example 2: ', result
! Example 3
num = 0
call reverse(num, reversed1)
call reverse(reversed1, reversed2)
result = (reversed2 == num)
write (*, '(A, L1)') 'Example 3: ', result
contains
subroutine reverse(num, reversed)
implicit none
integer, intent(in) :: num
integer, intent(out) :: reversed
character(len=4) :: str
integer :: i, j
write (str, '(I4)') num
reversed = 0
do i = 1, len(str)
j = len(str) - i + 1
reversed = (reversed * 10) + ichar(str(j:j)) - ichar('0')
end do
end subroutine reverse
end program main
Example 1: F Example 2: F Example 3: F
! Reverse an integer
integer function reverse(num) result(reversed)
implicit none
integer, intent(in) :: num
integer :: reversed1, reversed2
! Reverse num to get reversed1
reversed1 = 0
do while (num > 0)
reversed1 = reversed1 * 10 + mod(num, 10)
num = num / 10
end do
! Reverse reversed1 to get reversed2
reversed2 = 0
do while (reversed1 > 0)
reversed2 = reversed2 * 10 + mod(reversed1, 10)
reversed1 = reversed1 / 10
end do
! Check if reversed2 equals num
if (reversed2 == num) then
reversed = .true.
else
reversed = .false.
end if
end function reverse
! Test the reverse function with some examples
program main
implicit none
integer :: num, reversed
! Example 1: 526
num = 526
reversed = reverse(num)
if (reversed) then
write (*,*) "Reversing ", num, " gives ", num, " which equals ", num
else
write (*,*) "Reversing ", num, " gives ", num, " which does not equal ", num
end if
! Example 2: 1800
num = 1800
reversed = reverse(num)
if (reversed) then
write (*,*) "Reversing ", num, " gives ", num, " which equals ", num
else
write (*,*) "Reversing ", num, " gives ", num, " which does not equal ", num
end if
! Example 3: 0
num = 0
reversed = reverse(num)
if (reversed) then
write (*,*) "Reversing ", num, " gives ", num, " which equals ", num
else
write (*,*) "Reversing ", num, " gives ", num, " which does not equal ", num
end if
end program main
temp.f95:12:8: 12 | num = num / 10 | 1 Error: Dummy argument ‘num’ with INTENT(IN) in variable definition context (assignment) at (1) temp.f95:24:19: 24 | reversed = .true. | 1 Warning: Extension: Conversion from LOGICAL(4) to INTEGER(4) at (1) temp.f95:26:19: 26 | reversed = .false. | 1 Warning: Extension: Conversion from LOGICAL(4) to INTEGER(4) at (1) temp.f95:37:22: 37 | reversed = reverse(num) | 1 Error: Return type mismatch of function ‘reverse’ at (1) (UNKNOWN/INTEGER(4)) temp.f95:37:15: 37 | reversed = reverse(num) | 1 Error: Function ‘reverse’ at (1) has no IMPLICIT type temp.f95:38:8: 38 | if (reversed) then | 1 Error: IF clause at (1) requires a scalar LOGICAL expression temp.f95:37:22: 37 | reversed = reverse(num) | 1 Error: Return type mismatch of function ‘reverse’ at (1) (UNKNOWN/INTEGER(4)) temp.f95:46:15: 46 | reversed = reverse(num) | 1 Error: Function ‘reverse’ at (1) has no IMPLICIT type temp.f95:47:8: 47 | if (reversed) then | 1 Error: IF clause at (1) requires a scalar LOGICAL expression temp.f95:37:22: 37 | reversed = reverse(num) | 1 Error: Return type mismatch of function ‘reverse’ at (1) (UNKNOWN/INTEGER(4)) temp.f95:55:15: 55 | reversed = reverse(num) | 1 Error: Function ‘reverse’ at (1) has no IMPLICIT type temp.f95:56:8: 56 | if (reversed) then | 1 Error: IF clause at (1) requires a scalar LOGICAL expression
def minOperations(nums):
nums = sorted(set(nums))
n = len(nums)
ans = n
for i in range(n):
j = 0
while j < n and nums[j] <= nums[i] + n - 1:
j += 1
ans = min(ans, n - (j - i))
return ans
The algorithm first sorts the input array nums
and removes duplicates. Then, for each element in the sorted array, it calculates the upper bound position (j
) of the elements that can be included in a continuous range with the given element. The difference between the positions j
and i
is the number of elements in the continuous range. Subtracting this difference from the total number of unique elements in nums
gives the number of operations required for this particular range. The final answer is the minimum number of operations required for all possible ranges.
#include <vector>
#include <algorithm>
using namespace std;
int minOperations(vector<int>& nums) {
sort(nums.begin(), nums.end());
nums.erase(unique(nums.begin(), nums.end()), nums.end());
int n = nums.size();
int ans = n;
for (int i = 0; i < n; ++i) {
int j = upper_bound(nums.begin() + i, nums.end(), nums[i] + n - 1) - nums.begin();
ans = min(ans, n - (j - i));
}
return ans;
}
The algorithm first sorts the input array nums
and removes duplicates. Then, for each element in the sorted array, it calculates the upper bound position (j
) of the elements that can be included in a continuous range with the given element. The difference between the positions j
and i
is the number of elements in the continuous range. Subtracting this difference from the total number of unique elements in nums
gives the number of operations required for this particular range. The final answer is the minimum number of operations required for all possible ranges.