주녘공부일지

[프로그래머스 C#] Lv.2 연속 부분 수열 합의 개수 본문

Programmers - C#/CodingTest Lv.2

[프로그래머스 C#] Lv.2 연속 부분 수열 합의 개수

주녘 2023. 8. 25. 17:41
728x90

https://school.programmers.co.kr/learn/courses/30/lessons/131701

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

1. 정답코드 및 핵심 아이디어, 유의사항

- 수열 + HashSet<T>

https://godgjwnsgur7.tistory.com/46

 

[C#] 자료구조 정리 (Dict, List, Queue, Stack, HashSet 등)

자료구조(Data Structure)란? 데이터를 효율적으로 접근하고 조작할 수 있게 데이터 구조를 만들어 관리하는 것 - Collections은 C#에서 지원하는 자료구조 클래스 using System.Collections.Generic; 제네릭 컬렉

godgjwnsgur7.tistory.com

    using System;
    using System.Collections.Generic;

    public class Solution
    {
        public int solution(int[] elements)
        {
            List<int> list = new List<int>(elements);
            HashSet<int> hashSet = new HashSet<int>();
            
            // elements를 한번 더 넣어서 따로 끝 인덱스를 0번과 이어주지 않게 함
            for (int i = 0; i < elements.Length - 1; i++)
                list.Add(elements[i]);

            for (int i = 0; i < elements.Length; i++)
            {
                int num = 0;
                for (int j = 0; j < elements.Length; j++)
                {
                    num += list[i + j]; // 현재 인덱스 + 더해나갈 인덱스
                    hashSet.Add(num);
                }
            }

            return hashSet.Count;
        }
    }
728x90