본문 바로가기

Computer Science

(252)
letCode - Longest Substring Without Repeating Characters import java.util.*; public class algo { public static void main(String[] args) { String s = "abcabcbb"; lengthOfLongestSubstring(s); } public static int lengthOfLongestSubstring(String s) { int answer = 0; HashMap myMap = new HashMap(); for (int j = 0, i = 0; j < s.length(); j++) { if (myMap.containsKey(s.charAt(j))) { i = Math.max(myMap.get(s.charAt(j)), i); } answer = Math.max(answer, j - i + ..
letCode - Add Two Numbers import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; import java.util.stream.IntStream; public class algo { public static void main(String[] args) { ListNode listNode11 = new ListNode(); ListNode listNode21 = new ListNode(); ListNode listNode31 = new ListNode(); listNode11.val = 2; listNode21.val = 4; listNode31.val = 3; listNode11.next = list..
leetCode - twoSum class Solution { public int[] twoSum(int[] nums, int target) { int[] answer = {}; Map integerIntegerMap = new HashMap(); for (int i = 0; i
Stream 사용 주의사항 1. Stream은 단순한 흐름이다. 그러므로 입력값의 변화는 이루어낼수 없다. 입력값이 변하기를 원한다면 for-loop문을 사용하길 권장한다. 2. Stream은 break나 continue를 이용해서 멈출수가 없다. 흐름이 한번 시작되면 그 흐름의 끝을 맞이해야 한다. 그러므로 중간에 멈춰서 성능향상을 유도한다면 Stream이 아닌 for-loop 사용을 권장한다.
프로그래머스 모의고사 import java.util.*; import java.util.stream.IntStream; public class algo { public static void main(String[] args) { int[] answers = {1,3,2,4,2}; solution(answers); } public static class StudentInfo { int studentNumber; int[] studentAnswer; int studentScore = 0; int answerLength; public void setStudentScore(int studentScore) { this.studentScore = studentScore; } public StudentInfo(int studentNumb..
프로그래머스 더 맵게 import java.util.*; import java.util.stream.Collectors; public class algo { public static void main(String[] args) { int[] scoville = {1, 2, 3, 9, 10, 12}; int k = 7; solution(scoville, k); } public static class ScovilleMixInfo { private int firstScouvileNum; private int secondScouvileNum; private int mixedScouvileNum; public ScovilleMixInfo() { } } public static void mixedScovilleFood(PriorityQ..
1 Stream - boxed(). 이란 ? stream은 원시타입을 다룰때 사용이 간편하고 Array를 사용할때 편하다. 그렇기 때문에 자주 사용하는 편인데 최근 한가지 오차에 빠졌다. 단순 배열을 .collect(Collectors.toList())를 사용해서 리스트로 변경할수 있을까 ? 답은 아니다. int[] 같은 원시타입의 경우 Stream에 담기 위해서는 객체형태로 변경이 되어야 하는데 해당 역할을 수행하는 것은 MapToObj()라는 메소드이다. 해당 메소드를 통해 원시타입값을 객체로 바꾸고 스트림은 해당 타입을 이용한다. 그렇다면 원시타입을 List로 변경하려면 어떻게 해야 할까 ? 답은 boxed()의 사용이다. 해당 메소드의 내부 코드를 살펴보면 public final Stream boxed() { return mapToObj(I..
프로그래머스 가장큰수 import java.util.*; import java.util.function.Function; import java.util.function.IntToDoubleFunction; import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.IntStream; public class algo { public static void main(String[] args) { int[] numbers = { 6, 10, 2 }; solution(numbers); } public static String solution(int[] numbers) { String answer = ""; List numbe..