Given an m x n
matrix
, return true
if the matrix is Toeplitz. Otherwise, return false
.
A matrix is Toeplitz if every diagonal from top-left to bottom-right has the same elements.
Example 1:
Input: matrix = [[1,2,3,4],[5,1,2,3],[9,5,1,2]] Output: true Explanation: In the above grid, the diagonals are: "[9] ", "[5, 5] ", "[1, 1, 1] ", "[2, 2, 2] ", "[3, 3] ", "[4] ". In each diagonal all elements are the same, so the answer is True.
Example 2:
Input: matrix = [[1,2],[2,2]] Output: false Explanation: The diagonal "[1, 2] " has different elements.
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 20
0 <= matrix[i][j] <= 99
Follow up:
matrix
is stored on disk, and the memory is limited such that you can only load at most one row of the matrix into the memory at once?matrix
is so large that you can only load up a partial row into the memory at once?program main
implicit none
integer, parameter :: m = 3, n = 4
integer, dimension(m, n) :: matrix
logical :: result
! Example 1
matrix(:, :) = reshape([1, 2, 3, 4, &
5, 1, 2, 3, &
9, 5, 1, 2], shape(matrix))
write(*, '(L1)') toeplitz(matrix)
! Example 2
matrix(:, :) = reshape([1, 2, &
2, 2], shape(matrix))
write(*, '(L1)') toeplitz(matrix)
contains
function toeplitz(matrix) result(result)
implicit none
integer, intent(in) :: matrix(:, :)
logical :: result
integer :: i, j
do i = 1, size(matrix, 1) - 1
do j = 1, size(matrix, 2) - 1
if (matrix(i, j) /= matrix(i + 1, j + 1)) then
result = .false.
return
end if
end do
end do
result = .true.
end function toeplitz
end program main
temp.f95:15:27: 15 | matrix(:, :) = reshape([1, 2, & | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
module toeplitz
implicit none
contains
function is_toeplitz(matrix) result(is_toeplitz)
integer, intent(in) :: matrix(:,:)
logical :: is_toeplitz
integer :: i, j, m, n
m = size(matrix, 1)
n = size(matrix, 2)
is_toeplitz = .true.
do i = 1, m - 1
do j = 1, n
if (matrix(i, j) .ne. matrix(i+1, j+1)) then
is_toeplitz = .false.
exit
end if
end do
if (.not. is_toeplitz) exit
end do
end function is_toeplitz
end module toeplitz
program main
use toeplitz
implicit none
integer, parameter :: m = 3, n = 4
integer :: matrix(m, n) = reshape([1, 2, 3, 4, 5, 1, 2, 3, 9, 5, 1, 2], [m, n])
write (*, '(L1)') is_toeplitz(matrix)
end program main
temp.f95:5:48: 5 | function is_toeplitz(matrix) result(is_toeplitz) | 1 Error: RESULT variable at (1) must be different than function name temp.f95:6:34: 6 | integer, intent(in) :: matrix(:,:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:7:22: 7 | logical :: is_toeplitz | 1 Error: Symbol ‘is_toeplitz’ at (1) has already been host associated temp.f95:8:21: 8 | integer :: i, j, m, n | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:19: 10 | m = size(matrix, 1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:11:19: 11 | n = size(matrix, 2) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:13:12: 13 | is_toeplitz = .true. | 1 Error: Symbol ‘is_toeplitz’ at (1) has already been host associated temp.f95:15:15: 15 | do i = 1, m - 1 | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:16:15: 16 | do j = 1, n | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:52: 17 | if (matrix(i, j) .ne. matrix(i+1, j+1)) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:18:24: 18 | is_toeplitz = .false. | 1 Error: Symbol ‘is_toeplitz’ at (1) has already been host associated temp.f95:19:16: 19 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:20:11: 20 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:21:7: 21 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:22:26: 22 | if (.not. is_toeplitz) exit | 1 Error: Function ‘is_toeplitz’ requires an argument list at (1) temp.f95:23:3: 23 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:3: 25 | end function is_toeplitz | 1 Error: Expecting END MODULE statement at (1) temp.f95:30:5: 30 | use toeplitz | 1 Fatal Error: Cannot open module file ‘toeplitz.mod’ for reading at (1): No such file or directory compilation terminated.
class Node:
def __init__(self, val=0, prev=None, next=None, child=None):
self.val = val
self.prev = prev
self.next = next
self.child = child
def flatten(head: Node) -> Node:
if not head:
return None
curr = head
while curr:
if curr.child:
insert = curr.child
next_node = curr.next
insert.prev = curr
curr.next = insert
curr.child = None
while insert.next:
insert = insert.next
if next_node:
next_node.prev = insert
insert.next = next_node
curr = curr.next
return head
The basic idea of the algorithm is to traverse the doubly linked list and if a node has a child, we insert the child list between the current node and its next node. We do this by adjusting the previous and next pointers accordingly. After inserting the child list, we set the child pointer of the current node to null. We keep doing this for each node in the list.
class Node {
public:
int val;
Node* prev;
Node* next;
Node* child;
};
Node* flatten(Node* head) {
if(!head) return nullptr;
Node* curr = head;
while(curr) {
if(curr->child) {
Node* insert = curr->child;
Node* next = curr->next;
insert->prev = curr;
curr->next = insert;
curr->child = nullptr;
while(insert->next) insert = insert->next;
if(next) {
next->prev = insert;
insert->next = next;
}
}
curr = curr->next;
}
return head;
}
The basic idea of the algorithm is to traverse the doubly linked list and if a node has a child, we insert the child list between the current node and its next node. We do this by adjusting the previous and next pointers accordingly. After inserting the child list, we set the child pointer of the current node to null. We keep doing this for each node in the list.