Java - Google Foobar 挑战 2:“请传递编码消息”

时间:2021-03-31 19:06:56

标签: java string integer permutation long-integer

我很好奇为什么我的代码只通过了第四个测试用例。

如果您参与过 Google 的 Foobar 挑战,您可能知道测试用例是隐藏的,您从终端收到的唯一响应是“通过”或“失败”。到目前为止,尽管 Java 不是我的首选。

该算法是输入一个整数数组(范围为 0-9)并输出一个密码。 我已经确定通过按降序对数组进行排序,将其转换为字符串,并从该数字(从右到左)中删除尽可能少的数字以使其可被 3 整除来获得解决方案代码。

给出的测试用例:

//input:
    int[] l = { 3, 1, 4, 1 };
//output:
    int solution = 4311;

//input:
    int[] l = { 3, 1, 4, 1, 5, 9 };
//output:
    int solution = 94311;

这是我的代码:

//Foobar gives you this static class and no external dependencies.
//The driver code looks like "Solution.solution({input int array});", which must return Integer type.
public class Solution {

    public static int solution(int[] l) {
        
        //reverse sort and get value as both String and int (to take advantage of String manipulation options).
        quickSort(l, 0, l.length-1);
        String str = arrayToStringReverse(l);

        //Handle as long datatype to retain the ability to return up to Integer.MAX_VALUE.
        long num = Long.parseLong(str);
        num = (num % 3 == 0) ? num : makeDivisibleByThree(str);
    
        //Handle solutions exceeding Integer.MAX_VALUE.
        return((num > Integer.MAX_VALUE) ? 0 : ((int)num));
    }

    public static long makeDivisibleByThree(String str) {
        int pass = 1;
        int size = str.length() - 1;
        //If the number of digits cannot be removed, return early with 0.
        if(str.length() == pass) return(0);
        StringBuilder stb = new StringBuilder();
        int index = size;
        
        //crawl from right to left
        for(int i = size; i >= 0; i--) {
            //start with a new copy of the original string each itr.
            stb.replace(0, size, str);
            //remove n=pass digits.
            for(int x = 0; x < pass; x++) {
                stb.deleteCharAt(index - x);
            }

            //check if divisible by 3. if not, increment the index right.
            if(Long.parseLong(stb.toString()) % 3 == 0) {
                return(Long.parseLong(stb.toString()));
            }else {
                index--;
            }

            //if current pass completed without success, reset increment variables and continue for consecutive pass (iteratively removing an extra digit).
            if(i == 0) {
                pass++;
                //Return early if number of digits to be removed exceeds number of digits of the string being operated on.
                if(str.length() == pass) return(0);
                index = size;
                i = size;
                continue;
            }
        }
        return(0);
    }

    //I've minified these functions, as they are generic and work fine.
    public static String arrayToStringReverse(int[] arr){String rs="";for(int i=arr.length-1;i>=0;i--){rs+=arr[i];}return(rs);}
    public static void quickSort(int arr[],int begin,int end){if(begin<end){int partitionIndex=partition(arr,begin,end);quickSort(arr,begin,partitionIndex-1);quickSort(arr,partitionIndex+1,end);}}
    public static int partition(int arr[],int begin,int end){int pivot=arr[end];int i=(begin-1);for(int j=begin;j<end;j++){if(arr[j]<=pivot){i++;int swapTemp=arr[i];arr[i]=arr[j];arr[j]=swapTemp;}}int swapTemp=arr[i+1];arr[i+1]=arr[end];arr[end]=swapTemp;return i+1;}
}

/*
I've also found that test case 5 should return 0, for whatever that's worth.
My code is currently passing all but case #4.
*/

有人能给我一些建议或建议我可能忽略的任何东西吗?

0 个答案:

没有答案
相关问题