楼主: wrj5518
跳转到指定楼层
上一主题 下一主题
收起左侧

[CareerCup] 【第三轮】6.16-6.22 CareerCup 1.6

🔗
jaly50 2014-6-23 20:56:10 | 只看该作者
全局:
【解题思路】
   顺时针转,逆时针转,写了两个方法。
【时间复杂度】o(n^2)
【空间复杂度】o(1)
【gist link】
https://gist.github.com/jaly50/5ac7c2008e133dd765ca
---------------OPTional,如果觉得test case比较好,欢迎写出来分享----------------------
【test case】

The original image is:
  1  2  3  4
  5  6  7  8
  9 10 11 12
13 14 15 16

Clockwise:
13  9  5  1
14 10  6  2
15 11  7  3
16 12  8  4

CounterClockwise:
  1  2  3  4
  5  6  7  8
  9 10 11 12
13 14 15 16
回复

使用道具 举报

🔗
tonygxxx1212 2014-6-24 07:39:13 | 只看该作者
全局:

【解题思路】
  Approach 1: layered rotate (top-right-bottom-left), the subscripts of matrix are easy to be wrong
  Approach 2: transpose + flip according to vertical middle line
【时间复杂度】
  O(N^2)
【空间复杂度】
  O(1)
【gist link】
https://gist.github.com/xun-gong/78ced3e7837e5162bcde
回复

使用道具 举报

🔗
ivycheung1208 2014-6-24 09:59:33 | 只看该作者
全局:
本帖最后由 ivycheung1208 于 2014-6-24 13:59 编辑

【解题思路】
1. 4 pixel in a group, N^2/4 times of replacement (layer by layer)i = 0 -> N/2, j = i -> N-i-1 {
temp = a[j];
a[j] = a[N-j-1];
a[N-j-1] = a[N-i-1][N-j-1];
a[N-i-1][N-j-1] = a[j][N-i-1];
a[j][N-i-1] = temp;
}
2. transpose + flip
【时间复杂度】
O(N^2) to access each element in the matrix
【空间复杂度】
O(1)
【gist link】
先占坑……
【test case】
integer matrix N = 0 - 5

难点:multidimensional array/vector



补充内容 (2014-6-24 14:01):
不能编辑了…gist link在这里
https://gist.github.com/27e9ada3a324c3adad4f
回复

使用道具 举报

🔗
心焰 2014-6-26 03:27:42 | 只看该作者
全局:

【解题思路】
1.transpose
2.switch columns

【时间复杂度】
O(N^2)

【空间复杂度】
O(1)
【gist link】
https://github.com/FinalF/Carrer ... matrixRotation.java
回复

使用道具 举报

🔗
sanguine 2014-6-26 18:11:31 | 只看该作者
全局:
asdw3276 发表于 2014-6-19 01:57
【解题思路】 leetcode原题,通过观察我们可以发现旋转是4个一组进行旋转,所以只需要循环1/4个矩阵就可以 ...

the algorithm is wrong

because if it's a 4*4 matrix

in you solution, i<n/2 and j<=n/2
which means, the matrix[0][2] will be calculate twice.
回复

使用道具 举报

🔗
sanguine 2014-6-26 21:56:07 | 只看该作者
全局:
solution 1
Idea: same to the one on the book: We perform such a swap on each layer, starting from the outermost layer and working our way inwards
Time Complexity: O(n^2)
Space Complexity: O(1)

solution 2
Idea: Each rotate, we need to rotate four items in the matrix. So we just need to traverse from int[0][0] to int[n/2][n/2], Remember if num is even, avoid to rotate twice!
Time Complexity: O(n^2)
Space Complexity: O(1)

Code Link is here: http://www.jyuan92.com/post-319
回复

使用道具 举报

🔗
donnice 2014-6-27 03:27:46 | 只看该作者
全局:
本帖最后由 donnice 于 2014-6-27 04:46 编辑

【解题思路】
说白了就是把第i行换成第i列,把第j列换成第j行
所以原来的矩阵输出方式:
for(int i = 0; i<n;i++)
    for(int j = 0; j<n;j++)
        System.out.print(a[j])
换成
for(int i = 0; i<n;i++)
    for(int j = n-1; j>=0;j--)
        System.out.print(a[j])
【时间复杂度】
O(N^2)

【空间复杂度】
O(1)
【gist link】
无gist……贴代码吧
import java.util.*;
public class matrix{

public static void Rotation(int[][] d){
  int k = d.length;
  for(int a = 0; a<k; a++){
   for(int b = k-1; b>=0;b--){
    System.out.print(d[b][a]+" ");
   }
   System.out.println();
  }
}
public static void main(String[] args){
  Scanner sc = new Scanner(System.in);
  System.out.print("请输入矩阵大小(0~9)");
  int n = sc.nextInt();
  int[][] m = new int[n][n];
  for(int i = 0; i<n; i++){
   for(int j = 0; j<n; j++){
    m[i][j] = i;
    System.out.print(m[i][j]+" ");
   }
   System.out.println();
  }
  System.out.println();
  Rotation(m);  
}
}
回复

使用道具 举报

🔗
donnice 2014-6-27 04:46:57 | 只看该作者
全局:
【解题思路】
说白了就是把第i行换成第i列,把第j列换成第j行
所以原来的矩阵输出方式:
for(int i = 0; i<n;i++)
     for(int j = 0; j<n;j++)
         System.out.print(a[j])
换成
for(int i = 0; i<n;i++)
     for(int j = n-1; j>=0;j--)
         System.out.print(a[j])
【时间复杂度】
O(N^2)

【空间复杂度】
O(1)
【gist link】
无gist link,贴代码在此
import java.util.*;

public class matrix{
       
        public static void Rotation(int[][] d){
                int k = d.length;
                for(int a = 0; a<k; a++){
                        for(int b = k-1; b>=0;b--){
                                System.out.print(d[b][a]+" ");
                        }
                        System.out.println();
                }
        }

        public static void main(String[] args){
                Scanner sc = new Scanner(System.in);
                System.out.print("请输入矩阵大小(0~9)");
                int n = sc.nextInt();
                int[][] m = new int[n][n];
                for(int i = 0; i<n; i++){
                        for(int j = 0; j<n; j++){
                                m[i][j] = i;
                                System.out.print(m[i][j]+" ");
                        }
                        System.out.println();
                }
                System.out.println();
                Rotation(m);               
        }
}
回复

使用道具 举报

🔗
donnice 2014-6-27 04:47:53 | 只看该作者
全局:
donnice 发表于 2014-6-27 04:46
【解题思路】
说白了就是把第i行换成第i列,把第j列换成第j行
所以原来的矩阵输出方式:

本楼和37楼请都删掉,谢谢
回复

使用道具 举报

🔗
asdw3276 2014-7-1 12:38:59 | 只看该作者
全局:
sanguine 发表于 2014-6-26 18:11
the algorithm is wrong

because if it's a 4*4 matrix

thank you! you are right and i have updated my solution~  
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表