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]
 1 package GooglePhone;
 2
 3 import java.util.Arrays;
 4
 5 public class PrintMatrix {
 6
 7     static void print(int rows, int cols) {
 8         int limit = rows * cols;
 9         int T = rows * 2; // cycle
10         for (int i=1; i<=rows; i++) {
11             int[] curRow = new int[cols];
12             int k = 0;
13             int index = 0;
14             while (k * T + i <= limit || k * T + T - i + 1 <= limit) {
15                 if (k * T + i <= limit) curRow[index++] = k * T + i;
16                 if (k * T + T - i + 1 <= limit) curRow[index++] = k * T + T - i + 1;
17                 k++;
18             }
19             System.out.println(Arrays.toString(curRow));
20         }
21
22     }
23     /**
24      * @param args
25      */
26     public static void main(String[] args) {
27         // TODO Auto-generated method stub
28         print(4, 5);
29         //print(3, 5);
30     }
31
32 }
时间: 2024-10-19 09:43:26

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: left child should appear on the left column of root, and right child should appear on the right of root. Example: a b c d e f z g h i j 这道题若能发现inorde

G面经prepare: Sort String Based On Another

Given a sorting order string, sort the input string based on the given sorting order string. Ex sorting order string -> dfbcae Input string -> abcdeeabc output -> dbbccaaee 法一:Comparable sample Input: String order = "dfbcae"; String str

G面经Prepare: Longest All One Substring

give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping 这是一个简单版本of LC 424 Longest Repeating Character Replacement 又是Window, 又是Two Pointers Window还是采用每次都try to update left使得window valid, 每次都检查最大window 1 package

G面经prepare: Friends Recommendation

想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友 一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁给A,我说D,因为他是BC的共同好友,而E只是B的好友,到这我才明白干啥,就是给一个图和里面的一个节点A,用bfs从A出发,找出第二层中indegree度数最大节点 用HashMap<Character, HashSet<Character>>来建图 用HashMap<Chara

G面经prepare: Straight Partition of A Deck of Cards

Define “Straight” as 5 cards with consecutive numbers. Determine if the deck can be fully divided into sets of “Straight”. Example: 1, 2, 3, 4, 4, 5, 5, 6, 7, 8 -> True 这个是用一个hashtable,key是数字,value是出现次数 然后遍历原数组,每一个数字都把hash里从自己开始往后5个color数都-1,如果发现缺数则说

G面经prepare: Chucked Palindrome

给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起,(vo) (l) (vo),那么它是个palindrome.求实现返回最大的chunk 数量. 比如aaaaaa可以是(aaa)(aaa), 但是最大chunk数量应该是(a)(a)(a)(a)(a)(a)为6 这就是一个greedy的问题,从string的两边开始,用i和j记录当前scan到的位

G面经Prepare: Search word delete sequence in dictionary

给一个单词一个字典,每次删除单词里任一个字母直到剩下一个字母,形成一个序列,比如office->offce->ofce->ofc->oc->c.问是否字典里存在一个这种序列 1 package checkDictExistSequence; 2 import java.util.*; 3 4 public class Solution { 5 HashSet<String> dict = new HashSet<String>(); 6 7 publi

G面经Prepare: Valid Preorder traversal serialized String

1 求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 2 A) 9 3 4 # # 1 # # 2 # 6 # #是对的,因为表示 3 9 4 / 5 3 2 6 / \ 7 4 1 6 8 B ) 9 3 4 # # 1 # #就是错的,因为无法反构造回一棵树 我觉得可以在字符串里找"n##"这种结构(对应tree里两个children都是Null的叶节点),找到之后

G面经prepare: set difference

给你A{1,2,3,4,4,5}, B{2,4},求A-B={1,3,4,5},很简单. visit 1 只用一个HashMap 1 package TwoSets; 2 import java.util.*; 3 4 public class Solution { 5 public ArrayList<Integer> findDiff(int[] arr1, int[] arr2) { 6 ArrayList<Integer> res = new ArrayList<In