Given a string path
, where path[i] = 'N'
, 'S'
, 'E'
or 'W'
, each representing moving one unit north, south, east, or west, respectively. You start at the origin (0, 0)
on a 2D plane and walk on the path specified by path
.
Return true
if the path crosses itself at any point, that is, if at any time you are on a location you have previously visited. Return false
otherwise.
Example 1:
Input: path = "NES " Output: false Explanation: Notice that the path doesn't cross any point more than once.
Example 2:
Input: path = "NESWW " Output: true Explanation: Notice that the path visits the origin twice.
Constraints:
1 <= path.length <= 104
path[i]
is either 'N'
, 'S'
, 'E'
, or 'W'
.program main
implicit none
character(len=3) :: path
logical :: result
! Example 1
path = "NES"
result = crosses_path(path)
print *, result
! Example 2
path = "NESWW"
result = crosses_path(path)
print *, result
contains
function crosses_path(path) result(crosses)
implicit none
character(len=*), intent(in) :: path
integer :: i, j, n
logical :: crosses
integer, allocatable :: x(:), y(:)
n = len(path)
allocate(x(n))
allocate(y(n))
x(1) = 0
y(1) = 0
do i = 2, n
select case (path(i-1:i-1))
case('N')
y(i) = y(i-1) + 1
case('S')
y(i) = y(i-1) - 1
case('E')
x(i) = x(i-1) + 1
case('W')
x(i) = x(i-1) - 1
end select
end do
crosses = .false.
do i = 1, n-1
do j = i+1, n
if (x(i) == x(j) .and. y(i) == y(j)) then
crosses = .true.
exit
end if
end do
if (crosses) exit
end do
deallocate(x)
deallocate(y)
end function crosses_path
end program main
F F
!include "io.h"
program main
implicit none
character(len=1000) :: path
logical :: crosses
read (*, *) path
crosses = crosses_path(path)
if (crosses) then
write (*, *) "True"
else
write (*, *) "False"
end if
contains
function crosses_path(path) result(crosses)
implicit none
character(len=1000), intent(in) :: path
logical :: crosses
integer :: i, j
integer :: x, y
integer, dimension(1000) :: visited
x = 0
y = 0
visited = 0
do i = 1, len(path)
select case (path(i:i))
case ("N")
y = y + 1
case ("S")
y = y - 1
case ("E")
x = x + 1
case ("W")
x = x - 1
end select
if (visited(x + 1000 * y) == 1) then
crosses = .true.
return
end if
visited(x + 1000 * y) = 1
end do
crosses = .false.
end function crosses_path
end program main
At line 9 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7be89190a960 in ??? #1 0x7be89190b4d9 in ??? #2 0x7be891b5f17b in ??? #3 0x7be891b58684 in ??? #4 0x7be891b592aa in ??? #5 0x5d22942fb318 in MAIN__ #6 0x5d22942fb45c in main
def lucky_numbers(matrix):
m, n = len(matrix), len(matrix[0])
lucky_numbers = []
for i in range(m):
min_row = min(matrix[i])
min_row_idx = matrix[i].index(min_row)
is_lucky = all(matrix[k][min_row_idx] <= min_row for k in range(m))
if is_lucky:
lucky_numbers.append(min_row)
return lucky_numbers
lucky_numbers
to store the final lucky numbers from the matrix.min_row
in the row and its index min_row_idx
.min_row
is the maximum element in its column min_row_idx
. If so, add min_row
to the list of lucky_numbers
.lucky_numbers
list.#include <vector>
using namespace std;
vector<int> luckyNumbers(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<int> lucky_numbers;
for (int i = 0; i < m; ++i) {
int min_row = matrix[i][0], min_row_idx = 0;
for (int j = 0; j < n; ++j) {
if (matrix[i][j] < min_row) {
min_row = matrix[i][j];
min_row_idx = j;
}
}
bool is_lucky = true;
for (int k = 0; k < m; ++k) {
if (matrix[k][min_row_idx] > min_row) {
is_lucky = false;
break;
}
}
if (is_lucky)
lucky_numbers.push_back(min_row);
}
return lucky_numbers;
}
lucky_numbers
to store the final lucky numbers from the matrix.min_row
in the row and its index min_row_idx
.min_row
is the maximum element in its column min_row_idx
. If so, add min_row
to the list of lucky_numbers
.lucky_numbers
list.