首页 > 试题广场 >

二维数组打印

[编程题]二维数组打印
  • 热度指数:11191 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

有一个二维数组 (n*n) ,写程序实现从右上角到左下角沿主对角线方向打印。(注:主对角线方向为从左上角指向右下角这一斜线的方向)

给定一个二位数组 arr 及题目中的参数 n ,请返回结果数组。

数据范围:

示例1

输入

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

输出

[4,3,8,2,7,12,1,6,11,16,5,10,15,9,14,13]
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        int row=0,col=n-1;
        vector<int> vec;
        while(row<n){
            int i=row,j=col;
            while(i<n&&j<n){
                vec.push_back(arr[i][j]);
                i++;
                j++;
            }
            if(j==n&&i<n)
                col--;
            if(i==n)
                row++;   
        }
        return vec;
    }
};

发表于 2016-08-17 21:14:51 回复(7)
import java.util.*;

public class Printer {
    public int[] arrayPrint(int[][] arr, int n) {
        // write code here
        int[] A = new int[n*n];
        int p = 0;
        for(int p1=n-1;p1>-1;p1--){
            for(int p2=0;p2<n-p1;p2++){
                A[p] = arr[p2][p1+p2];
                A[n*n-1-p] = arr[n-1-p2][n-1-p1-p2];
                p++;
            }  
        }
        return A;
    }
}

发表于 2016-08-25 11:17:40 回复(0)
之前做过一个巧妙的打印方法。
但看到题就是想到最直接的方法
import java.util.*;

public class Printer {
    public int[] arrayPrint(int[][] arr, int n) {
        // write code here
     //   if(n<1) return -1;
  //打印上三角包括中间00-nn的对角线
        int [] out=new int[n*n];
        int index=0;
        for(int j=n-1;j>=0;j--){
            int k=j;
            for(int i=0;i<n-j;i++){
                out[index++]=arr[i][k++];
            }
        }
   //打印剩下的下三角
        for(int i=1;i<n;i++){
            int k=i;
            for(int j=0;j<n-i;j++){
                out[index++]=arr[k++][j];
            }           
        }
        return out;
    }
}

编辑于 2017-05-01 13:17:12 回复(1)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        vector<int> result;         for(int i=0;i<n;i++)             for(int j=0;j<=i;j++)                 result.push_back(arr[j][n-1-i+j]);         for(int i=1;i<n;i++)             for(int j=0;j<n-i;j++)                 result.push_back(arr[i+j][j]);         return result;
    }
};

发表于 2017-10-16 01:03:35 回复(0)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        int j=n-1,k=j,i=0;
        vector<int> a;
        while(j>=0){
            while(j<n)
                a.push_back(arr[i++][j++]);
            k--; j=k; i=0;
        }
        i=k=1,j=0;
        while(i<n){
            while(i<n)
                a.push_back(arr[i++][j++]);
            k++; i=k; j=0;
        }
        return a;
    }
};
//
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        vector<int> a;
        int i,j,k;
        for(j=n-1;j>=0;j--){
            i=0,k=j;
            while(k<n)
                a.push_back(arr[i++][k++]);
        }
        for(i=1;i<n;i++){
            j=0,k=i;
            while(k<n)
                a.push_back(arr[k++][j++]);
        }
        return a;
    }
};

编辑于 2017-07-24 12:05:45 回复(0)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        // write code here
        vector <int>ans;
        for(int j=n-1;j>=0;j--)
        {
            int i=0;
            int m =j;
            while(m<=n-1)
                {
                ans.push_back(arr[i][m]);
                i++;
                m++;
            }
        }
        
        for(int i=1;i<=n-1;i++)
        {
            int j=0;
            int m=i;
            while(m<=n-1)
                {
                ans.push_back(arr[m][j]);
                m++;
                j++;
            }
        }
        return ans;
    }
};

发表于 2016-08-25 16:06:23 回复(1)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        // write code here
        vector<int> pri;
        for(int j=n-1;j>=0;j--)
        {
            for(int i=0;i<n-j;i++)
            {
                pri.push_back(arr[i][j+i]);
            }
        }
        for(int i=1;i<n;i++)
        {
            for(int j=0;j<n-i;j++)
            {
                pri.push_back(arr[i+j][j]);
            }
        }
        return pri;
    }
};

