전체 글
-
[프로그래머스] 9로 나눈 나머지알고리즘 노트 2023. 10. 17. 11:25
이 문제는 결국 모든 String을 하나씩 더하고 9로 나눈 값을 구하라는 뜻과 같습니다. 즉, String을 char형으로 변경한 후 loop를 돌며 더한 값을 9로 나누라는 것이죠. [문제풀이] 1. IntStream의 range()함수를 사용하여 number 문자열의 길이만큼 loop를 구현합니다. 2. mapToLong타입으로 문자열을 숫자로 변환해줍니다. 3. sum()을 통해 구해진 Long값을 더한 후 9로 나누어 나머지를 정수형으로 반환해줍니다. 이렇게 풀어도 사실 다르지는 않습니다만, String을 Long으로 바꾸고 다시 Long을 int형으로 바꾸며 캐스팅 시 데이터 유실의 위험성이 있습니다. 다른 분 코드를 참고하여 이렇게 수정해 보았습니다. [문제풀이] 1. number 문자열의 ..
-
Java Optional의 메소드 사용 설명서(Optional 잘 활용하기)스터디 노트 2023. 10. 16. 15:50
원본 강의 영상 : https://youtu.be/RsUTolCVm_E?si=JRr_Xu73RmoaybFP 📌 Optional이란? Java의 Optional 함수는 JDK 8에서 추가가 되었습니다. Optional은 쉽게 말해 있거나 없는 값을 표현할 수 있는 클래스입니다. 즉, null을 대체할 수 있는 타입입니다. null(💩)은 언제나 위기를 야기하기에 잘 다루어야 합니다 📌 Optional 만들기 Optional을 생성할 수 있는 방법은 of(), ofNullable(), empty()를 통해 생성할 수 있습니다. // of() : null이 아닌 값으로 생성됨 Optional opt = Optional.of("value"); // 만약 of()에 null을 넘기게 되면 NullPointerEx..
-
[프로그래머스] 배열 만들기 4알고리즘 노트 2023. 10. 16. 12:41
조건이 조금 복잡하긴 하지만, 결국 i를 어디서 증가시켜주느냐를 잘 판단하면 문제 없이 해결 가능할 것 같습니다. 코드가 조금 지저분하긴 하네요.. [문제 풀이] 1. 가변 데이터를 담기 위한 List 선언 2. 주어지는 arr만큼 for문을 돌 수 있도록 선언하며 i 증감 문법은 제외 3. list가 비어있거나, list의 마지막 요소가 arr[i]보다 작으면 list에 arr[i]를 추가하고 i 증가 4. arr[i]가 list의 마지막 요소보다 크거나 같으면 마지막 요소를 제거 이전에 사용했던 Stream의 skip함수를 사용해서 마지막 요소에 손쉽게 접근할 수 있었습니다.
-
[Java] IntStream 메소드 사용해보기스터디 노트 2023. 10. 16. 11:30
알고리즘 문제를 풀던 중 IntStream에 대해 활용하게된 내용이 있어서 공유를 합니다. 우선 IntStream.concat() 메소드는 2개의 IntStream을 합쳐주는 역할을 합니다. "요소가 첫 번째 스트림의 모든 요소 다음에 두 번째 스트림의 모든 요소인 게으르게 연결된 스트림을 만듭니다. 입력 스트림이 모두 정렬되면 결과 스트림이 정렬되고 입력 스트림 중 하나가 병렬인 경우 병렬로 정렬됩니다. 결과 스트림이 닫히면 두 입력 스트림에 대한 닫기 핸들러가 호출됩니다." 두 스트림을 병합한 후 하나의 스트림으로 반환해주는 역할을 하는 메소드로 이런 식으로 사용할 수 있습니다. 하나의 기본 스트림을 두고 하나의 변형 스트림을 구성해야 한다면 다음과 같이 구성할 수 있겠죠. // n이 10일 때 n이..