728x90
SMALL

using System;

public class Solution {
    public int solution(int i, int j, int k) {
        int answer = 0;
        int temp = 1;
        
        for(int a=i;a<=j;a++)
        {
            temp = a;
            while(temp > 0)
            {
                if(temp%10 == k) // 마지막 자리가 k이면 answer++
                {
                    answer++;
                }
                temp /= 10;
            }
        }
        
        return answer;
    }
}

 

//

using System;
using System.Linq;

public class Solution {
    public int solution(int i, int j, int k) {
        int answer = Enumerable.Range(i, j - i + 1).Sum(x => x.ToString().Count(o => o.ToString() == k.ToString()));

        return answer;
    }
}

 

 

파이썬

/////

 

def solution(i, j, k):
    answer = 0
    for n in range(i, j + 1):
        answer += str(n).count(str(k))
    return answer

 

/////////

 

from collections import Counter

def solution(i, j, k):
    answer = 0
    for n in range(i,j+1):
        answer += Counter(str(n))[str(k)]
    return answer

 

자바

////////////

 

class Solution {
    public int solution(int i, int j, int k) {
        String str = "";
        
        for(int a = i; a <= j; a++) 
        {
            str += a+"";
        }

        return str.length() - str.replace(k+"", "").length();
    }
}

 

////////////

 

class Solution {
    public int solution(int i, int j, int k) {
        int answer = 0;

        for (int num = i; num <= j; num++)
        {
            int tmp = num;
            while (tmp != 0)
            {
                if (tmp % 10 == k)
                    answer++;
                tmp /= 10;
            }
        }
        return answer;
    }
}
728x90

설정

트랙백

댓글