Given a 2D integer array matrix
, return the transpose of matrix
.
The transpose of a matrix is the matrix flipped over its main diagonal, switching the matrix's row and column indices.
Example 1:
Input: matrix = [[1,2,3],[4,5,6],[7,8,9]] Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:
Input: matrix = [[1,2,3],[4,5,6]] Output: [[1,4],[2,5],[3,6]]
Constraints:
m == matrix.length
n == matrix[i].length
1 <= m, n <= 1000
1 <= m * n <= 105
-109 <= matrix[i][j] <= 109
program main
implicit none
integer :: matrix(3, 3)
matrix(:, :) = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], shape(matrix))
write(*, '(A)') 'Example 1:'
call print_matrix(transpose(matrix))
matrix(:, :) = reshape([1, 2, 3, 4, 5, 6], shape(matrix))
write(*, '(A)') 'Example 2:'
call print_matrix(transpose(matrix))
contains
subroutine print_matrix(matrix)
implicit none
integer, intent(in) :: matrix(:, :)
integer :: i, j
do i = 1, size(matrix, 1)
write(*, '(*(I2, 1X))') (matrix(i, j), j=1, size(matrix, 2))
end do
end subroutine print_matrix
end program main
temp.f95:11:27: 11 | matrix(:, :) = reshape([1, 2, 3, 4, 5, 6], shape(matrix)) | 1 Error: Without padding, there are not enough elements in the intrinsic RESHAPE source at (1) to match the shape
module transpose
implicit none
contains
function transpose(matrix) result(transposed_matrix)
integer, intent(in) :: matrix(:, :)
integer :: transposed_matrix(size(matrix, 2), size(matrix, 1))
integer :: i, j
do i = 1, size(matrix, 1)
do j = 1, size(matrix, 2)
transposed_matrix(j, i) = matrix(i, j)
end do
end do
end function transpose
end module transpose
program main
use transpose
implicit none
integer, allocatable :: matrix(:, :)
integer :: i, j
! Example 1
allocate(matrix(3, 3))
matrix = reshape([1, 2, 3, 4, 5, 6, 7, 8, 9], [3, 3])
write (*, '(A)') 'Example 1'
write (*, '(A)') 'Input:'
write (*, '(A)') 'matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]'
write (*, '(A)') 'Output:'
write (*, '(A)') '[[1, 4, 7], [2, 5, 8], [3, 6, 9]]'
write (*, '(A)') 'Actual:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') 'Expected:'
write (*, '(A)') '[[1, 4, 7], [2, 5, 8], [3, 6, 9]]'
write (*, '(A)') 'Diff:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') '--------------------------------------------------------------------------------'
! Example 2
allocate(matrix(2, 3))
matrix = reshape([1, 2, 3, 4, 5, 6], [2, 3])
write (*, '(A)') 'Example 2'
write (*, '(A)') 'Input:'
write (*, '(A)') 'matrix = [[1, 2, 3], [4, 5, 6]]'
write (*, '(A)') 'Output:'
write (*, '(A)') '[[1, 4], [2, 5], [3, 6]]'
write (*, '(A)') 'Actual:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') 'Expected:'
write (*, '(A)') '[[1, 4], [2, 5], [3, 6]]'
write (*, '(A)') 'Diff:'
write (*, '(A)') '[[', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), '],'
write (*, '(A)') ' [', (matrix(i, j), j = 1, 3), ']]'
write (*, '(A)') '--------------------------------------------------------------------------------'
! Example 3
temp.f95:7:18: 7 | function transpose(matrix) result(transposed_matrix) | 1 Error: MODULE attribute of ‘transpose’ conflicts with PROCEDURE attribute at (1) temp.f95:9:35: 9 | integer, intent(in) :: matrix(:, :) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:34: 10 | integer :: transposed_matrix(size(matrix, 2), size(matrix, 1)) | 1 Error: Symbol ‘matrix’ is used before it is typed at (1) temp.f95:11:15: 11 | integer :: i, j | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:25: 13 | do i = 1, size(matrix, 1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:14:29: 14 | do j = 1, size(matrix, 2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:15:46: 15 | transposed_matrix(j, i) = matrix(i, j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:16:7: 16 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:17:3: 17 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:19:3: 19 | end function transpose | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:5: 25 | use transpose | 1 Fatal Error: Cannot open module file ‘transpose.mod’ for reading at (1): No such file or directory compilation terminated.
def new21Game(n: int, k: int, maxPts: int) -> float:
if k == 0 or n >= k + maxPts:
return 1
dp = [0] * (n + 1)
dp[0] = 1
s, ans = 1, 0
for i in range(1, n + 1):
dp[i] = s / maxPts
if i < k:
s += dp[i]
else:
ans += dp[i]
if i >= maxPts:
s -= dp[i - maxPts]
return ans
1. If Alice has already reached k points or n >= k + maxPts, there is no chance of drawing more cards, so return 1.
#include <vector>
using namespace std;
double new21Game(int n, int k, int maxPts) {
if (k == 0 || n >= k + maxPts) {
return 1;
}
vector<double> dp(n + 1, 0);
dp[0] = 1;
double sum = 1, ans = 0;
for (int i = 1; i <= n; ++i) {
dp[i] = sum / maxPts;
if (i < k) {
sum += dp[i];
} else {
ans += dp[i];
}
if (i >= maxPts) {
sum -= dp[i - maxPts];
}
}
return ans;
}
1. If Alice has already reached k points or n >= k + maxPts, there is no chance of drawing more cards, so return 1.