Deep_Dev
article thumbnail

๋ฌธ์ œ ์„ค๋ช…

1๋ถ€ํ„ฐ 13๊นŒ์ง€์˜ ์ˆ˜์—์„œ, 1์€ 1, 10, 11, 12, 13 ์ด๋ ‡๊ฒŒ ์ด 6๋ฒˆ ๋“ฑ์žฅํ•ฉ๋‹ˆ๋‹ค. ์ •์ˆ˜ i, j, k๊ฐ€ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ฃผ์–ด์งˆ ๋•Œ, i๋ถ€ํ„ฐ j๊นŒ์ง€ k๊ฐ€ ๋ช‡ ๋ฒˆ ๋“ฑ์žฅํ•˜๋Š”์ง€ return ํ•˜๋„๋ก solution ํ•จ์ˆ˜๋ฅผ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

 
์ œํ•œ์‚ฌํ•ญ
  • 1 ≤ i < j ≤ 100,000
  • 0 ≤ k ≤ 9

 

์ž…์ถœ๋ ฅ ์˜ˆ ( i, j, k, result )

 

1 13 1 6
10 50 5 5
3 10 2 0

 

 


๋‚˜๋จธ์ง€ ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ณ„์† ๊ตฌํ•ด์„œ countํ•˜๋Š” ๋ฐฉ์‹์ด ๋จผ์ € ์ƒ๊ฐ๋‚ฌ๋Š”๋ฐ,

๊ทธ๋ƒฅ ๋ฌธ์ž์—ด์— ๋‹ค ๋”ํ•˜๊ณ  countํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ํ’€์—ˆ๋‹ค.

์Œ .. int -> char ํ˜• ๋ณ€ํ™˜์„ ์ฐพ์•„๋ดค์—ˆ๋Š”๋ฐ

Character.forDigit()  ์„ ์‚ฌ์šฉํ•œ๋‹ค.


 

import java.util.Scanner;
class Solution {
    public int solution(int n, int m, int k) {
        int answer=0;
        String s="";
        char c=Character.forDigit(k,10);
        for(int i=n; i<=m; i++)
            s += String.valueOf(i);

        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)==c)
                answer++;
        }
        return answer;
    }
}

 

 

 

 

< ์ž๋ฆฟ์ˆ˜์ด์šฉํ•ด์„œ ๊ตฌํ•˜๋Š” ๋ฐฉ์‹ >

 

 

import java.util.Scanner;
class Solution {
    public int solution(int n, int m, int k) {
        int answer=0;
        for(int i=n; i<=m; i++){
            int tmp=i;
            while(tmp!=0){
                if(tmp%10==k)
                    answer++;
                tmp/=10;
            }
        }
        return answer;
    }
}