Home [Lv.1] 없는 숫자 더하기
Post
Cancel

[Lv.1] 없는 숫자 더하기

문제 0부터 9까지의 숫자 중 일부가 들어있는 정수 배열 numbers가 매개변수로 주어집니다. numbers에서 찾을 수 없는 0부터 9까지의 숫자를 모두 찾아 더한 수를 return 하도록 solution 함수를 완성해주세요.

제한조건
  • 1 ≤ numbers의 길이 ≤ 9
  • 0 ≤ numbers의 모든 원소 ≤ 9
  • numbers의 모든 원소는 서로 다릅니다.

입출력 예시
numbersresult
[1,2,3,4,6,7,8,0]14
[5,8,4,0,6,7,9]6

C#

숫자의 범위가 0 ~9까지니까 0~9까지를 문자열로 적어놓고,
정규식 표현으로 numbers에 해당하지 않는 값을 추출해서 더하기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Text.RegularExpressions;

public class Solution
{
	public int solution(int[] numbers)
	{
		string stdNums = "0123456789";
		string condition = string.Join("|", numbers);

		var sumData = Regex.Matches(stdNums, $@"[^{condition}]");

		int answer = 0;

		foreach(Match data in sumData)
		{
			answer += Convert.ToInt32(data.Value);
		}

		return answer;
	}
}

[성공]


프로그래머스에서 문제 확인

This post is licensed under CC BY 4.0 by the author.