문제 설명

정수 배열 numbers가 주어집니다. numbers에서 서로 다른 인덱스에 있는 두 개의 수를 뽑아 더해서 만들 수 있는 모든 수를 배열에 오름차순으로 담아 return 하도록 solution 함수를 완성해주세요.


제한사항

  • numbers의 길이는 2 이상 100 이하입니다.
    • numbers의 모든 수는 0 이상 100 이하입니다.

입출력 예

numbersresult

[2,1,3,4,1] [2,3,4,5,6,7]
[5,0,2,7] [2,5,7,9,12]

입출력 예 설명

입출력 예 #1

  • 2 = 1 + 1 입니다. (1이 numbers에 두 개 있습니다.)
  • 3 = 2 + 1 입니다.
  • 4 = 1 + 3 입니다.
  • 5 = 1 + 4 = 2 + 3 입니다.
  • 6 = 2 + 4 입니다.
  • 7 = 3 + 4 입니다.
  • 따라서 [2,3,4,5,6,7] 을 return 해야 합니다.

입출력 예 #2

  • 2 = 0 + 2 입니다.
  • 5 = 5 + 0 입니다.
  • 7 = 0 + 7 = 5 + 2 입니다.
  • 9 = 2 + 7 입니다.
  • 12 = 5 + 7 입니다.
  • 따라서 [2,5,7,9,12] 를 return 해야 합니다.

 


작성한 코드 - JavaScript

function solution(numbers) {
    var answer = [];
    
    for (var i = 0; i < numbers.length ; i++) {
        for (var j = i+1; j < numbers.length ; j++) {
            var sum = numbers[i] + numbers[j];
            if (answer.indexOf(sum) < 0){
                answer.push(sum);
            }
        }
    }
    answer.sort((a,b)=>(a-b));
    
    return answer;
}

채점 결과

정확성: 100.0
합계: 100.0 / 100.0


sort() 사용 시 유니코드 값으로 정렬되기 때문에(ex. 1,10,3,7) 파라미터를 활용하여 정렬 필요

indexOf(value) 함수는 배열에 value값이 없을 때 -1 반환 

'✏️ 공부 > 코딩테스트' 카테고리의 다른 글

프로그래머스(lv2) - 스킬트리  (0) 2021.03.20
프로그래머스(lv2) - 문자열 압축  (0) 2021.03.19
완주하지 못한 선수  (0) 2021.03.16
서울에서 김서방 찾기  (0) 2021.03.15
두 정수 사이의 합  (0) 2021.03.11

  • Xxx-Spring-Boot-Autoconfigure 모듈: 자동 설정

  • Xxx-Spring-Boot-Starter 모듈: 필요한 의존성 정의

  • 그냥 하나로 만들고 싶을 때는?

    ○ Xxx-Spring-Boot-Starter

  • 구현방법

1. 의존성 추가

<dependencies>
	<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
	<dependency>
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-autoconfigure-processor</artifactId> 
      <optional>true</optional>
	</dependency>
</dependencies>

<dependencyManagement>
  <dependencies>
    <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-dependencies</artifactId> 
      <version>2.0.3.RELEASE</version> 
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

2. @Configuration 파일 작성

3. src/main/resource/META-INFspring.factories 파일 만들기

4. spring.factories 안에 자동 설정 파일 추가

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ 
  me.baekssul.HolomanConfiguration

5. mvn install
  --> 빌드하여 jar 생성, 다른 메이븐 프로젝트에서 사용할 수 있도록 local maven 저장소에 설치

  • @EnableAutoConfiguration (@SpringBootApplication 안에 숨어 있음)
  • 빈은 사실 두 단계로 나눠서 읽힘
    • 1단계: @ComponentScan
    • 2단계: @EnableAutoConfiguration
  • 컴포넌트 스캔은 Component 어노테이션을 가지고 있는 클래스를 스캔하여 Bean으로 등록
  • @ComponentScan
    • @Component
    • @Configuration @Repository @Service @Controller @RestController
  • @EnableAutoConfiguration
    • spring.factories
      • org.springframework.boot.autoconfigure.EnableAutoConfiguration
    • @Configuration
    • @ConditionalOnXxxYyyZzz (조건에 따라 bean을 등록)

의존성은 pom.xml 에서 관리함

 

의존성을 추가해주는 방법은

<dependencies> 아티팩트 내에 <dependency> ... </dependency>를 추가함.

 

스프링부트에서 버전을 관리해주지 않는 의존성의 경우 버전까지 표기해주어야 함.

 

기존 의존성의 버전을 수정해야하는 경우

<properties> 아티팩트를 추가하고 그 안에 <..version>을 표기해서 오버라이딩 해줌.

백기선의 스트링부트 

스프링부트는 스프링 기반의 애플리케이션을 만들 때 빠르고 쉽게 만들어준다.

설정 또한 많이 사용되는 항목을 자동으로 설정해준다.

톰캣 라이브러리 제공

 

프로젝트 생성 후

pom.xml에 spring boot를 부모로 설정

starter-web 의존성 추가

maven-plugin 의존성 추가 

reimport 필요

<!-- Inherit defaults from Spring Boot -->
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.3.RELEASE</version>
	</parent>

	<!-- Add typical dependencies for a web application -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
	</dependencies>

	<!-- Package as an executable jar -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

java디렉토리 내 패키지 아래 클래스(ex.Application) 만들고

스프링부트 어플리케이션 생성

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

 

또는 

 

start.spring.io/

 

 

메인 애플리케이션의 위치는 /main/java/기본 패키지에 만들어줌.

+ Recent posts