[LeetCode] 1. Two Sum

2021. 6. 3. 15:56·알고리즘 문제풀이/LeetCode
728x90

문제

https://leetcode.com/problems/two-sum/

 

Two Sum - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

풀이

이중 for문을 활용하여 문제를 해결한다.

i : 0 ~ num.length-1

j : i+1 ~ num.length

모든 범위를 확인하며 nums[i] + nums[j] 가 target이 되는 경우 답을 answer에 저장하고 리턴한다.

 

코드

class Solution {
	public int[] twoSum(int[] nums, int target) {

		int[] answer = new int[2];

		for (int i = 0; i < nums.length - 1; i++) {
        	for (int j = i + 1; j < nums.length; j++) {
            	if (nums[i] + nums[j] == target) {
                	answer[0] = i;
                    answer[1] = j;
                    return answer;
                }
            }
        }

		return answer;
    }
}
728x90

'알고리즘 문제풀이 > LeetCode' 카테고리의 다른 글

[LeetCode] 70. Climbing Stairs  (0) 2021.06.10
[LeetCode] 322. Coin Change  (1) 2021.06.10
[LeetCode] 238. Product of Array Except Self  (0) 2021.06.06
[LeetCode] 217. Contains Duplicate  (0) 2021.06.06
[LeetCode] 121. Best Time to Buy and Sell Stock  (0) 2021.06.06
'알고리즘 문제풀이/LeetCode' 카테고리의 다른 글
  • [LeetCode] 322. Coin Change
  • [LeetCode] 238. Product of Array Except Self
  • [LeetCode] 217. Contains Duplicate
  • [LeetCode] 121. Best Time to Buy and Sell Stock
kiminae
kiminae
공부한 내용을 정리합니다.
  • kiminae
    데이터 다루는 사람
    kiminae
  • 전체
    오늘
    어제
    • 분류 전체보기 (67)
      • AI & 빅데이터 (6)
        • kafka (10)
        • [Book] 빅데이터를 지탱하는 기술 (12)
      • 알고리즘 (19)
      • 알고리즘 문제풀이 (13)
        • programmers (0)
        • 백준 (1)
        • LeetCode (12)
      • Android (3)
      • Book&Lesson (13)
        • [Lesson] 프로그래머스 커뮤러닝 (Pyth.. (1)
      • 참고한 글들 (1)
      • 컨퍼런스 정리 (1)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

  • 공지사항

  • 인기 글

  • 태그

    카프카
    ViewModel
    정렬알고리즘
    리트코드
    Algorithm
    파이프라인구축
    알고리즘풀이
    leetcode
    DP문제
    정렬
    알고리즘
    빅데이터를지탱하는기술
    추천알고리즘
    트리
    빅데이터
    hadoop
    머신러닝
    Kafka
    개인화추천
    데이터엔지니어
    카프카클라이언트
    sort
    릿코드
    알고리즘문제
    BI도구
    MPP데이터베이스
    시간복잡도
    mvvm
    버블정렬
    데이터시각화
  • 최근 댓글

  • 최근 글

  • hELLO· Designed By정상우.v4.10.1
kiminae
[LeetCode] 1. Two Sum
상단으로

티스토리툴바