Given a string s
, reverse the string according to the following rules:
Return s
after reversing it.
Example 1:
Input: s = "ab-cd" Output: "dc-ba"
Example 2:
Input: s = "a-bC-dEf-ghIj" Output: "j-Ih-gfE-dCba"
Example 3:
Input: s = "Test1ng-Leet=code-Q!" Output: "Qedo1ct-eeLg=ntse-T!"
Constraints:
1 <= s.length <= 100
s
consists of characters with ASCII values in the range [33, 122]
.s
does not contain '\ "'
or '\\'
.program main
implicit none
character(len=100) :: s
call get_command_argument(1, s)
print '(A)', reverse_string(s)
contains
function reverse_string(s) result(r)
implicit none
character(len=*), intent(in) :: s
character(len=len(s)) :: r
integer :: i, j
do i = 1, len(s)
if (is_letter(s(i:i))) then
j = len(s) - i + 1
r(j:j) = s(i:i)
else
r(i:i) = s(i:i)
end if
end do
end function reverse_string
function is_letter(c) result(res)
implicit none
character(len=*), intent(in) :: c
logical :: res
res = any(c == ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']) .or. &
any(c == ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'])
end function is_letter
end program main
temp.f95:34:132: 34 | res = any(c == ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']) .or. & | 1 Error: Line truncated at (1) [-Werror=line-truncation] temp.f95:34:23: 34 | res = any(c == ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']) .or. & | 1 Error: Invalid character in name at (1) f951: some warnings being treated as errors
! This program reverses a string according to the following rules:
!
! * All the characters that are not English letters remain in the same position.
! * All the English letters (lowercase or uppercase) should be reversed.
program reverse_string
implicit none
character(len=100) :: s
character(len=100) :: reversed_s
integer :: i, j
! Read the input string from the command line argument
call get_command_argument(1, s)
! Initialize the reversed string to all spaces
reversed_s = repeat(' ', len(s))
! Iterate over the characters in the input string
do i = 1, len(s)
! If the character is an English letter, reverse it
if (is_letter(s(i:i))) then
reversed_s(len(s) - i + 1:len(s) - i + 1) = s(i:i)
else
! If the character is not an English letter, leave it in the same position
reversed_s(i:i) = s(i:i)
end if
end do
! Print the reversed string to stdout
write (*, '(a)') reversed_s
contains
! Check if a character is an English letter
function is_letter(c) result(is_letter)
character(len=1), intent(in) :: c
logical :: is_letter
is_letter = (c >= 'a' .and. c <= 'z') .or. (c >= 'A' .and. c <= 'Z')
end function is_letter
end program reverse_string
temp.f95:37:39: 37 | function is_letter(c) result(is_letter) | 1 Error: RESULT variable at (1) must be different than function name temp.f95:38:37: 38 | character(len=1), intent(in) :: c | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:39:24: 39 | logical :: is_letter | 1 Error: Symbol ‘is_letter’ at (1) has already been host associated temp.f95:41:14: 41 | is_letter = (c >= 'a' .and. c <= 'z') .or. (c >= 'A' .and. c <= 'Z') | 1 Error: Symbol ‘is_letter’ at (1) has already been host associated temp.f95:42:3: 42 | end function is_letter | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:23:8: 23 | if (is_letter(s(i:i))) then | 1 Error: Function ‘is_letter’ at (1) has no IMPLICIT type
def num_rescue_boats(people, limit):
people.sort()
left, right = 0, len(people) - 1
boats = 0
while left <= right:
if people[left] + people[right] <= limit:
left += 1
right -= 1
boats += 1
return boats
The algorithm sorts the weights of the people in ascending order. Then, it uses two pointers, left, starting at the first person, and right, starting at the last person, to assign people to boats efficiently.
In each iteration, if the person pointed by left and the person pointed by right can fit in the same boat, they are assigned to one. Whether those two people can share a boat or not, we still move the right pointer to the previous person, because the heaviest person will always need a boat.
This process is repeated until the left pointer and right pointer meet, guaranteeing all people have been assigned to boats. The count of boats used during the process is returned as the minimum number of boats needed.
#include <algorithm>
#include <vector>
int numRescueBoats(std::vector<int>& people, int limit) {
std::sort(people.begin(), people.end());
int left = 0, right = people.size() - 1;
int boats = 0;
while (left <= right) {
if (people[left] + people[right] <= limit) {
left++;
}
right--;
boats++;
}
return boats;
}
The algorithm sorts the weights of the people in ascending order. Then, it uses two pointers, left, starting at the first person, and right, starting at the last person, to assign people to boats efficiently.
In each iteration, if the person pointed by left and the person pointed by right can fit in the same boat, they are assigned to one. Whether those two people can share a boat or not, we still move the right pointer to the previous person, because the heaviest person will always need a boat.
This process is repeated until the left pointer and right pointer meet, guaranteeing all people have been assigned to boats. The count of boats used during the process is returned as the minimum number of boats needed.