Given an array of integers sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm's runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
解题思路:
1. 由于是一个有序数组的查找问题,所以首先想到利用二分查找的算法
2. 这就转化成了利用二分查找,查找有序数列中的可重复元素的问题
3. 最开始想的是直接利用二分查找找到目标值,然后从两边发散遍历数组找到范围,然后超时了,时间复杂度不符合要求,所以需要再重新考虑
4. 由于二分查找的时间复杂度就是O(log n),所以要考虑直接在二分查找的算法中找到结果,而不用再搜索,所讲要求的范围也当做参数传入,当找到目标元素后,继续搜索,再次找到,即扩大范围。
代码如下:
public class Solution {
public int[] searchRange(int[] nums, int target) {
int[] result = {nums.length,-1};
binSearch(nums,0,nums.length-1,target,result);
if(result[0] > result[1]){
result[0] = -1;
}
//System.out.println(result[0]);
return result;
}
public void binSearch(int[] nums,int left,int right,int target,int[] result){
if(right < left){
return;
}
int mid = (left + right) /2;
//System.out.println(left + " " + mid + " " + right);
if(nums[mid] == target){
if(mid < result[0]){
result[0] = mid;
binSearch(nums,left,mid-1,target,result);
}
if(mid > result[1]){
result[1] = mid;
binSearch(nums,mid+1,right,target,result);
}
}
else if(nums[mid] < target){
binSearch(nums,mid+1,right,target,result);
}
else{
binSearch(nums,left,mid-1,target,result);
}
}
}
Problem 36 Valid Sudoku
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
The Sudoku board could be partially filled, where empty cells are filled with the character '.'
.
A partially filled sudoku which is valid.
Note:
A valid Sudoku board (partially filled) is not necessarily solvable. Only the filled cells need to be validated.
解题思路:
1. 这道题只需要弄清数独的规则就可以很快做出来,9*9的棋盘格,每一行,每一列,每一个3*3的小块,都必须正好有1~9这九个数,也就是说,一旦出现重复,则是不符合标准的
2. 首先判断棋盘格是不是9*9的,不是,则直接返回false,然后用函数checkRow、checkColumn、checkBlock 分别来检查每一行,每一列,和每一小块是否符合要求
代码如下:
public class Solution {
public boolean isValidSudoku(char[][] board) {
if(board.length != 9) return false;
for(int i = 0;i < 9;i++){
if(board[i].length != 9){
return false;
}
}
int pos[][] ={{1,1},{1,4},{1,7},
{4,1},{4,4},{4,7},
{7,1},{7,4},{7,7}} ;
boolean result = true;
for(int i = 0;i < 9;i++){
result = checkRow(board,i) && checkColumn(board,i) && checkBlock(board,pos[i]);
if(result == false){
break;
}
}
return result;
}
public boolean checkRow(char[][] board,int row){
Set<Character> set = new HashSet<>();
for(char c : board[row]){
if(c == '.'){
continue;
}
else if(c < '1' || c > '9'|| set.contains(c)){
return false;
}
else{
set.add(c);
}
}
return true;
}
public boolean checkColumn(char[][] board,int column){
Set<Character> set = new HashSet<>();
for(int i = 0;i < 9;i++){
char c = board[i][column];
if(c == '.'){
continue;
}
else if(c < '1' || c > '9'|| set.contains(c)){
return false;
}
else{
set.add(c);
}
}
return true;
}
public boolean checkBlock(char[][] board,int[] pos){
Set<Character> set = new HashSet<>();
for(int i = pos[0] - 1;i <= pos[0] + 1;i++){
for(int j = pos[1] - 1;j <= pos[1] +1;j++){
char c = board[i][j];
if(c == '.'){
continue;
}
else if(c < '1' || c > '9'|| set.contains(c)){
return false;
}
else{
set.add(c);
}
}
}
return true;
}
}