본문 바로가기

Algorithm8

[LeetCode] #4 Median of Two Sorted Arrays Median of Two Sorted Arrays​xThere are two sorted arrays nums1 and nums2 of size m and n respectively.​Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).​You may assume nums1 and nums2 cannot be both empty. 중간값 (Median)을 구하라는 문제이다.홀수개일 때는 중간값을, 짝수개일때는 중간 값 2개의 평균을 반환하면 된다. xxxxxxxxxxclass Solution { public double findMedianSortedArrays(int[] nums1, .. 2019. 12. 8.
[LeetCode] #3 Longest Substring Without Repeating Characters Longest Substring Without Repeating CharactersxGiven a string, find the length of the longest substring without repeating characters. 중복되는 알파벳이 없는 가장 최장의 substring을 구하라는 문제이다.내 풀이부터 우선 보자. xclass Solution { public int lengthOfLongestSubstring(String s) { int answer = 0; String tmp = ""; for (int i = 0; i 2019. 12. 5.
[LeetCode] #1 Two Sum Two SumxGiven an array of integers, return indices of the two numbers such that they add up to a specific target.​You may assume that each input would have exactly one solution, and you may not use the same element twice. 첫번째 문제 답게 쉬운 문제가 나왔다.그런데 뭔가 멋있게 풀고싶은 마음이 너무 드글드글해서,회사 제품 특성상 낮은 jdk버전에도 사용 가능한 코드만 짜다보니 간단하면서도 최근 jdk(라고 해봐야 자바8이지만..ㅎㅎ)에서 사용 가능한 방법으로 풀고싶었다. 그래서 우선 스트림을 사용해봤다.xxxxxxxxxximport.. 2019. 12. 5.
[codewars] Which are in? Which are in?xGiven two arrays of strings a1 and a2 return a sorted array r in lexicographical order of the strings of a1 which are substrings of strings of a2.​#Example 1: a1 = ["arp", "live", "strong"]​a2 = ["lively", "alive", "harp", "sharp", "armstrong"]​returns ["arp", "live", "strong"]​#Example 2: a1 = ["tarp", "mice", "bull"]​a2 = ["lively", "alive", "harp", "sharp", "armstrong"]​returns .. 2019. 3. 29.
[codewars] Who likes it? WhoLikeThis Who likes it?xYou probably know the "like" system from Facebook and other pages. People can "like" blog posts, pictures or other items. We want to create the text that should be displayed next to such an item.​Implement a function likes :: [String] -> String, which must take in input array, containing the names of people who like an item. It must return the display text as shown in t.. 2019. 3. 24.
[codewars] number fun NumberFun NumberFunxFYou might know some pretty large perfect squares. But what about the NEXT one?​Complete the findNextSquare method that finds the next integral perfect square after the one passed as a parameter. Recall that an integral perfect square is an integer n such that sqrt(n) is also an integer.​If the parameter is itself not a perfect square, than -1 should be returned. You may assu.. 2019. 3. 24.
[codewars] Dubstep dubstep DubstepxxxxxxxxxxFor example, a song with words "I AM X" can transform into a dubstep remix as "WUBWUBIWUBAMWUBWUBX" and cannot transform into "WUBWUBIAMWUBX".​Recently, Jonny has heard Polycarpus's new dubstep track, but since he isn't into modern music, he decided to find out what was the initial song that Polycarpus remixed. Help Jonny restore the original song.​InputThe input consist.. 2019. 3. 23.
[algorithm] 방 번호 방 번호x// 방 번호는 4자리// 숫자 세트는 0~9 숫자 하나씩이 한세트다.// 방 번호를 충족하기 위해서는 몇 세트가 필요한가?// * 6과 9는 호환 가능하다. (9999인 경우, '9' 2개와 '6' 2개를 사용해 2세트로 완성 가능하다.) 이번 문제는 스터디를하면서 풀게 된 문제이다.갑자기 풀게 된 문제라 그런지 인터넷에서 내가 문제를 골라서 풀 때랑은 아주 다른 느낌이었다.나는 단순한 사람이므로 그냥 문제 설명을 들은 대로 풀었다.숫자가 들어있는 세트를 가지고 숫자를 하나씩 꺼내 방 번호를 완성하는 식이었다. xxxxxxxxxximport java.util.Arrays;​class Solution { public static int solution(String room) { int resul.. 2019. 2. 22.