发表于 2019-04-09 19:45:26 回复(0)
class Printer 
{
public:
    vector<int> arrayPrint(vector<vector<int>> arr, int n) 
    {
        for (int i = n - 1; i >= 1 - n; i--)
        {
            if (i >= 0)
            {
                help(arr, 0, i, n);
            }
            else
            {
                help(arr, -i, 0, n);
            }
        }
        return _res;
    }
private:
    void help(vector<vector<int>> &arr, int x, int y, int n)
    {
        while (x < n&&y < n)
        {
            _res.push_back(arr[x++][y++]);
        }
    }
private:
    vector<int> _res;
};
发表于 2018-08-24 09:33:45 回复(0)
/* 
题目描述
    有一个二维数组(n*n),写程序实现从右上角到左下角沿主对角线方向打印。
给定一个二位数组arr及题目中的参数n,请返回结果数组。
测试样例:
    [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]],4
返回:
    [4,3,8,2,7,12,1,6,11,16,5,10,15,9,14,13]
*/
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        vector<int> brr;
        for(int i=0;i<2*n-1;i++){
            int x=i<n?0:i-n+1;
            int y=i<n?n-1-i:0;
            for(;y<n&&x<n;y++,x++){
                brr.push_back(arr[x][y]);
            }
        }
        return brr;
    }
};

发表于 2017-09-29 17:58:35 回复(0)
import java.util.*;

public class Printer {
    public int[] arrayPrint(int[][] arr, int n) {
        
    	int [] aa=new int[arr.length*(arr[0].length)];
    	int cout=0;
    	for(int i=0;i<arr.length+arr[0].length-1;i++){ 		
    		for(int j=0;j<arr.length;j++){
    			int x=j+arr[0].length-1-i;
    			if(0<=x&&x<=arr[0].length-1){
    				aa[cout]=arr[j][x];
    				cout++;
    			}
    		}
    	}   	
    	return aa;
    }
}

发表于 2017-09-06 21:49:11 回复(0)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        vector<int>a;
        for(int i=0;i<n;i++)
            {
            a.push_back(arr[0][n-1-i]);
            int y=n-1-i;
            int x=1;
                while(y<n-1)
                    {
                    a.push_back(arr[x][y+1]);
                    x++;
                    y++;
                }
        }
        for(int i=1;i<n;i++)
            {
            a.push_back(arr[i][0]);
            int y=0;
            int x=i+1;
                while(x<=n-1)
                    {
                    a.push_back(arr[x][y+1]);
                    x++;
                    y++;
                }
        }
        return a;
    }
};
发表于 2017-07-24 10:17:26 回复(0)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        vector<int> res;
        int index1=n-1;
        int index2=0;
        while(res.size()<n*n)
        {
            int p=index1;
            int q=index2;
            while(p<n&&q<n)
            {
                res.push_back(arr[q][p]);
                p++;
                q++;
            }
            if(index1>0)
                index1--;
            else
			{
				index1=0;
                index2++;
			}
        }
        return res;
    }
};

发表于 2017-06-21 11:40:46 回复(0)
import java.util.*;

public class Printer {
    public int[] arrayPrint(int[][] arr, int n) {
        // write code here
        int[] result = new int[n * n];
        int row = 0, col = n - 1, index = 0;
        while(row < n) {
            while(row < n && col < n) {
	            result[index++] = arr[row][col];
    	        row++;
                col++;
            }
            if(row < col) {
	            col -= row + 1;
    	        row = 0;                
            } else {
                row -= col - 1;
                col = 0;
            }
        }
        return result;
    }
}

发表于 2017-05-09 22:29:13 回复(0)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        // write code here
        vector<int>ve;
        
        for(int i=n-1;i>=0;i--){
            for(int j=i,k=0;j<n&&k<n;j++,k++){
                ve.push_back(arr[k][j]);
            }
        }
        for(int i=1;i<n;i++){
            for(int j=i,k=0;j<n&&k<n;j++,k++){
                ve.push_back(arr[j][k]);
            }
        }
        return ve;
    }
};

发表于 2017-05-05 22:17:34 回复(0)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        // write code here
        arr=arraytrans(arr);
        vector<int> rel;
        for(int i=0;i<n;i++)
        {
            for(int j=i;j>=0;j--)
            {
                rel.push_back(arr[i-j][j]);
            }
        }
        for(int j=1;j<n;j++)
        {
            for(int i=j;i<n;i++)
            {
                rel.push_back(arr[i][n+j-1-i]);
            }
        }
        return rel;
    }
    vector<vector<int>> arraytrans(vector<vector<int>> arr)
    {
        int ls=arr.size();
        for(int i=0;i<ls;i++)
        {
            for(int j=0;j<ls/2;j++)
            {
                int temp=arr[i][j];
                arr[i][j]=arr[i][ls-j-1];
                arr[i][ls-j-1]=temp;
            }
        }
        return arr;
    }
};

