본문 바로가기
TIL

12.22 (TIL-코딩문제)

by 오랑이귀엽다 2023. 12. 22.

나누어 떨어지는 숫자

 

나의 풀이

더보기

using System.Collections.Generic;

public class Solution {
    public int[] solution(int[] arr, int divisor) {
        int[] answer = new int[] {};
        var list = new List<int>();
        
        if (divisor != 1)
        {
            foreach (var v in arr)
            {
                if (v % divisor == 0)
                    list.Add(v);
            }

            if (list.Count == 0)
                list.Add(-1);
        }
        
        else
        {
            list.AddRange(arr);
        }

        list.Sort();

        return list.ToArray();
    }
}

 

다른사람풀이

더보기
using System.Linq;
public class Solution {
    public int[] solution(int[] arr, int divisor)
    {
        int[] answer = arr.Where(x => (x % divisor) == 0).OrderBy(x => x).ToArray();

        if (answer.Count() == 0)
        {
            answer = new int[1];
            answer[0] = -1;
        }

        return answer;
    }
}

 

음양 더하기

 

 

 

나의 풀이

더보기

using System;

public class Solution {
    public int solution(int[] absolutes, bool[] signs) {
        int answer = 0;
        for (int i = 0; i < absolutes.Length; i++)
        {
            if (!signs[i])
            {
                absolutes[i] = -absolutes[i];
            }
            answer += absolutes[i];
        }
        return answer;
    }
}

 

다른 사람 풀이

더보기
using System;
using System.Linq;

public class Solution {
    public int solution(int[] absolutes, bool[] signs) {
        return absolutes.Select((t, idx) => signs[idx]? t : -t).Sum();
    }
}

 

휴대폰 번호 가리기

 

 

나의 풀이

더보기

public class Solution {
    public string solution(string phone_number) {
        string answer = "";
        
            string temp_str = phone_number.Substring(phone_number.Length - 4, 4);

            for(int i = 0; i < phone_number.Length - 4; ++i)
            {
                answer += "*";
            }

            answer += temp_str;

        return answer;
    }
}

 

다른사람풀이

더보기
public class Solution {
    public string solution(string phone_number) {
        string answer = phone_number.Substring(phone_number.Length - 4);
        answer = answer.PadLeft(phone_number.Length, '*');
        return answer;
    }
}

 

 

'TIL' 카테고리의 다른 글

12.27 (TIL-Unity3D)  (0) 2023.12.27
12.26 (TIL-Unity3D)  (0) 2023.12.26
12.21 (TIL - Unity)  (0) 2023.12.21
12.20 (TIL - Unity)  (1) 2023.12.20
12.19 (TIL-Unity3D)  (0) 2023.12.19