티스토리 뷰
1
2
3
4
5
6
7
8
9
10
11
12
13
|
class Solution {
public int[] twoSum(int[] nums, int target) {
for (int i = 0; i < nums.length - 1; i++) {
int answer = target - nums[i];
for (int j = i + 1; j < nums.length; j++) {
if (answer - nums[j] == 0) {
return new int[] {i, j};
}
}
}
return null;
}
}
|
cs |
1. 그냥 생각하기 쉽게 푼거
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
class Solution {
public int[] twoSum(int[] nums, int target) {
HashMap<Integer, Integer> hashMap = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
if (hashMap.containsKey(target - nums[i])) {
return new int[] {i, hashMap.get(target - nums[i])};
} else {
hashMap.put(nums[i], i);
}
}
return null;
}
}
|
cs |
2. 다른 방법 찾아본거
HashMap 쓸 생각 못했는데 O(n)이라 훨씬 빠른듯
링크
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- Spring Security
- ㅇㄷㅇㅈ
- 가상 면접 사례로 배우는 대규모 시스템 설계 기초
- HTTP 완벽가이드
- http
- 김영한 http
- 백기선 스터디
- dreamcoding
- 프로그래머스
- 킹수빈닷컴
- 패스트캠퍼스 컴퓨터공학 완주반
- js promise
- BOJ
- js array
- java
- 김영한 JPA
- HTTP 완벽 가이드
- 이펙티브자바 아이템60
- 백준
- REST API
- JPA 연관관계 매핑
- 이펙티브자바 아이템59
- 프로그래머스 SQL
- 드림코딩
- GCP
- js api
- JS 딥다이브
- 이펙티브자바 스터디
- 이펙티브자바
- 모던자바스크립트
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
글 보관함