LeetCode 34.Search for a Range & 36.Valid Sudoku

本文介绍了解决两个经典算法问题的方法:一是通过改进二分查找算法寻找目标值的范围;二是验证数独的有效性,确保每个单元格、每行、每列及每个3x3宫格内的数字不重复。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem 34 Search for a Range

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;
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值