判斷乙個 9x9 的數獨是否有效。只需要根據以下規則,驗證已經填入的數字是否有效即可。
數字 1-9 在每一行只能出現一次。
數字 1-9 在每一列只能出現一次。
數字 1-9 在每乙個以粗實線分隔的 3x3 宮內只能出現一次。
上圖是乙個部分填充的有效的數獨。
數獨部分空格內已填入了數字,空白格用 '.' 表示。
示例 1:
輸入:[
["5","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]輸出: true
示例 2:
輸入:[
["8","3",".",".","7",".",".",".","."],
["6",".",".","1","9","5",".",".","."],
[".","9","8",".",".",".",".","6","."],
["8",".",".",".","6",".",".",".","3"],
["4",".",".","8",".","3",".",".","1"],
["7",".",".",".","2",".",".",".","6"],
[".","6",".",".",".",".","2","8","."],
[".",".",".","4","1","9",".",".","5"],
[".",".",".",".","8",".",".","7","9"]
]輸出: false
解釋: 除了第一行的第乙個數字從 5 改為 8 以外,空格內其他數字均與 示例1 相同。
但由於位於左上角的 3x3 宮內有兩個 8 存在, 因此這個數獨是無效的。
說明:乙個有效的數獨(部分已被填充)不一定是可解的。
只需要根據以上規則,驗證已經填入的數字是否有效即可。
給定數獨序列只包含數字 1-9 和字元 '.' 。
給定數獨永遠是 9x9 形式的。
class solution
return true;
}int helper(int row,int col)
};
給你乙個未排序的整數陣列,請你找出其中沒有出現的最小的正整數。
示例 1:
輸入: [1,2,0]
輸出: 3
示例 2:
輸入: [3,4,-1,1]
輸出: 2
示例 3:
輸入: [7,8,9,11,12]
輸出: 1
你的演算法的時間複雜度應為o(n),並且只能使用常數級別的額外空間。
class solution
}for(int i=0;i0)return i+1;//正數則為所求結果
return nsize+1;
}};
41 缺失的第乙個正數
給定乙個未排序的整數陣列,找出其中沒有出現的最小的正整數。示例 1 輸入 1,2,0 輸出 3示例 2 輸入 3,4,1,1 輸出 2示例 3 輸入 7,8,9,11,12 輸出 1說明 你的演算法的時間複雜度應為o n 並且只能使用常數級別的空間。思路 這個題目和442.陣列中重複的資料 448....
41 缺失的第乙個正數
給定乙個未排序的整數陣列,找出其中沒有出現的最小的正整數。示例 1 輸入 1,2,0 輸出 3 示例 2 輸入 3,4,1,1 輸出 2 示例 3 輸入 7,8,9,11,12 輸出 1 說明 你的演算法的時間複雜度應為o n 並且只能使用常數級別的空間。這是從王道的考研資料結構看來的。設定乙個與給...
41 缺失的第乙個正數
先遍歷一遍,將大於等於1和小於等於陣列長度的數字放在與下標同值的位置 交換 再遍歷一遍,檢查數值是否和下標同值,不同值則返回該下標,如果都同值,則返回陣列長度 1 注意每次交換後要檢查心焦緩過來的數字是夠需要再交換,所以用while迴圈,而不用for迴圈 class solution object ...