LeetCode-Number of Digit One

本文介绍了一种高效算法,用于计算从1到给定整数n之间所有整数中数字1出现的总次数。通过分析不同位数上1出现的规律,提出了一种非遍历的方法,避免了逐一检查每个数字。该算法适用于编程竞赛及面试等场景。

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n.

For example:
Given n = 13,
Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13.

题不难但是很难分析,首先我们想到的就是分解问题,找规律,把大问题小问题化,然后在编程解决。

很明显这里有一个比较好想的办法就是从1~n遍历,每次都去判断每个i中1的个数,显然比较慢。

肯定有别的办法,就是在给定n的时候直接分析。这个时候分析问题就可以从n=1开始尝试,用归纳法,找出什么规律出来。

很显然,我们可以按照位一个一个找,即在个位、十位、百位。。。出现过的1。

现在直接给出N的一般规律,比如N=abced五位数字的时候,我们分析百位c,有三种情况:

1)c == 0的时候,比如12013,此时百位出现1的是:00 100 ~ 00 199, 01 100~01 199,……,11 100~ 11 199,共1200个,显然这个有高位数字决定,并且受当前位数影响;

2)c == 1的时候,比如12113,此时百位出现1的肯定包括c=0的情况,另外还需要考虑低位的情况即:12100 ~ 12113共114个,

3)c >= 2的时候,比如12213,此时百位出现1的是:00 100 ~ 00 199, 01 100~01 199,……,11 100~ 11 199,12 100 ~ 12 199,共1300个,这个有高位数字决定,其实是加一,并且乘以当前位数



code:

public class Solution {
    public int NumberOf1Between1AndN_Solution(int n) {
    int cnt = 0;
    for (int m = 1; m <= n; m *= 10) {
        int a = n / m, b = n % m;
        cnt += (a + 8) / 10 * m + (a % 10 == 1 ? b + 1 : 0);
    }
    return cnt;
}

}




### LeetCode Problem 37: Sudoku Solver #### Problem Description The task involves solving a partially filled Sudoku puzzle. The input is represented as a two-dimensional integer array `board` where each element can be either a digit from '1' to '9' or '.' indicating empty cells. #### Solution Approach To solve this problem, one approach uses backtracking combined with depth-first search (DFS). This method tries placing numbers between 1 and 9 into every cell that contains '.', checking whether it leads to a valid solution by ensuring no conflicts arise within rows, columns, and subgrids[^6]. ```cpp void solveSudoku(vector<vector<char>>& board) { backtrack(board); } bool backtrack(vector<vector<char>> &board){ for(int row = 0; row < 9; ++row){ for(int col = 0; col < 9; ++col){ if(board[row][col] != '.') continue; for(char num='1';num<='9';++num){ if(isValidPlacement(board,row,col,num)){ placeNumber(num,board,row,col); if(backtrack(board)) return true; removeNumber(num,board,row,col); } } return false; } } return true; } ``` In the provided code snippet: - A function named `solveSudoku()` initiates the process. - Within `backtrack()`, nested loops iterate over all positions in the grid looking for unassigned spots denoted by '.' - For any such spot found, attempts are made to insert digits ranging from '1' through '9'. - Before insertion, validation checks (`isValidPlacement`) ensure compliance with Sudoku rules regarding uniqueness per row/column/subgrid constraints. - If inserting a number results in reaching a dead end without finding a complete solution, removal occurs before trying another possibility. This algorithm continues until filling out the entire board correctly or exhausting possibilities when returning failure status upward along recursive calls stack frames. --related questions-- 1. How does constraint propagation improve efficiency while solving puzzles like Sudoku? 2. Can genetic algorithms provide alternative methods for tackling similar combinatorial problems effectively? 3. What optimizations could enhance performance further beyond basic DFS/backtracking techniques used here?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值