原文:
给定n x m
阶的 2d 矩阵,以矩阵的螺旋形式打印第k
个元素。 请参阅以下示例。
示例:
input: mat[][] =
{{1, 2, 3, 4}
{5, 6, 7, 8}
{9, 10, 11, 12}
{13, 14, 15, 16}}
k = 6
output: 12
explanation: the elements in spiral order is
1, 2, 3, 4, 8, 12, 16, 15...
so the 6th element is 12
input: mat[][] =
{{1, 2, 3, 4, 5, 6}
{7, 8, 9, 10, 11, 12}
{13, 14, 15, 16, 17, 18}}
k = 17
output: 10
explanation: the elements in spiral order is
1, 2, 3, 4, 5, 6, 12, 18, 17,
16, 15, 14, 13, 7, 8, 9, 10, 11
so the 17 th element is 10\.
简单方法:一种简单的pg电子试玩链接的解决方案是开始以螺旋形式遍历矩阵并启动计数器,即count = 0
。只要count
等于k
,就打印该元素。
-
算法:
-
保持变量
count = 0
以存储计数。 -
从头到尾遍历矩阵。
-
每次迭代将计数增加 1。
-
如果计数等于
k
的给定值,则打印该元素并中断。
-
-
实现:
c
```
using namespace std;
void spiralprint(int m, int n, int a[r][c], int c) { int i, k = 0, l = 0; int count = 0;
/ k - starting row index m - ending row index l - starting column index n - ending column index i - iterator /
while (k < m && l < n) { / check the first row from the remaining rows / for (i = l; i < n; i) { count ;
if (count == c) cout << a[k][i] << " "; } k ;
/ check the last column from the remaining columns / for (i = k; i < m; i) { count ;
if (count == c) cout << a[i][n - 1] << " "; } n--;
/ check the last row from the remaining rows / if (k < m) { for (i = n - 1; i >= l; --i) { count ;
if (count == c) cout << a[m - 1][i] << " "; } m--; }
/ check the first column from the remaining columns / if (l < n) { for (i = m - 1; i >= k; --i) { count ;
if (count == c) cout << a[i][l] << " "; } l ; } } }
/ driver program to test above functions / int main() { int a[r][c] = { { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, { 13, 14, 15, 16, 17, 18 } }, k = 17;
spiralprint(r, c, a, k); return 0; }
```
java
```
import java.io.*;
class gfg { static int r = 3; static int c = 6;
static void spiralprint(int m, int n, int[][] a, int c) { int i, k = 0, l = 0; int count = 0;
/ k - starting row index m - ending row index l - starting column index n - ending column index i - iterator /
while (k < m && l < n) { / check the first row from the remaining rows / for (i = l; i < n; i) { count ;
if (count == c) system.out.println(a[k][i] " "); } k ;
/ check the last column from the remaining columns / for (i = k; i < m; i) { count ;
if (count == c) system.out.println(a[i][n - 1] " "); } n--;
/ check the last row from the remaining rows / if (k < m) { for (i = n - 1; i >= l; --i) { count ;
if (count == c)
system.out.println(a[m - 1][i] " "); } m--; }
/ check the first column from the remaining columns / if (l < n) { for (i = m - 1; i >= k; --i) { count ;
if (count == c) system.out.println(a[i][l] " "); } l ; } } }
/ driver program to test above functions / public static void main (string[] args) { int a[][] = { { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, { 13, 14, 15, 16, 17, 18 } }; int k = 17;
spiralprint(r, c, a, k); } }
// this code is contributed by shivanisinghss2110
```
python3
```
r = 3 c = 6
def spiralprint(m, n, a, c): k = 0 l = 0 count = 0 """ k - starting row index m - ending row index l - starting column index n - ending column index i - iterator """ while (k < m and l < n): for i in range(l,n): count =1
if (count == c): print(a[k][i] , end=" ")
k =1 """ check the last column from the remaining columns """ for i in range(k,m): count =1 if (count == c): print(a[i][n - 1],end=" ") n-=1 """ check the last row from the remaining rows """ if (k < m): for i in range(n - 1,l-1,-1): count =1 if (count == c): print(a[m - 1][i],end=" ") m-=1 """ check the first column from the remaining columns """ if (l < n): for i in range(m - 1,k-1,-1): count =1 if (count == c): print(a[i][l],end=" ") l =1
""" driver program to test above functions """
a = [[1, 2, 3, 4, 5, 6 ],[ 7, 8, 9, 10, 11, 12 ],[ 13, 14, 15, 16, 17, 18]] k = 17 spiralprint(r, c, a, k)
```
输出:
``` 10
```
-
复杂度分析:
-
时间复杂度:
o(r * c)
,需要矩阵的单个遍历,因此时间复杂度为o(r * c)
。 -
空间复杂度:
o(1)
,需要恒定的空间。
-
高效方法:以螺旋顺序遍历数组时,使用一个循环遍历边。 因此,如果可以发现第k
个元素位于给定的边,则可以在恒定时间内找到第k
个元素。 这可以递归和迭代地完成。
-
算法:
-
以螺旋或循环形式遍历矩阵。
-
因此,一个循环可分为 4 个部分,因此,如果循环的大小为
m x n
。 -
元素在第一行,即
k <= m
。 -
元素位于最后一列,即
k <= (m n-1)
。 -
元素位于最后一行,即
k <= (m n-1 m-1)
。 -
元素在第一列中,即
k <= (m n-1 m-1 n-2)
。 -
如果满足上述任何条件,则可以发现第
k
个元素是恒定时间。 -
否则,从数组中删除循环并递归调用该函数。
-
-
实现:
c
```
// c program for kth element in spiral // form of matrix
using namespace std;
/ function for kth element / int findk(int a[max][max], int n, int m, int k) { if (n < 1 || m < 1) return -1;
/....if element is in outermost ring ..../ / element is in first row / if (k <= m) return a[0][k - 1];
/ element is in last column / if (k <= (m n - 1)) return a[(k - m)][m - 1];
/ element is in last row / if (k <= (m n - 1 m - 1)) return a[n - 1][m - 1 - (k - (m n - 1))];
/ element is in first column / if (k <= (m n - 1 m - 1 n - 2)) return a[n - 1 - (k - (m n - 1 m - 1))][0];
/....if element is not in outermost ring ..../ / recursion for sub-matrix. &a[1][1] is address to next inside sub matrix./ return findk((int(*)[max])(&(a[1][1])), n - 2, m - 2, k - (2 * n 2 * m - 4)); }
/ driver code / int main() { int a[max][max] = { { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, { 13, 14, 15, 16, 17, 18 } }; int k = 17; cout << findk(a, 3, 6, k) << endl; return 0; }
```
java
```
// java program for kth element in spiral // form of matrix class gfg {
static int max = 100;
/ function for kth element / static int findk(int a[][], int i, int j, int n, int m, int k) { if (n < 1 || m < 1) return -1;
/.....if element is in outermost ring ..../ / element is in first row / if (k <= m) return a[i 0][j k - 1];
/ element is in last column / if (k <= (m n - 1)) return a[i (k - m)][j m - 1];
/ element is in last row / if (k <= (m n - 1 m - 1)) return a[i n - 1][j m - 1 - (k - (m n - 1))];
/ element is in first column / if (k <= (m n - 1 m - 1 n - 2)) return a[i n - 1 - (k - (m n - 1 m - 1))][j 0];
/.....if element is not in outermost ring ..../ / recursion for sub-matrix. &a[1][1] is address to next inside sub matrix./ return findk(a, i 1, j 1, n - 2, m - 2, k - (2 * n 2 * m - 4)); }
/ driver code / public static void main(string args[]) { int a[][] = { { 1, 2, 3, 4, 5, 6 }, { 7, 8, 9, 10, 11, 12 }, { 13, 14, 15, 16, 17, 18 } }; int k = 17; system.out.println(findk(a, 0, 0, 3, 6, k)); } }
// this code is contributed by arnab kundu
```
python3
```
max = 100
''' function for kth element ''' def findk(a, n, m, k):
if (n < 1 or m < 1): return -1
'''....if element is in outermost ring ....''' ''' element is in first row ''' if (k <= m): return a[0][k - 1]
''' element is in last column ''' if (k <= (m n - 1)): return a[(k - m)][m - 1]
''' element is in last row ''' if (k <= (m n - 1 m - 1)): return a[n - 1][m - 1 - (k - (m n - 1))]
''' element is in first column ''' if (k <= (m n - 1 m - 1 n - 2)): return a[n - 1 - (k - (m n - 1 m - 1))][0]
'''....if element is not in outermost ring ....''' ''' recursion for sub-matrix. &a[1][1] is address to next inside sub matrix.''' a.pop(0) [j.pop(0) for j in a] return findk(a, n - 2, m - 2, k - (2 * n 2 * m - 4))
''' driver code '''
a = [[1, 2, 3, 4, 5, 6],[7, 8, 9, 10, 11, 12 ], [ 13, 14, 15, 16, 17, 18 ]] k = 17 print(findk(a, 3, 6, k))
```
输出:
``` 10
```
-
复杂度分析:
-
时间复杂度:
o(c)
,其中c
是相对于第k
个元素的外圆环数。 -
空间复杂度:
o(1)
。由于需要恒定的空间。
-
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处