Seawolf 发表于 2019-8-8 08:08:51

Google foobar challenge

Level 2 第二个问题:

Elevator Maintenance
====================

You've been assigned the onerous task of elevator maintenance - ugh! It wouldn't be so bad, except that all the elevator documentation has been lying in a disorganized pile at the bottom of a filing cabinet for years, and you don't even know what elevator version numbers you'll be working on.

Elevator versions are represented by a series of numbers, divided up into major, minor and revision integers. New versions of an elevator increase the major number, e.g. 1, 2, 3, and so on. When new features are added to an elevator without being a complete new version, a second number named "minor" can be used to represent those new additions, e.g. 1.0, 1.1, 1.2, etc. Small fixes or maintenance work can be represented by a third number named "revision", e.g. 1.1.1, 1.1.2, 1.2.0, and so on. The number zero can be used as a major for pre-release versions of elevators, e.g. 0.1, 0.5, 0.9.2, etc (Commander Lambda is careful to always beta test her new technology, with her loyal henchmen as subjects!).

Given a list of elevator versions represented as strings, write a function solution(l) that returns the same list sorted in ascending order by major, minor, and revision number so that you can identify the current elevator version. The versions in list l will always contain major numbers, but minor and revision numbers are optional. If the version contains a revision number, then it will also have a minor number.

For example, given the list l as ["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"], the function solution(l) would return the list ["1.0", "1.0.2", "1.0.12", "1.1.2", "1.3.3"]. If two or more versions are equivalent but one version contains more numbers than the others, then these versions must be sorted ascending based on how many numbers they have, e.g ["1", "1.0", "1.0.0"]. The number of elements in the list l will be at least 1 and will not exceed 100.

Languages
=========

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

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

-- Python cases --
Input:
solution.solution(["1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"])
Output:
    0.1,1.1.1,1.2,1.2.1,1.11,2,2.0,2.0.0

Input:
solution.solution(["1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"])
Output:
    1.0,1.0.2,1.0.12,1.1.2,1.3.3

-- Java cases --
Input:
Solution.solution({"1.11", "2.0.0", "1.2", "2", "0.1", "1.2.1", "1.1.1", "2.0"})
Output:
    0.1,1.1.1,1.2,1.2.1,1.11,2,2.0,2.0.0

Input:
Solution.solution({"1.1.2", "1.0", "1.3.3", "1.0.12", "1.0.2"})
Output:
    1.0,1.0.2,1.0.12,1.1.2,1.3.3


花了几个小时终于搞定了,要说也不算难,就是麻烦

import java.util.ArrayList;

public class Solution {
       
    public static String[] solution(String[] l) {
      // Your code here
            String result = "" ;
           
            ArrayList <String> resource = new ArrayList<String>();
           
            ArrayList <String> result_list = new ArrayList<String>();
           
            int len1 = l.length;
           
            for(int i = 0; i < len1 ; i++) {
                   
                    resource.add(l);
                   
            }
           
           
//            String[] test = resource.get(0).split("\\.");//resource.get(0).split(".");
//            System.out.println(test.length);
//            System.out.println("--------");
           
//            print_list(resource);
           
            for(int m = 0 ; m < len1 ; m ++) {
                   
              int len = resource.size();
             
                    String[] min = resource.get(0).split("\\.");
                   
//                    print_string_list(min);
//                   
//                    System.out.println(len);
           
                    for(int i = 1; i < len ; i++) {
                   
                    String[] temp = resource.get(i).split("\\.");
                   
//                    System.out.println("--------");
//              print_string_list(temp);
                   
                    if(cmp(min,temp) == true) {
                           
                            min = temp;
                    }
                    }
                           
           
//            print_string_list(min);
//           
//               System.out.println(min.length);
           
//               System.out.println("--------");
              
//               System.out.println(min);
//               System.out.println(min);
           
           
            for(int n = 0; n <min.length; n++ ) {
                   
                    if(n == min.length - 1) {
                   
                            result = result + min ;
                   
                    }
                    else {
                           
                            result = result + min+ ".";
                    }
            }
           
           
//            System.out.println(result);
//           
//            System.out.println("******");
//           
           
            result_list.add(result);
           
            resource.remove(result);
           
            result = "";
           
//            print_list(result_list);
//           
//            print_list(resource);
           
                   
            }
//            print_list(result_list);
           
            String[] foo = new String;
           
            for(int x = 0 ; x < result_list.size(); x++) {
                   
                    foo = result_list.get(x);
            }
            return foo;
   
    }
   
   
private static void print_list(ArrayList lst) {
               
                for(int i = 0; i< lst.size(); i++) {
                       
                        System.out.println(lst.get(i));
                }
        }
       
        private static void print_string_list(String[] lst) {
               
                for(int i = 0; i< lst.length; i++) {
                       
                        System.out.println(lst);
                }
        }
       
        private static boolean cmp(String[] lst1, String[] lst2) {
               
//                System.out.println("**");
//                print_string_list(lst1);
//               
//                System.out.println("@@");
//                print_string_list(lst2);
               
               
                if((lst1.length == 1 && lst2.length > 1)&& Integer.parseInt(get_head(lst1)) == Integer.parseInt(get_head(lst2)) ) {
                       
                        return false;
                }
                else if ((lst2.length == 1 && lst1.length >1)&& Integer.parseInt(get_head(lst1)) == Integer.parseInt(get_head(lst2)) ) {
                       
                        return true;
                }
               
               
                if(Integer.parseInt(get_head(lst1)) > Integer.parseInt(get_head(lst2))) {
                       
                        return true;
                       
                }
                else if (Integer.parseInt(get_head(lst1)) < Integer.parseInt(get_head(lst2))) {
                       
                        return false;
                }
               
                else {
                       
                        return cmp(get_tail(lst1), get_tail(lst2));
                       
                }
               

       
        }
       
        private static String get_head(String[] lst) {
               
                if(lst == null || lst.length == 0) {
                       
                        return null;
                }
               
                return lst;
        }
       
        private static String[] get_tail(String[] lst) {
               
                if(lst == null || lst.length == 0) {
                       
                        return null;
                }
               
                if(lst.length == 1) {
                       
                        return null;
                }
               
                String[] result = new String;
               
                for(int i = 1; i< lst.length; i++) {
                       
                        result = lst;
                }
               
                return result;
               
        }

}



页: [1]
查看完整版本: Google foobar challenge