鱼C论坛

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

[学习笔记] Google foobar challenge

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

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

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

x
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[i]);
                    
            }
            
            
//            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[0]);
//               System.out.println(min[1]);
            
            
            for(int n = 0; n <min.length; n++ ) {
                    
                    if(n == min.length - 1) {
                    
                            result = result + min[n] ;
                    
                    }
                    else {
                            
                            result = result + min[n]+ ".";
                    }
            }
            
            
//            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[result_list.size()];
            
            for(int x = 0 ; x < result_list.size(); x++) {
                    
                    foo[x] = 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[i]);
                }
        }
        
        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[0];
        }
        
        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[lst.length-1];
                
                for(int i = 1; i< lst.length; i++) {
                        
                        result[i-1] = lst[i];
                }
                
                return result;
                
        }

}


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

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-9-21 04:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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