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中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
随机推荐
- ionic2中使用极光IM的WebSDK实现即时聊天
本文主要介绍如何在ionic项目中集成极光IM的WebSDK,详细文档可参考官方介绍. 一.准备 1. 注册激光账号,进入开发者服务页面创建应用. 2. 创建应用后须完成对应平台的推送设置,进行应用或 ...
- 通过linux版本的lr agent提示找不到web_reg_save_param_ex函数
Action.c(5): Error: C interpreter run time error: /tmp/brr_TSBgR2/netdir/E/lrscript/aaa/Action.c (5) ...
- Codeforces 375B Maximum Submatrix 2 (DP)
<题目链接> 题目大意:给出一个01矩阵,行与行之间可以互换位置,问能够得到最大的全1矩阵的面积. #include <bits/stdc++.h> using namespa ...
- 解决ubuntu下firefox无法在线播放音频和视频的问题
原因 Ubuntu 为了规避专利和版权问题,很多东西没有预装,比如音视频解码器AAC. 那么为什么明明 Ubuntu 上没有AAC解码器, Chrome 却可以正常播放呢,自然的想法是 Chrome ...
- C# 堆栈(Stack)和队列(Queue)
一.什么是堆?(Heap) 堆是无序的,是一片不连续的内存域,由用户自己来控制和释放,如果用户自己不释放的话,当内存达到一定的特定值时,通过垃圾回收器(GC)来回收. 是程序运行期 ...
- Understanding HBase and BigTable
Hbase is a distributed data storage systems. A Bigtable is spare , distributed , persistent multidim ...
- session 存到memcache里
web服务器的php session都给memcached ,这样你不管分发器把 ip连接分给哪个web服务器都不会有问题了,配置方法很简单,就在php的配置文件内 增加一条语句就可以了,不过前提你需 ...
- VB封装的WebSocket模块,拿来即用
一共就下面的两个模块,调用只使用到mWSProtocol模块,所有调用函数功能简单介绍一下: 建立连接后就开始握手,服务端用Handshake()验证,如果是客户端自己发送握手封包接收数据,先用Ana ...
- c#常用数值范围汇总
short.MaxValue 32767 short.MinValue -32768 int.MaxValue 2147483647 int.MinValue -2147483648 long.Max ...
- __x__(26)0907第四天__文档流_网页最底层
文档流 处在网页的最底层,表示的是一个页面中的位置. 创建的元素,默认都处于文档流中. 元素在文档流中的特点 块元素 在文档流中独占一行. 自上而下排列. 宽度默认占父元素的 100%,width=& ...