Number Of Digit One


import java.util.Scanner;
public class Leetcode1 {
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
String k=String.valueOf(1);
for(int i=0;i<n;i++)
{
String s1=String.valueOf(i);
if(s1.startsWith(k))
{
System.out.print(s1+",");
}
}
}
}

java:
class Solution { public int countDigitOne(int n) { if (n <= 0) { return 0; } int m = n; int sum = 0; int e = 1; while (n > 0) { int r = n % 10; n /= 10; if (r == 0) { sum += n * e; } else if (r > 1) { sum += (n + 1) * e; } else { // r == 1 sum += m  n * 9 * e  e + 1; } e *= 10; } return sum; } }