C# solution using maths


  • 0
    K
    using System;
    public class Solution {
        public int FindNthDigit(int n) {
    
            long totalLen = 0;
            int i=1;
            while (totalLen < n)
            {
                totalLen += 9*(long)Math.Pow(10,i-1)*i;
                i++;
            }
            i--;
            long diff = totalLen - n;
            int d = Convert.ToInt32(diff / i);
            int r = Convert.ToInt32(diff % i);
            long number = (int)Math.Pow(10,i)-1-d;
            string s = number.ToString();
            return Convert.ToInt32(s[s.Length-r-1].ToString());
    
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.