//구글콘솔 광고 추가가
옹알이 (1) 문제

내 코드
using System;

public class Solution {
    public int solution(string[] babbling) {
        int answer = 0;
        string[] ableTalk = {"aya", "ye", "woo", "ma"};
        for (int i = 0; i < babbling.Length; i++)
        {
            for (int j = 0; j < ableTalk.Length; j++)
            {
                babbling[i] = babbling[i].Replace(ableTalk[j], "H");
            }

            babbling[i] = babbling[i].Replace('H'.ToString(),"");
            if (babbling[i] == "") answer++;
        }        
        return answer;
    }
}
728x90
무작위로 K개의 수 뽑기 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int[] solution(int[] arr, int k) {
        var answer = new List<int>(arr).Distinct().ToList();
        var answerArr = new List<int>();
        for(int i =0; i < k; i++)
        {
            if(i < answer.Count)
                answerArr.Add(answer[i]);
            else
                answerArr.Add(-1);
        }
        return answerArr.ToArray();
    }
}

 

728x90
전국 대회 선발 고사 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int solution(int[] rank, bool[] attendance) {
        int answer = 0;
        Dictionary<int, int> dic = new Dictionary<int, int>();
        for (int i = 0; i < rank.Length; i++)
        {
            if (attendance[i])
                dic.Add(rank[i], i);
        }
        List<int> list = dic.Keys.ToList();
        list.Sort();
        answer = dic[list[0]] * 10000 + dic[list[1]] * 100 + dic[list[2]];
        return answer;
    }
}
728x90
수열과 구간 쿼리 2 문제

 

내 코드
using System;
using System.Collections.Generic;
using System.Linq;
public class Solution {
    public int[] solution(int[] arr, int[,] queries) {
        var answer = new List<int>();
        for (int i = 0; i < queries.GetLength(0); i++)
        {
            int min = arr.Max()+1;
            for (int j = queries[i, 0]; j <= queries[i, 1]; j++)
            {
                if (arr[j] > queries[i, 2] && arr[j]< min)
                    min = arr[j];
            }
            if (min == arr.Max() + 1) min = -1;
            answer.Add(min);
        }
        return answer.ToArray();
    }
}
728x90
사용할 때 마다 헷갈려서 다시 한번 공부 해보는 다차원 배열과 가변 배열.
c#에서 배열은 참조형! 
배열의 장점 : 연속된 메모리 공간에 데이터가 저장되므로 속도가 빠름.
배열의 단점 : 할당된 배열의 크기는 변경 X >> 메모리 사용 비효율적.
즉, 한번 메모리를 할당 해두면 데이터를 저장하지 않아도 메모리가 사용됨. 

다차원 배열
  • 1차원 배열: 가로로 쭉 값이 들어가는 나열형식.
자료형 배열이름[가로크기(열)] int[] scoreArr = new int[5] {60, 50, 40, 70, 30}; //1행 5열
  • 2차원 배열 : 1차원 배열이 n개 있는 것.  가로, 세로 나열형식. ex>> 평면이 사각형 모양
자료형 배열이름[세로크기(행), 가로크기(열)]
int[,] scoreArr = new int[2,3]
{
   //2행 3열
   {90,30,50},
   {20, 80,100}
}; 
 for (int i = 0; i < 2; i++)
 {
      for (int j = 0; j < 3; j++)
      {
           Console.Write($"{twoArray[i, j]}\t");
      }
 }
  • 3차원 배열 : 2차원 배열이 m개가 있는 것. 2차원 배열에서 더 나아가 한 종류의 배열을 더 가진 배열. 가로, 세로, 높이 나열형식. ex>>3차원 3D로된 사각형 모양
자료형 배열이름[높이(면),세로크기(행),가로크기()]
int[,,,]scoreArr = new int[2,3,5] { 
   { 
      {99, 90, 95, 93, 91}, 
      {85, 89, 81, 83, 87},
      {72, 74, 76, 77, 78}
   }, 
   { 
      {60, 64, 62, 67, 68}, 
      {52, 53, 57, 58, 56}, 
      {42, 44, 46, 47, 48} 
   } 
};
for (int i = 0; i < 2; i++)
{
     for (int j = 0; j < 3; j++)
     {
          for (int k = 0; k < 5; k++)
          {
               Console.Write($"{threeArray[i, j, k]}\t");
          }
     }
}

 

가변배열
  • 다차원 배열과 비슷하지만 비슷하지 않음. 다양한 1차원 배열들을 가지고 있는 배열.
  • 행과 열을 가지는 가변배열.
  • 가변배열의 요소는 다양한 차원과 크기를 가질수 있어 "배열의 배열" 이라 함.
자료형[][] 배열이름 = new 자료형[n][]; n은 1차원 배열들의 갯수를 말하고 그뒤 대괄호는 표기 X.(1차원 배열의 길이 다 다를 수 있음)
int[][] array = new int[3][];

array[0] = new int[2];
array[1] = new int[3];
array[2] = new int[4];
int[][] array = new int[3][];

array[0] = new int[2]{1, 2};
array[1] = new int[3]{3, 4, 5};
array[2] = new int[4]{6, 7, 8, 9};

int[][] array = new int[][]
{
new int[]{1, 2, 3},
new int[]{4, 5, 6},
new int[]{7, 8, 9}
};
int[][] array =
{

new int[]{1, 2, 3},
new int[]{4, 5, 6},
new int[]{7, 8, 9}
}
728x90

'c#' 카테고리의 다른 글

튜플(Tuples) 자료형 - 튜플 형식에 대해  (0) 2023.12.19
Invoke 와 BeginInvoke의 차이점  (1) 2023.12.14
async 와 await 키워드(비동기 함수)  (0) 2023.04.07
TCP VS UDP  (0) 2023.03.08
객체지향 프로그래밍  (0) 2023.03.08

+ Recent posts