发表于 2017-03-31 12:30:25 回复(0)
  vector<int> ans;
    for(int i=1;i<=n;i++)
    {
        for(int j=0;j<i;j++)
        {
			ans.push_back(arr[j][n-i+j]);
        }
    }
    for(int i=1;i<n;i++)
    {
        for(int j=0;j<=n-1-i;j++)
        {
            ans.push_back(arr[i+j][j]);
        }
    }  
	vector<int>::iterator iter; 
	for (iter=ans.begin();iter!=ans.end();iter++)  
    {  
        cout<<*iter<<endl;  
    } 
	return ans;

发表于 2017-03-21 13:51:19 回复(0)
// 层次遍历 更容易理解
//根节点入队  孩子入队 根节点出队 直至队列为空
import java.util.*;

class Node{
        int i;
        int j;
        Node(int i,int j){
            this.i = i;
            this.j = j;
        }
    }
public class Printer {
    public int[] arrayPrint(int[][] arr, int n) {
        // write code here
        Node s = new Node(0,n-1);
        List<Node> list = new LinkedList<Node>();
        list.add(s);

        int[][] mark = new int[n][n];
        int[] result = new int[n*n];
        result[0] = arr[0][n-1];
        int k =1;
        while(!list.isEmpty()){
            Node temp = list.get(0);
            list.remove(0);
            if (mark[temp.i][temp.j] != 0)
                result[k++] = arr[temp.i][temp.j];
            if (temp.j -1 >=0 && mark[temp.i][temp.j-1]==0){
                list.add(new Node(temp.i,temp.j-1));
                mark[temp.i][temp.j-1]=1;
            }

            if (temp.i +1 <n && mark[temp.i+1][temp.j]==0){
                list.add(new Node(temp.i+1,temp.j));
                mark[temp.i+1][temp.j]=1;
            }
        }
        return result;
    }
}

编辑于 2016-09-09 16:55:14 回复(0)
import java.util.*;

public class Printer {
    public int[] arrayPrint(int[][] arr, int n) {
        // write code here
        int count = 0;
		int times = 1;
		int result[] = new int[n * n];
		for (int i = 0; i < n; ++i) {
			int p = 0;
			int q = n - 1 - i;
			for (int x = 0; x < times; ++x) {
				result[count++] = arr[p][q];
				++p;
				++q;
			}
			++times;
		}

		times = n - 1;
		for (int i = 0; i < n - 1; ++i) {
			int p = i + 1;
			int q = 0;
			for (int x = 0; x < times; ++x) {
				result[count++] = arr[p][q];
				++p;
				++q;
			}
			--times;
		}
		return result;
    }
}

发表于 2016-09-02 16:13:49 回复(0)
class Printer {
public:
    vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        // write code here
        vector<int>A;
        int i,j;
        int k1,k2,k;
        for( i=1;i<=n;i++)//打印右上角及对角线
            {
            k1=0;
            k2=n-i;
            k=i;          //标志个数
            while(k--)
                {
                A.push_back(arr[k1][k2]);//压入数据
                k1++;//下标同时增加
                k2++;
            }
        }
        for( i=1;i<=n-1;i++)//打印左下角
            {
            k1=i;
            k2=0;
            k=n-i;          //标志个数
            while(k--)
                {
                A.push_back(arr[k1][k2]);//压入数据
k1++;//下标同时增加
                k2++;
            }
        }
        return A;
    }
};
发表于 2016-05-18 01:32:42 回复(0)
vector<int> arrayPrint(vector<vector<int> > arr, int n) {
        int row = 0;
        int count = 1;
        vector<int> result;
        for(int i = 1;i<=n;i++)
        {
            for(int r = 0,c = n-count;r<=i-1,c<n;r++,c++)
            {
                result.push_back(arr[r][c]);
               // cout<<r<<","<<c<<endl;
            }
            count ++;
        }
        if(n<=1)
        return result;
        count = n-1;
        for(int i =n-1;i>=1;i--)
        {
           for(int r = n-count,c = 0;r<=n-1,c<=i-1;c++,r++)
           result.push_back(arr[r][c]);
           count --;
        }
        return result;    
}

发表于 2015-10-22 14:40:44 回复(0)

热门推荐

通过挑战的用户

查看代码