코테/프로그래머스

[프로그래머스/Lv. 0] 369게임

imname1am 2023. 2. 6. 10:42
반응형

내 코드

class Solution {
    public int solution(int order) {
        int answer = 0;
        
        String orderStr = Integer.toString(order);
        answer = orderStr.length() - orderStr.replaceAll("[369]", "").length();
        
        return answer;
    }
}

문자열 치환할 때 [] (정규식)로 한 번에 해줬고,

문자열에서 특정 문자 개수 구하기

: 전체 문자열 길이 - 특정 문자를 뺀 길이 = 특정 문자만 포함한 길이

이렇게 풀어줬다.

(아래 문제처럼!)

 

[프로그래머스/Lv. 0] k의 개수

내 코드 class Solution { public int solution(int i, int j, int k) { int cnt = 0; String str = ""; for(int t=i ; t

bono039.tistory.com

 

다른 코드

class Solution {
    public int solution(int order) {
        int answer = 0;

        while (order >= 1) {
            if (order % 10 == 3 || order % 10 == 6 || order % 10 == 9) answer ++;
            order /= 10;
        }

        return answer;
    }
}

나머지 구하는 방식으로!

사실 나도 이렇게 할 생각을 했었다,,

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

반응형