<Algorithm> 89. twosum(LeetCode)
by BFine반응형
1. Twosum
예전에 거의 똑같은 문제를 본 적이 있었는데 오랜만에 다시보니 확 떠오르질 않았다.
이문제도 수학적으로 접근해 볼 필요가 있는 문제
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 | import java.util.HashMap; import java.util.Map; import java.util.stream.IntStream; public class Main { public int[] two(){ int nums[] = new int[] {1,11,15,17}; int target = 12; int result[] = new int[2]; Map<Integer,Integer> map = new HashMap<>(); IntStream.range(0, nums.length) .forEach(i ->{ int sub = nums[i] - target; if(map.containsKey(sub)) { result[0] = map.get(sub); result[1] = i; } map.put(nums[i], i); }); return result; } } | cs |
참고 & 출처
블로그의 정보
57개월 BackEnd
BFine