📣 独立日限时特惠: VIP通行证立减$68
123
返回列表 发新帖
跳转到指定楼层
上一主题 下一主题
收起左侧

gg intern 02/09面经

🔗
bobzhang2004 2016-3-4 08:44:34 | 只看该作者
全局:
第二题解法,只是这个是flower and garden
  1. ublic class FlowersInGarden {

  2.         public static void main(String[] args) {
  3.                 char[][] matrix = {{'f', 'x', 'x', 'w', 'f'},
  4.                                      {'f', 'f', 'x' ,'x' ,'x'},
  5.                                      {'x', 'x', 'f', 'w', 'f'},
  6.                                      {'f', 'f', 'x', 'w', 'x'}};
  7.                 maxFlowersInGarden(matrix);
  8.         }
  9.        
  10.         public static int maxFlowersInGarden(char[][] matrix) {
  11.                 if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
  12.                         return 0;
  13.                 }
  14.                 int m = matrix.length;
  15.                 int n = matrix[0].length;
  16.                
  17.                 int[][] res = new int[m][n];
  18.                 for (int i = 0; i < m; i++) {
  19.                         int count = 0;
  20.                         int j = 0;
  21.                         int start = 0;
  22.                         while (j < n) {
  23.                                 if (matrix[i][j] == 'f') {
  24.                                         count++;
  25.                                 } else if (matrix[i][j] == 'w') {
  26.                                         for (int k = start; k < j; k++) {
  27.                                                 res[i][k] = count;
  28.                                         }
  29.                                         start = j + 1;
  30.                                         count = 0;
  31.                                 }
  32.                                 j++;
  33.                         }
  34.                         for (int k = start; k < j; k++) {
  35.                                 res[i][k] = count;
  36.                         }
  37.                 }
  38.                 for (int j = 0; j < n; j++) {
  39.                         int count = 0;
  40.                         int i = 0;
  41.                         int start = 0;
  42.                         while (i < m) {
  43.                                 if (matrix[i][j] == 'f') {
  44.                                         count++;
  45.                                 } else if (matrix[i][j] == 'w') {
  46.                                         for (int k = start; k < i; k++) {
  47.                                                 res[k][j] += count;
  48.                                         }
  49.                                         start = i + 1;
  50.                                         count = 0;
  51.                                 }
  52.                                 i++;
  53.                         }
  54.                         for (int k = start; k < i; k++) {
  55.                                 res[k][j] += count;
  56.                         }
  57.                 }
  58.                 for (int i = 0; i < m; i++) {
  59.                         for (int j = 0; j < n; j++) {
  60.                                 if (matrix[i][j] == 'f') {
  61.                                         res[i][j] -= 1;
  62.                                 }
  63.                         }
  64.                 }
  65.                 int max = 0;
  66.                 for (int i = 0; i < m; i++) {
  67.                         for (int j = 0; j < n; j++) {
  68.                                 System.out.print(res[i][j] + " ");
  69.                                 max = Math.max(max, res[i][j]);
  70.                         }
  71.                         System.out.println();
  72.                 }
  73.                
  74.                
  75.                 return max;
  76.         }
  77. }
复制代码

评分

参与人数 1大米 +40 收起 理由
夏虫不知雪花 + 40 感谢分享!

查看全部评分

回复

使用道具 举报

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

本版积分规则

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