Menu
Coddy logo textTech

Practice #2

Coddy의 Hash Table - 자료구조 시리즈 #4 코스 레슨 — 14개 중 11번째.

challenge icon

챌린지

쉬움

정수 배열 arr와 정수 target을 입력받아, 두 요소의 합이 target이 되는 두 요소의 인덱스를 반환하는 함수 twoSum을 작성하세요.

인덱스는 오름차순으로 반환해야 합니다: [smaller_index, larger_index]. 각 입력에는 정확히 하나의 유효한 쌍이 존재합니다.

반드시 hashmap.<ext>에 제공된 HashMap 클래스를 사용해야 합니다. 언어 내장 기능인 set, dict, map 등을 사용하지 마세요.

직접 해보기

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "solution.h"

int main() {
    char line[8192];
    char tline[64];
    if (!fgets(line, sizeof(line), stdin)) line[0] = '\0';
    if (!fgets(tline, sizeof(tline), stdin)) tline[0] = '\0';
    int arr[4096];
    int n = 0;
    char* tok = strtok(line, " \t\r\n");
    while (tok) { arr[n++] = atoi(tok); tok = strtok(NULL, " \t\r\n"); }
    int target = atoi(tline);
    int rs = 0;
    int* r = twoSum(arr, n, target, &rs);
    printf("%d %d\n", r[0], r[1]);
    return 0;
}

Hash Table - 자료구조 시리즈 #4의 모든 레슨