题解 | #数字序列中某一位的数字#

数字序列中某一位的数字

https://www.nowcoder.com/practice/29311ff7404d44e0b07077f4201418f5

  1. 要分区间,一位数的,两位数的,0-9,10-99,100-999
  2. 基于区间的起点,定位n在哪个数字
  3. 最后基于求余,找到数字上的具体位置
struct Solution{

}

impl Solution {
    fn new() -> Self {
        Solution{}
    }

    /**
    * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    *
    * 
        * @param n int整型 
        * @return int整型
    */
    pub fn findNthDigit(&self, n: i32) -> i32 {
        // write code here
          if n < 0 {
        return -1; // or handle the error in your preferred way
    }

    let mut n = n as usize; // Convert n to usize for indexing convenience
    let mut digits_count = 1;
    let mut base = 1;

    // Determine the range of numbers with the given number of digits
    while n > digits_count * 9 * base {
        n -= digits_count * 9 * base;
        digits_count += 1;
        base *= 10;
    }

    // Find the actual number
    let number = base + (n - 1) / digits_count;

    // Find the position of the digit within the number
    let position = (n - 1) % digits_count;

    // Extract the digit
    let digit = number.to_string().chars().nth(position).unwrap();

    digit.to_digit(10).unwrap() as i32
    }
}

#rust#
全部评论

相关推荐

大方的大熊猫准备进厂:1.教育背景:你希望从事什么专业的工作你的主修课就是什么;成绩优秀是你应该做的,没什么可描述的,成绩不优秀也许人家在大学忙着创业呢?(成绩优秀不一定是好事,只能说明多元化的大学你上成了高中,没有真正上明白大学,反而体现了你死板,不爱社交,没有别的突出能力) 2.实践经历:你想表达的意思没有说清楚。你是说你会个性化服务,还是你有实习经历。如果没有带来,经济收益,表彰,更好的发展前景,那你还不如说说提升了自己哪些技能。你说有人给你送锦旗我都能明白你优秀,但是你说你会xxxx,你说这话谁信,证据呢。 3.入伍经历:你描述的就是你的工作职责或者你应该做的,并没有体现出来你把这个事情做好了,而且入伍经历并不能证明你能干好你要应聘的工作,不如只写经历其余所有内容都不写。 4.荣誉技能:重点突出一下,但不要过多描述,这些荣誉的含金量懂得都懂。 重点:你要应聘什么工作(具体岗位,实习生不具体),你的期望薪资
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
06-13 19:30
化身华黑&nbsp;今天询问对接人审批情况,结果被告知没HC了&nbsp;云计算&nbsp;
苦闷的柠檬精allin实习:主管面结束后hr每周保温一次,结果前几天和我说没hc了,我也化身华黑子了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务