原文:

给定 2d 数组,以逆时针螺旋形式打印。 请参阅以下示例。

示例

input:
        1    2   3   4
        5    6   7   8
        9   10  11  12
        13  14  15  16
output: 
1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7 
input:
        1   2   3   4  5   6
        7   8   9  10  11  12
        13  14  15 16  17  18
output: 
1 7 13 14 15 16 17 18 12 6 5 4 3 2 8 9 10 11 

说明

下面是实现:

c

// c   implementation to print 
// the counter clock wise 
// spiral traversal of matrix 
#include  
using namespace std; 
  
#define r 4 
#define c 4 
  
// function to print the 
// required traversal 
void counterclockspiralprint(int m,  
                             int n,  
                             int arr[r][c]) 
{ 
    int i, k = 0, l = 0; 
  
    //  k - starting row index 
    //    m - ending row index 
    //    l - starting column index 
    //    n - ending column index 
    //    i - iterator  
  
    // initialize the count 
    int cnt = 0; 
  
    // total number of  
    // elements in matrix 
    int total = m * n; 
  
    while (k < m && l < n)  
    { 
        if (cnt == total) 
            break; 
  
        // print the first column  
        // from the remaining columns 
        for (i = k; i < m;   i) 
        { 
            cout << arr[i][l] << " "; 
            cnt  ; 
        } 
        l  ; 
  
        if (cnt == total) 
            break; 
  
        // print the last row from 
        // the remaining rows  
        for (i = l; i < n;   i)  
        { 
            cout << arr[m - 1][i] << " "; 
            cnt  ; 
        } 
        m--; 
  
        if (cnt == total) 
            break; 
  
        // print the last column  
        // from the remaining columns  
        if (k < m)  
        { 
            for (i = m - 1; i >= k; --i)  
            { 
                cout << arr[i][n - 1] << " "; 
                cnt  ; 
            } 
            n--; 
        } 
  
        if (cnt == total) 
            break; 
  
        // print the first row  
        // from the remaining rows  
        if (l < n)  
        { 
            for (i = n - 1; i >= l; --i)  
            { 
                cout << arr[k][i] << " "; 
                cnt  ; 
            } 
            k  ; 
        } 
    } 
} 
  
// driver code 
int main() 
{ 
    int arr[r][c] = {{ 1, 2, 3, 4 }, 
                     { 5, 6, 7, 8 }, 
                     { 9, 10, 11, 12 }, 
                     { 13, 14, 15, 16 }}; 
    counterclockspiralprint(r, c, arr); 
    return 0; 
}

java

// java implementation to print 
// the counter clock wise 
// spiral traversal of matrix 
import java.io.*; 
  
class gfg  
{ 
    static int r = 4; 
    static int c = 4; 
  
    // function to print the 
    // required traversal 
    static void counterclockspiralprint(int m,  
                                        int n,  
                                        int arr[][]) 
    { 
        int i, k = 0, l = 0; 
  
    /* k - starting row index 
        m - ending row index 
        l - starting column index 
        n - ending column index 
        i - iterator */
  
        // initialize the count 
        int cnt = 0; 
  
        // total number of 
        // elements in matrix 
        int total = m * n; 
  
        while (k < m && l < n)  
        { 
            if (cnt == total) 
                break; 
  
            // print the first column  
            // from the remaining columns 
            for (i = k; i < m;   i) 
            { 
                system.out.print(arr[i][l]   " "); 
                cnt  ; 
            } 
            l  ; 
  
            if (cnt == total) 
                break; 
  
            // print the last row from 
            // the remaining rows  
            for (i = l; i < n;   i)  
            { 
                system.out.print(arr[m - 1][i]   " "); 
                cnt  ; 
            } 
            m--; 
  
            if (cnt == total) 
                break; 
  
            // print the last column  
            // from the remaining columns  
            if (k < m)  
            { 
                for (i = m - 1; i >= k; --i)  
                { 
                    system.out.print(arr[i][n - 1]   " "); 
                    cnt  ; 
                } 
                n--; 
            } 
  
            if (cnt == total) 
                break; 
  
            // print the first row  
            // from the remaining rows 
            if (l < n)  
            { 
                for (i = n - 1; i >= l; --i)  
                { 
                    system.out.print(arr[k][i]   " "); 
                    cnt  ; 
                } 
                k  ; 
            } 
        } 
    } 
  
// driver code 
public static void main(string[] args) 
{ 
    int arr[][] = { { 1, 2, 3, 4 }, 
                    { 5, 6, 7, 8 }, 
                    { 9, 10, 11, 12 }, 
                    { 13, 14, 15, 16 } }; 
      
    // function calling              
    counterclockspiralprint(r, c, arr); 
} 
} 
  
// this code is contributed by vt_m

python3

# python3 implementation to print 
# the counter clock wise 
# spiral traversal of matrix 
r = 4
c = 4
  
