C# 프로그래머스 [Level 1] _제일 작은 수 제거하기
2020. 5. 29. 19:41ㆍ프로그래머스 알고리즘/코딩 테스트 문제
반응형
C# 프로그래머스 [Level 1] _문제이름

출제 링크 : https://programmers.co.kr/learn/courses/30/lessons/12935
코딩테스트 연습 - 제일 작은 수 제거하기
정수를 저장한 배열, arr 에서 가장 작은 수를 제거한 배열을 리턴하는 함수, solution을 완성해주세요. 단, 리턴하려는 배열이 빈 배열인 경우엔 배열에 -1을 채워 리턴하세요. 예를들어 arr이 [4,3,2,1
programmers.co.kr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution | |
{ | |
public int[] solution(int[] arr) | |
{ | |
List<int> returnArr = new List<int>(); | |
int min = 0; | |
if (arr.Length == 1) | |
{ | |
returnArr.Add(-1); | |
return returnArr.ToArray(); | |
} | |
for (int i = 0; i < arr.Length; i++) | |
{ | |
returnArr.Add(arr[i]); | |
} | |
min = returnArr.Min(); | |
foreach (var i in returnArr.ToArray()) | |
{ | |
if (i.Equals(min)) | |
{ | |
returnArr.Remove(i); | |
} | |
} | |
return returnArr.ToArray(); | |
} | |
} |
반응형