코딩테스트/LV. 0

LV. 0 :: 7의 개수

KUROMI98 2023. 1. 22. 23:17

https://school.programmers.co.kr/learn/courses/30/lessons/120912

 

프로그래머스

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

programmers.co.kr

 

 

function solution(array) {
    var answer = 0;
    text = array.join('');
    
    for (i=0; i<text.length; i++)
    {
        if (text[i]==7) {answer +=1;}
        // 문자열의 원소가 7라면 answer에 1을 더해준다.
    } 
  
    return answer;
}

+) 230512

const solution = (array) => {
    let count = 0;
    const str = array.join('');
    for(i=0; i<str.length; i++) { 
        if (str[i].indexOf(7) >= 0)
        count++;
    }
    return count;
}

230527 python

def solution(array):  
    string = ''
    for x in array:
        string += str(x) 
    return string.count('7')

230916 C++

#include <string>
#include <vector>

using namespace std;

int solution(vector<int> array) {
    int answer = 0;
    string str = "";

    // array의 내부 원소 중 모든 숫자에 대하여, 문자열의 형태로 바꿔서 str이라는 문자열에 넣는다.
    for(int num : array){
        str += to_string(num);
    }

// str의 내부 원소 중 모든 문자에 대하여, '7' 이라는 문자가 나오면 answer를 1 증가시킨다.
    for(char c: str){
        if (c=='7') answer ++;
    }
    return answer;
}