G面经Prepare: Print Zigzag Matrix
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的更多相关文章
- 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: ...
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- 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 ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- 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 ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
随机推荐
- JS语法转换-ES6转ES5
1.使用Babel转码 全局安装 npm install -g babel-cli 布局安装 npm install -g babel-cli --save-dev Babel的配置文件是.babel ...
- manjaro安装及设置
因我的笔记本(联想的拯救者)昨晚打开后什么都还没做就被更新系统“抢走”了画面导致按什么都不管用 所以就想起能不能不用win系统,都知道linux比win稳定,so....就找到了manjaro.以下是 ...
- pyqt pyside QLineEdit 重写键盘事件
pyqt pyside QLineEdit 重写键盘事件 def keyPressEvent(self, event): if (event.modifiers() & QtCore.Qt.S ...
- Typescript高级类型与泛型难点详解
最近做的TS分享,到了高级类型这一块.通过琢磨和实验还是挖掘出了一些深层的东西,在此处做一下记录,也分享给各位热爱前端的小伙伴. 其实在学习TS之前就要明确以下几点: 1. typescrip ...
- 2017 CCPC Qinhuangdao Site
A. Balloon Robot 假设机器人$0$时刻位于$0$号位置,那么每个气球所需的时间为$(s_a-b)\bmod m$. 将所有气球按这个时间排序,枚举每个气球的时间作为偏移量,得出最优解即 ...
- C++ 控制台推箱子小游戏
// 游戏菜单.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #in ...
- MongDB 数据结构
Object ID :Documents 自生成的 _id String: 字符串,必须是utf-8 Boolean:布尔值,true 或者false (这里有坑哦~在我们大Python中 True ...
- Jmeter学习系列----2 录制脚本
虽然专业的自动化测试人员都不会选择录制脚本的方式来进行自动化脚本的编写,但是,我们作为初学者还是可以学习一下怎么利用工具来进行脚本的录制,体验一下自动化工具的效率,下面,具体讲下如何使用jmeter自 ...
- MBA 拓展训练总结
1. 拓展训练,大家绑腿跑 沟通时间极短, 规则制定不完善, 对方'王者队'沟通很好, 女队练习的同时, 男队边观看边练习, 效率很高, 由于之前王者输的比较多, 总结很多, 所以执行力也占优了, 我 ...
- [LeetCode] Number of Matching Subsequences 匹配的子序列的个数
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...