# function to print  
# the required traversal 
def counterclockspiralprint(m, n, arr) : 
    k = 0; l = 0
      
    # k - starting row index 
    # m - ending row index 
    # l - starting column index 
    # n - ending column index 
    # i - iterator  
  
    # initialize the count 
    cnt = 0
  
    # total number of  
    # elements in matrix 
    total = m * n 
  
    while (k < m and l < n) : 
        if (cnt == total) : 
            break
  
        # print the first column  
        # from the remaining columns  
        for i in range(k, m) : 
            print(arr[i][l], end = " ") 
            cnt  = 1
          
        l  = 1
  
        if (cnt == total) : 
            break
  
        # print the last row from 
        # the remaining rows  
        for i in range (l, n) : 
            print( arr[m - 1][i], end = " ") 
            cnt  = 1
          
        m -= 1
          
        if (cnt == total) : 
            break
  
        # print the last column   
        # from the remaining columns  
        if (k < m) : 
            for i in range(m - 1, k - 1, -1) : 
                print(arr[i][n - 1], end = " ") 
                cnt  = 1
            n -= 1
  
        if (cnt == total) : 
            break
  
        # print the first row  
        # from the remaining rows  
        if (l < n) : 
            for i in range(n - 1, l - 1, -1) : 
                print( arr[k][i], end = " ") 
                cnt  = 1
                  
            k  = 1
              
  
# driver code 
arr = [ [ 1, 2, 3, 4 ], 
        [ 5, 6, 7, 8 ], 
        [ 9, 10, 11, 12 ], 
        [ 13, 14, 15, 16 ] ] 
          
counterclockspiralprint(r, c, arr) 
  
# this code is contributed by nikita tiwari

c

// c# implementation to print 
// the counter clock wise 
// spiral traversal of matrix; 
using system; 
  
class gfg  
{ 
    static int r = 4; 
    static int c = 4; 
  
    // function to print the required traversal 
    static void counterclockspiralprint(int m,  
                                        int n,  
                                        int[,] arr) 
    { 
        int i, k = 0, l = 0; 
  
        // k - starting row index 
        // m - ending row index 
        // l - starting column index 
        // n - ending column index 
        // i - iterator 
  
        // initialize the count 
        int cnt = 0; 
  
        // total number of elements in matrix 
        int total = m * n; 
  
        while (k < m && l < n)  
        { 
            if (cnt == total) 
                break; 
  
            // print the first column from  
            // the remaining columns 
            for (i = k; i < m;   i)  
            { 
                console.write(arr[i,l]   " "); 
                cnt  ; 
            } 
            l  ; 
  
            if (cnt == total) 
                break; 
  
            // print the last row from 
            // the remaining rows  
            for (i = l; i < n;   i) 
            { 
                console.write(arr[m - 1, i]   " "); 
                cnt  ; 
            } 
            m--; 
  
            if (cnt == total) 
                break; 
  
            // print the last column from  
            // the remaining columns 
            if (k < m) { 
                for (i = m - 1; i >= k; --i) 
                { 
                    console.write(arr[i, n - 1]   " "); 
                    cnt  ; 
                } 
                n--; 
            } 
  
            if (cnt == total) 
                break; 
  
            // print the first row from 
            // the remaining rows  
            if (l < n)  
            { 
                for (i = n - 1; i >= l; --i) 
                { 
                    console.write(arr[k, i]   " "); 
                    cnt  ; 
                } 
                k  ; 
            } 
        } 
    } 
  
// driver code 
public static void main() 
{ 
    int[,] arr =new int[,] {{1, 2, 3, 4}, 
                            {5, 6, 7, 8}, 
                            {9, 10, 11, 12}, 
                            {13, 14, 15, 16}}; 
      
    // function calling      
    counterclockspiralprint(r, c, arr); 
} 
} 
  
// this code is contributed by krv.

php

= $k; --$i)  
            { 
                echo $arr[$i][$n - 1] , " "; 
                $cnt  ; 
            } 
            $n--; 
        } 
  
        if ($cnt == $total) 
            break; 
  
        // print the first row  
        // from the remaining rows  
        if ($l < $n) { 
            for ($i = $n - 1; $i >= $l; --$i)  
            { 
                echo $arr[$k][$i] , " "; 
                $cnt  ; 
            } 
            $k  ; 
        } 
    } 
} 
  
// driver code 
global $r,$c; 
    $arr = array(array( 1, 2, 3, 4 ), 
                 array( 5, 6, 7, 8 ), 
                 array( 9, 10, 11, 12 ), 
                 array( 13, 14, 15, 16 )); 
echo counterclockspiralprint($r, $c, $arr); 
  
// this code is contributed by anuj_67. 
?>

输出:

1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7  

时间复杂度:o(mn)

替代实现:

python

# python3 implementation to print 
# the counter clock wise 
# spiral traversal of matrix 
   
#function to print matrix in counterclockwise 
def counterclockspiralprint(matrix):  
    size = len(matrix) 
    flag = 0
    k, i = 0, size 
  
    # print all layers one by one 
    while(i > 0): 
  
        # print first column of current layer 
        for j in range(flag,i): 
            print(matrix[j][k], end = ' ') 
        i = i - 1 
        k = j 
  
        # print bottom row and last column 
        # of current layer 
        if (i > 0): 
            for j in range(size - i,i   1): 
                print(matrix[k][j], end = ' ') 
            for j in range(k-1,size-i-2,-1): 
                print(matrix[j][k], end = ' ') 
        else: break
        k = j 
        i = i-1
  
        # print top row of current layer    
        if (i > 0):  
            for j in range(i,size - i-2,-1): 
                print(matrix[k][j], end = ' ') 
            k,i = k 1,i 1
            flag = flag   1
        else: break
       
# driver code 
arr = [ [ 1, 2, 3, 4 ], 
        [ 5, 6, 7, 8 ], 
        [ 9, 10, 11, 12 ], 
        [ 13, 14, 15, 16 ] ] 
   
counterclockspiralprint(arr) 
   
# this code is contributed by srihari r