C# 알고리즘 코드카타
63. 숫자 짝꿍
잼잼재미
2024. 1. 4. 09:26
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
public string solution(string X, string Y) {
string answer;
bool allZeros = true;
bool[] num = new bool[Y.Length];
for(int i = 0; i < Y.Length; ++i)
{
num[i] = false;
}
char[] x = X.ToCharArray();
char[] y = Y.ToCharArray();
List<int> nums = new List<int>();
for(int i = 0; i < x.Length; ++i)
{
for(int j = 0; j < y.Length; ++j)
{
if(x[i] == y[j])
{
if(num[j] == false)
{
num[j] = true;
nums.Add(x[i]);
break;
}
}
}
}
if(nums.Count == 0)
{
answer = "-1";
return answer;
}
else
{
foreach (int i in nums)
{
if ((char)i != '0')
{
allZeros = false;
break;
}
}
if (allZeros)
{
return "0";
}
nums.Sort();
nums.Reverse();
char[] cha = new char[nums.Count];
for(int i = 0; i < nums.Count; ++i)
{
cha[i] = (char)nums[i];
}
answer = new string(cha);
return answer;
}
}
}
위 처럼 코드를 작성했는데 시간초과 오류가 남
수정 코드
추후, 수정해야 함