鱼C论坛

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

[学习笔记] Google foobar challenge

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

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

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

x
废话不多说,直接上level 2 question

  1. Numbers Station Coded Messages
  2. ==============================

  3. When you went undercover in Commander Lambda's organization, you set up a coded messaging system with Bunny Headquarters to allow them to send you important mission updates. Now that you're here and promoted to Henchman, you need to make sure you can receive those messages - but since you need to sneak them past Commander Lambda's spies, it won't be easy!

  4. Bunny HQ has secretly taken control of two of the galaxy's more obscure numbers stations, and will use them to broadcast lists of numbers. They've given you a numerical key, and their messages will be encrypted within the first sequence of numbers that adds up to that key within any given list of numbers.

  5. Given a non-empty list of positive integers l and a target positive integer t, write a function solution(l, t) which verifies if there is at least one consecutive sequence of positive integers within the list l (i.e. a contiguous sub-list) that can be summed up to the given target positive integer t (the key) and returns the lexicographically smallest list containing the smallest start and end indexes where this sequence can be found, or returns the array [-1, -1] in the case that there is no such sequence (to throw off Lambda's spies, not all number broadcasts will contain a coded message).

  6. For example, given the broadcast list l as [4, 3, 5, 7, 8] and the key t as 12, the function solution(l, t) would return the list [0, 2] because the list l contains the sub-list [4, 3, 5] starting at index 0 and ending at index 2, for which 4 + 3 + 5 = 12, even though there is a shorter sequence that happens later in the list (5 + 7). On the other hand, given the list l as [1, 2, 3, 4] and the key t as 15, the function solution(l, t) would return [-1, -1] because there is no sub-list of list l that can be summed up to the given target value t = 15.

  7. To help you identify the coded broadcasts, Bunny HQ has agreed to the following standards:

  8. - Each list l will contain at least 1 element but never more than 100.
  9. - Each element of l will be between 1 and 100.
  10. - t will be a positive integer, not exceeding 250.
  11. - The first element of the list l has index 0.
  12. - For the list returned by solution(l, t), the start index must be equal or smaller than the end index.

  13. Remember, to throw off Lambda's spies, Bunny HQ might include more than one contiguous sublist of a number broadcast that can be summed up to the key. You know that the message will always be hidden in the first sublist that sums up to the key, so solution(l, t) should only return that sublist.

  14. Languages
  15. =========

  16. To provide a Python solution, edit solution.py
  17. To provide a Java solution, edit Solution.java

  18. Test cases
  19. ==========
  20. Your code should pass the following test cases.
  21. Note that it may also be run against hidden test cases not shown here.

  22. -- Python cases --
  23. Input:
  24. solution.solution([1, 2, 3, 4], 15)
  25. Output:
  26.     -1,-1

  27. Input:
  28. solution.solution([4, 3, 10, 2, 8], 12)
  29. Output:
  30.     2,3

  31. -- Java cases --
  32. Input:
  33. Solution.solution({1, 2, 3, 4}, 15)
  34. Output:
  35.     -1,-1

  36. Input:
  37. Solution.solution({4, 3, 10, 2, 8}, 12)
  38. Output:
  39.     2,3
复制代码


这个问题感觉也是比level 1的要简单,居然给了72 hours 有没有搞错!!


  1.        
  2.         public class Solution {
  3.                
  4.                 public static int[] solution(int[] l , int t) {
  5.                        
  6.                         int len = l.length;
  7.                        
  8.                         int result[] = {-1,-1};
  9.                        
  10.                         int total = 0;
  11.                        
  12.                         int start = 0;
  13.                        
  14.                         for(int i = 0 ; i< len; i++) {
  15.                                
  16.                                 total = 0;
  17.                                
  18.                                 for(int j = i; j <len;j++) {
  19.                                        
  20.                                         if(total < t) {
  21.                                                
  22.                                                 if(total == 0) {
  23.                                                        
  24.                                                         start = j;
  25.                                                 }
  26.                                                
  27.                                                 total = total + l[j];
  28.                                         }
  29.                                        
  30.                                         if(total == t){
  31.                                                
  32.                                                 System.out.println(j);
  33.                                                
  34.                                                 System.out.println(j-1);
  35.                                                
  36.                                                 result[0] = start;
  37.                                                
  38.                                                 result[1] = j;
  39.                                                
  40.                                                 return result;
  41.                                                
  42.                                         }
  43.                                        
  44.                                         if(total > t) {
  45.                                                
  46.                                                 j = len;
  47.                                                
  48.                                                 total = 0;
  49.                                         }
  50.                                 }
  51.                                
  52.                         }

  53.                         return result;
  54.                 }
  55.                
  56.                 public static void main(String[] args) {
  57.                        
  58.                         int[] l = {1,2,3,4};
  59.                        
  60.                         int t = 15;
  61.                        
  62.                         int[] result = solution(l,t);
  63.                        
  64.                         for(int i = 0 ; i< result.length; i++) {
  65.                                
  66.                                 System.out.println(result[i]);
  67.                         }
  68.                 }
  69.         }

复制代码


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-27 22:59

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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