For instance, give row = 4, col = 5, print matrix in zigzag order like:

[1, 8, 9, 16, 17]
[2, 7, 10, 15, 18]
[3, 6, 11, 14, 19]
[4, 5, 12, 13, 20]
 package GooglePhone;

 import java.util.Arrays;

 public class PrintMatrix {

     static void print(int rows, int cols) {
int limit = rows * cols;
int T = rows * 2; // cycle
for (int i=1; i<=rows; i++) {
int[] curRow = new int[cols];
int k = 0;
int index = 0;
while (k * T + i <= limit || k * T + T - i + 1 <= limit) {
if (k * T + i <= limit) curRow[index++] = k * T + i;
if (k * T + T - i + 1 <= limit) curRow[index++] = k * T + T - i + 1;
k++;
}
System.out.println(Arrays.toString(curRow));
} }
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
print(4, 5);
//print(3, 5);
} }

G面经Prepare: Print Zigzag Matrix的更多相关文章

  1. U面经Prepare: Print Binary Tree With No Two Nodes Share The Same Column

    Give a binary tree, elegantly print it so that no two tree nodes share the same column. Requirement: ...

  2. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  3. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  4. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

  5. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  6. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  7. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  8. G面经prepare: Android Phone Unlock Pattern

    1 2 3 4 5 6 7 8 9 只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9 但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9 ...

  9. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

随机推荐

  1. 树莓派做coolpy服务器

    安装前需要了解的 1. coolpy是一个基于NodeJS的物联网平台(官网http://icoolpy.com). 注:国内物联网平台有乐联网,yeelink等,但只有coolpy是开源的. 2. ...

  2. Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学

    Sasha and Interesting Fact from Graph Theory n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m} 然后就 ...

  3. 转:mongoDB 修改 ulimit

    转自:http://blog.yucanlin.cn/2015/03/23/mongodb-%E4%BF%AE%E6%94%B9-ulimit/ mongoDB 修改 ulimit 一切都源于mong ...

  4. docker+fastdfs+nginx 实现分布式大文件存储系统以及视频缓存播放

    废话不多说,直接开撸 首先是一些准备工作: 1.关闭防火墙 service iptables stop --- fastdfs虽然在docker部署,但是使用的是主机网络,所以关闭防火墙. 2  下载 ...

  5. C# 堆栈(Stack)和队列(Queue)

    一.什么是堆?(Heap)      堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收.      是程序运行期 ...

  6. 最近公共祖先(LCA)的三种求解方法

    转载来自:https://blog.andrewei.info/2015/10/08/e6-9c-80-e8-bf-91-e5-85-ac-e5-85-b1-e7-a5-96-e5-85-88lca- ...

  7. 2017 ACM Jordanian Collegiate Programming Contest

    A. Chrome Tabs 当$n=1$时答案为$0$,当$k=1$或$k=n$时答案为$1$,否则答案为$2$. #include<cstdio> int T,n,k; int mai ...

  8. 关于UTF-8和GBK编码的转换

    $oldname=mb_convert_encoding($_POST['oldname'], "GBK" , "UTF-8");//将变量转码为GBK,已知原 ...

  9. spring boot中jsp解析c标签方法

    pro.xml中添加jstl标签 <dependency><groupId>javax.servlet</groupId><artifactId>jst ...

  10. PAT甲级1103 Integer Factorization【dfs】【剪枝】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 题意: 给定一个数n,要求从1~n中找 ...