为什么当Eclipse将会让Codility执行我的代码?

时间:2016-01-12 03:11:40

标签: java eclipse collections

处理BinaryGap问题以准备技术评估。无论出于何种原因,我的代码在Eclipse中编译和运行都很好,但在通过Codility运行时却没有。两个" IDE"据说可以在Java8环境中执行。这是错误:

 Example test:   1041
 WRONG ANSWER (got 0 expected 5)

  Example test:   15
  OK
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Gapper {
    public static void main(String args[]){
        Solution sol = new Solution(1041);
        sol.solution(sol.getId());
    }
}

class Solution{
    private int count = 0;
    private int id;
    private String binary;
    private static final String Regex = "(1{1})([0]{1,})(1{1})";
    private Pattern pattern;
    private Matcher mat;
    private TreeSet<Integer> tree;
    public void setId(int id){
        this.id = id;
    }
    public int getId(){
        return this.id;
    }
    public int solution(int N){
        tree = new TreeSet<Integer>();
        this.binary = Integer.toBinaryString(id);
        for(int i = 0; i < this.binary.length(); i++){ 
            this.pattern= Pattern.compile(Regex);
            this.mat = this.pattern.matcher(this.binary.substring(i));
            while(this.mat.find()){
                if(this.mat.group().length() != 0){
                    tree.add(Integer.parseInt(this.mat.group().toString()));
                }
            }
        }

        int counter = 0;
        if(!tree.isEmpty()){
            String biggest = tree.last().toString();
            for(int i = 0; i < (biggest.length()); i++){
                if(biggest.charAt(i) == '0')counter++;
            }
            this.count = counter;
            System.out.println("Binary gap = "+count);
            return this.count;
        }else{
            return this.count;
        }
    }
    public Solution(int id){
        this.setId(id);
    }
    public Solution(){}
}

2 个答案:

答案 0 :(得分:0)

这是在所有情况下均适用的正确解决方案。

public int solution(int N) {

        int curr = 0;
        int max = 0;
        int state = 0;

        while (N > 0) {
            int bit = N & 1;
            switch (state) {
                case 0:
                    if (bit == 1 )
                    state = 1;
                    break;
                case 1:
                    curr = 0;
                    if (bit == 0) {
                        curr++;
                        state = 2;
                    }
                    break;
                case 2:
                    if (bit == 0) {
                        curr++;
                    }
                    else {
                        state = 1;
                        if (curr > max) {
                            max = curr;
                        }
                    }
                    break;
                }
            N >>= 1;
        }
        return max;
    }

答案 1 :(得分:-1)

这不是编译错误 - 编码成功编译并运行代码,它只是提醒您代码产生了错误的答案。在这种情况下,对于输入1041,正确的答案是5,但是你的代码给出0。

我认为用正则表达式解决这个问题是错误的做法。它迫使您对数字的外观做出假设。 Bit shifting并且计算零可能会更简单:

public int soltuion(int n) {
    int curr = 0;
    int max = 0;

    while (n > 0) {
        int bit = n & 1;
        n >>= 1;
        if (bit == 1) {
            if (curr > max) {
                max = curr;
            }
            curr = 0;
        } else {
            curr++;
        }
    }

    return max;
}