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. [转] Brook 搭建教程

    https://www.jiongjun.cc/technology/500.html 在搭建 brook 代理之前,首先要求你要有一台国外 vps,关于国外 vps 选择,可以参考这篇:推荐几款国外 ...

  2. 【转】Oracle imp 总是不停地重复闪烁

    http://blog.itpub.net/7282477/viewspace-1003160/ 在dos下执行: imp username/password buffer=1000000 file= ...

  3. 咸鱼入门到放弃9--jsp中使用的JavaBean

    一.什么是JavaBean JavaBean是一个遵循特定写法的Java类,它通常具有如下特点: 这个Java类必须具有一个无参的构造函数 属性必须私有化. 私有化的属性必须通过public类型的方法 ...

  4. 《团队作业第二周》五小福团队作业——UNO

    <团队作业第二周>五小福团队作业--UNO 一.修改完善上周提交的需求规格说明书 THE FIRST改变 首先:我们组的博客无小组分工及占比,这是第一个问题,当时我们在写博客的时候由于很多 ...

  5. 程序员之路:python3+PyQt5+pycharm桌面GUI开发

    http://blog.sina.com.cn/s/blog_989218ad0102wz1k.html 先看效果: 图 1 没错,学过C#的同学应该很熟悉这个界面,按钮风格和界面风格很相似,万万没想 ...

  6. Fiddler 教程---小坦克

    协议. Fiddler无论对开发人员或者测试人员来说,都是非常有用的工具 Fiddler的工作原理 Fiddler 是以代理web服务器的形式工作的,它使用代理地址:127.0.0.1, 端口:888 ...

  7. centos安装svn并创建版本库配置用户分组权限

    1.设置aliyun安装源// 本步骤非必须, 使用aliyun安装源后, 执行yum update速度明显提升 wget -O /etc/yum.repos.d/CentOS-Base.repo h ...

  8. springboot2.0以后WebMvcConfigurationSupport代替WebMvcConfigurationAdapter

    1:WebMvcConfigurationSupport代替WebMvcConfigurationAdapter https://blog.csdn.net/wilsonsong1024/articl ...

  9. mobile_轮播图_style_left 版本

    mobile 轮播图 小圆点逻辑(排他) 1. 统一给所有 span 元素加 class=""; 2. 切换到谁,谁的 class="active"; 移动端轮 ...

  10. react_app 项目开发 (6)_后台服务器端-node

    后台服务器端 负责处理前台应用提交的请求,并向前台返回 json 数据 前台应用 负责 展现数据与用户交互 发 ajax 请求与后台应用交互 yarn add axios /src/api/ajax. ...