鱼C论坛

 找回密码
 立即注册
查看: 2117|回复: 0

[学习笔记] Google foobar challenge

[复制链接]
发表于 2019-8-8 01:56:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Seawolf 于 2019-8-8 02:01 编辑

一个比较有名的Google coding challenge,一共5个level,完成三个可以得到Google的interview,不是每个人都有机会,需要拿到link才可以参与,完成level 2 以后可以把link分享给他人。

level 1 -5 难度会逐渐增加,根据难度的不同每个题目都有不同的时间限制,代码可以用python 也可以用java,本人选择的是java,下面是一个level 1 的题目,due day 是 48 hours。

  1. The cake is not a lie!
  2. ======================

  3. Commander Lambda has had an incredibly successful week: she completed the first test run of her LAMBCHOP doomsday device, she captured six key members of the Bunny Rebellion, and she beat her personal high score in Tetris. To celebrate, she's ordered cake for everyone - even the lowliest of minions! But competition among minions is fierce, and if you don't cut exactly equal slices of cake for everyone, you'll get in big trouble.

  4. The cake is round, and decorated with M&Ms in a circle around the edge. But while the rest of the cake is uniform, the M&Ms are not: there are multiple colors, and every minion must get exactly the same sequence of M&Ms. Commander Lambda hates waste and will not tolerate any leftovers, so you also want to make sure you can serve the entire cake.

  5. To help you best cut the cake, you have turned the sequence of colors of the M&Ms on the cake into a string: each possible letter (between a and z) corresponds to a unique color, and the sequence of M&Ms is given clockwise (the decorations form a circle around the outer edge of the cake).

  6. Write a function called solution(s) that, given a non-empty string less than 200 characters in length describing the sequence of M&Ms, returns the maximum number of equal parts that can be cut from the cake without leaving any leftovers.

  7. Languages
  8. =========

  9. To provide a Python solution, edit solution.py
  10. To provide a Java solution, edit Solution.java

  11. Test cases
  12. ==========
  13. Your code should pass the following test cases.
  14. Note that it may also be run against hidden test cases not shown here.

  15. -- Python cases --
  16. Input:
  17. solution.solution("abcabcabcabc")
  18. Output:
  19.     4

  20. Input:
  21. solution.solution("abccbaabccba")
  22. Output:
  23.     2

  24. -- Java cases --
  25. Input:
  26. Solution.solution("abcabcabcabc")
  27. Output:
  28.     4

  29. Input:
  30. Solution.solution("abccbaabccba")
  31. Output:
  32.     2
复制代码


首次尝试,通过了7个case:

  1.        
  2.         public class Solution {
  3.                
  4.                 public static int solution(String x) {
  5.                        
  6.                         int len = x.length();
  7.                        
  8.                         int count = 1;
  9.                        
  10.                         String a = "";
  11.                        
  12.                         String b = "";
  13.                        
  14.                         while(len >= 1) {
  15.                                
  16.                                 a = x.substring(0, len/2);
  17.                                
  18.                                 b = x.substring(len/2, len);
  19.                                
  20.                                 if(a.equals(b)) {
  21.                                        
  22.                                         count = count * 2;
  23.                                
  24.                                 }
  25.                                        
  26.                                 len = len /2;
  27.                         }
  28.                        
  29.                         return count;
  30.                
  31.                 }
  32.                
  33.                 public static void main(String[] args) {
  34.                        
  35.                         int c = solution("abccbaabccba");
  36.                        
  37.                         System.out.println(c);
  38.                 }
  39.         }

复制代码


但是这个代码有个问题,就是如果input全是相同的字母,则不能通过,于是稍作修改以后通过了8个case


  1.        
  2.         public class Solution {
  3.                
  4.                 public static int solution(String x) {
  5.                        
  6.                         int len = x.length();
  7.                        
  8.                         int count = 1;
  9.                        
  10.                         int count1 = 0;
  11.                        
  12.                         String a = "";
  13.                        
  14.                         String b = "";
  15.                        
  16.                         for(int i = 0; i < len; i++) {
  17.                                
  18.                                 if(x.substring(i, i+1).equals(x.substring(0, 1))){
  19.                                        
  20.                                         count1 ++;
  21.                                 }
  22.                         }
  23.                        
  24.                         if(count1 == len) {
  25.                                
  26.                                 return count1;
  27.                         }
  28.                        
  29.                         while(len >= 1) {
  30.                                
  31.                                 a = x.substring(0, len/2);
  32.                                
  33.                                 b = x.substring(len/2, len);
  34.                                
  35.                                 if(a.equals(b)) {
  36.                                        
  37.                                         count = count * 2;
  38.                                
  39.                                 }
  40.                                        
  41.                                 len = len /2;
  42.                         }
  43.                        
  44.                         return count;
  45.                
  46.                 }
  47.                
  48.                 public static void main(String[] args) {
  49.                        
  50.                         int c = solution("abccbaabccba");
  51.                        
  52.                         System.out.println(c);
  53.                 }
  54.         }

复制代码



现在这个code 还有最后一个问题,就是当string的长度是奇数的时候无法通过,主要是设计思路还是有缺陷,于是打破之前的思路重新来写。

code:

  1. public class Solution {
  2.        
  3.         public static int solution(String x) {
  4.                
  5.                 int len = x.length();
  6.                
  7.                 int ans = 0;
  8.                
  9.                 for(int i = len; i >= 1; i --) {
  10.                        
  11.                         int n = len/i;
  12.                        
  13.                         if(len == n* i) {
  14.                                
  15.                                 boolean t = true;
  16.                                
  17.                                 String sub = x.substring(0, n);
  18.                                
  19.                                 for(int j = 1; j <i; j++) {
  20.                                        
  21.                                         if(!x.substring(j*n, j*n + n).equals(sub)) {
  22.                                                
  23.                                                 t= false;
  24.                                                
  25.                                                 break;
  26.                                                
  27.                                         }
  28.                                        
  29.                                 }
  30.                                
  31.                                 if(t == true) {
  32.                                        
  33.                                         ans = i;
  34.                                        
  35.                                         break;
  36.                                 }
  37.                         }
  38.                        
  39.                        
  40.                 }
  41.         
  42.         return ans;
  43.        
  44.         }
  45. }
复制代码


最终通过10个case,level 1 pass,记录一下。

8.jpg


9.jpg

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 16:28

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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