java https://codility.com/programmers/lessons/

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java https://codility.com/programmers/lessons/相关的知识,希望对你有一定的参考价值。

public class TapeEquilibrium {
	/* 
	 * 100% solution 
	 * https://codility.com/demo/results/demo3VDKT8-AG5/
	 */
	    public int solution(int[] A) {
	        int start = A[0];
	        int end = sum(A, 1);
	        int minDif = Math.abs(start - end);
	         
	        for (int index = 1; index < A.length-1; index++) {
	            start += A[index];
	            end -= A[index];
	            if (Math.abs(start - end) < minDif)
	                minDif = Math.abs(start - end);
	        }
	        return minDif;
	    }
	     
	    public int sum(int[] A, int P) {
	        int result = A[P];  
	        for ( ; P < A.length-1; P++)
	            result += A[P+1];
	         
	        return result;
	    }
}
import java.util.*;

public class PermMissingElem {
	/* 
	 * 100% solution 
	 * https://codility.com/demo/results/demoRZDAXR-HJX/
	 */
	public int solution(int[] A) {
		int missingNum = 0;
	        if (A.length != 0) {
		        Arrays.sort(A);
		        for (int currentNum : A) {
		        	if (++missingNum != currentNum)
		                    return missingNum;
		        }
	        	
		}
	        return ++missingNum;
		
	}
}
public class FrgJump {
	/* 
	 * 100% solution 
	 * https://codility.com/demo/results/demoD644JF-Z36/
	 */
	public int solution(int X, int Y, int D) {
		int distance = Y - X;
		return (distance % D == 0) ? (distance / D) : ((distance / D) + 1);
	}
}

以上是关于java https://codility.com/programmers/lessons/的主要内容,如果未能解决你的问题,请参考以下文章

关于 Codility 的 HilbertMaze 任务

Solution of NumberOfDiscIntersections by Codility

Codility 任务 TapEquilibrium 正确性仅为 33%

Solution to Triangle by Codility

the solution of CountNonDivisible by Codility

Codility峰的O(N * log(log(N)))算法?