数独计数子字段

时间:2016-07-05 15:55:10

标签: java arrays sudoku

我目前正在研究SudokuChecker我想检查数独的子域[3x3]。以下代码执行此操作:

int[][] field = new field[9][9];
int wrongNumbers = 0;
for (int i = 0; i < 9; i += 3) {
    for (int j = 0; j < 9; j += 3) {

        // Check subfield by using an array
        int arr[] = new int[10];
        for (int k = 0; k < 3; k++) {
            for (int l = 0; l < 3; l++) {
                arr[field[i + k][j + l]]++;
            }
        }

        for (int k = 1; k < arr.length; k++) {
            wrongNumbers += arr[k] > 1 ? arr[k] - 1 : 0;
        }
    }
}

我想知道给定代码有什么改进吗? (我不是在谈论使39等不变)

1 个答案:

答案 0 :(得分:1)

我在thucnguyen的Codefights中找到了一个非常好的答案:

func uploadFiles() {

    if let imageData = UIImageJPEGRepresentation(customKindImage.image!, 1) {            
        print("There is Not Any Images to upload!")

        let uploadScriptUrl = URL(string : "http://www.swiftdeveloperblog.com/http-post-example-script/")
        var request = URLRequest(url: uploadScriptUrl!)            
        request.httpMethod = "POST"            
        request.setValue("Keep-Alive" , forHTTPHeaderField : "Connection")

        let task = URLSession.shared.uploadTask(with: request, from: imageData, completionHandler: { (data, response, error) in
            guard let data = data, error == nil else {
                print(error?.localizedDescription ?? "Error")
                return
            }
            //access data here
        })            
        task.resume()
    }       
}