[LeetCode/Kotlin]344. Reverse String
·
LeetCode/Kotlin | Easy
344. Reverse String (Kotlin) 문제Write a function that reverses a string. The input string is given as an array of characters s.You must do this by modifying the input array in-place with O(1) extra memory.Example 1:Input: s = ["h","e","l","l","o"]Output: ["o","l","l","e","h"]Example 2:Input: s = ["H","a","n","n","a","h"]Output: ["h","a","n","n","a","H"]Constraints:1 s[i] is a printable ascii char..
[LeetCode/Kotlin]Medium - 5. Longest Palindromic Substring
·
LeetCode/Kotlin | Medium
문제 url: https://leetcode.com/problems/longest-palindromic-substring/ 문제 해석s라는 문자열을 입력받을 때, 해당 문자열 내에서 앞뒤로 봐도 똑같은 글자들 중 가장 긴 글자를 찾아내면 된다.풀이 방법풀이 접근 과정기존에 프로그래머스 Level3 문제에서 가장 긴 팰린드롬 문제를 풀었던 적이 있다.그 풀이와 유사하게 풀었다.최종 소스코드class Solution { fun longestPalindrome(s: String): String { var cnt = 0 var answerIdx = Pair(0,0) fun palindrom(startIndex: Int, endIndex: Int) { ..
[LeetCode/Kotlin]Medium - 34. Find First and Last Position of Element in Sorted Array
·
LeetCode/Kotlin | Medium
Medium - 34. Find First and Last Position of Element in Sorted Array https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/나의 풀이class Solution { fun searchRange(nums: IntArray, target: Int): IntArray { var answer = intArrayOf(-1, -1) var start = 0 var end = nums.lastIndex var mid: Int while (start = 0 && nums[mid-index] == tar..
[LeetCode/Kotlin]Easy - 13. Roman to Integer
·
LeetCode/Kotlin | Easy
Easy - 13. Roman to Integerhttps://leetcode.com/problems/roman-to-integer/description/나의 풀이풀이 접근 과정 우선 로마 숫자를 모두 배열에 넣고 치환하여 더하는 작업 진행 최종 풀이class Solution { fun romanToInt(s: String): Int { var input = s var answer = 0 val map: Array> = arrayOf( "CM" to 900, "CD" to 400, "XC" to 90, "XL" to 40, "IX" to 9, "..
[LeetCode/Kotlin]Easy - 278. First Bad Version
·
LeetCode/Kotlin | Easy
Easy - 278. First Bad Version 문제 해석 총 물건의 개수 n을 입력받는다. 그 중 불량품의 첫 시작을 찾아내는 문제이다. 만약 1~7번까지의 상품이 있는데, 3번이 불량인 경우 3번부터는 모두 불량품이 된다. 문제에서는 input이 n과 bad 두가지 값이라고 나오지만 n만 입력받는다 생각하고 풀면 된다. 풀이 방법 풀이 접근 과정 그냥 이진탐색으로 풀면 된다고 생각했다. 아예 예시에서 이진탐색으로 풀도록 알려줘서 접근 자체는 쉬웠다. 최종 소스코드 /* The isBadVersion API is defined in the parent class VersionControl. fun isBadVersion(version: Int) : Boolean {} */ class Solutio..
[LeetCode]Medium - 238. Product of Array Except Self
·
LeetCode/Kotlin | Medium
Medium - 238. Product of Array Except Self https://leetcode.com/problems/product-of-array-except-self/description/ 문제 해석 반복문을 한번만 돌려 나를 제외한 모든 값들의 곱을 구해야 한다. 대신! 나누기 연산을 사용하지 않을 것! 풀이 방법 풀이 접근 과정 인터넷 힌트 참고함. 자신을 기준으로 왼쪽값들의 곱을 구하고 자신을 기준으로 오른쪽값들의 곱을 구해 그 곱을 다시 곱하면 된다. 최종 소스코드 class Solution { fun productExceptSelf(nums: IntArray): IntArray { val answer = IntArray(nums.size) { 1 } for (i in 1 unti..
[LeetCode/Kotlin]442. Find All Duplicates in an Array
·
LeetCode/Kotlin | Medium
LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 해석 배열 안에서 중복된 값만 뽑아내야 한다. 하지만 조건에 O(N)을 사용하라고 하였으므로 반복문은 한번만 돌아야 함. 그리고 추가 공간만 사용해야 한다는 사실을 명심하자 !! 풀이 방법 풀이 접근 과정 반복문을 한 번만 돌기 위해 map을 사용하여 중복된 값을 걸러내고자 하였다. 최종 소스코드 class..
[LeetCode/Kotlin]Medium - 6. Zigzag Conversation
·
LeetCode/Kotlin | Medium
Zigzag Conversion - LeetCode Can you solve this real interview question? Zigzag Conversion - The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I leetcode.com 문제 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: ..
[LeetCode/Kotlin]Medium - 12. Integer to Roman
·
LeetCode/Kotlin | Medium
Integer to Roman - LeetCode Can you solve this real interview question? Integer to Roman - Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000 For example, 2 is written as II in Roman numeral, just tw leetcode.com 문제 Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. SymbolValue I 1 V..
[LeetCode/Kotlin]Medium - 396. Rotate Function
·
LeetCode/Kotlin | Medium
Rotate Function - LeetCode Can you solve this real interview question? Rotate Function - You are given an integer array nums of length n. Assume arrk to be an array obtained by rotating nums by k positions clock-wise. We define the rotation function F on nums as follow: * F(k) = 0 * leetcode.com 문제 You are given an integer array nums of length n. Assume arrk to be an array obtained by rotating n..
[LeetCode/Kotlin]Medium - 2807. Insert Greatest Common Divisors in Linked List
·
LeetCode/Kotlin | Medium
Insert Greatest Common Divisors in Linked List - LeetCode Can you solve this real interview question? Insert Greatest Common Divisors in Linked List - Given the head of a linked list head, in which each node contains an integer value. Between every pair of adjacent nodes, insert a new node with a value equal to t leetcode.com 문제 Given the head of a linked list head, in which each node contains a..
[LeetCode/Kotlin]Easy - 1. Two Sum
·
LeetCode/Kotlin | Easy
Two Sum - LeetCode Can you solve this real interview question? Two Sum - Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not leetcode.com 문제 Given an array of integers nums and an integer target, return indices of the two numbers such that they add..
[LeetCode/Kotlin]Medium - 322. Coin Change
·
LeetCode/Kotlin | Medium
Coin Change - LeetCode Can you solve this real interview question? Coin Change - You are given an integer array coins representing coins of different denominations and an integer amount representing a total amount of money. Return the fewest number of coins that you need to make leetcode.com 문제 You are given an integer array coins representing coins of different denominations and an integer amou..
[LeetCode/Kotlin]Medium - 2690. Happy Students
·
LeetCode/Kotlin | Medium
LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 You are given a 0-indexed integer array nums of length n where n is the total number of students in the class. The class teacher tries to select a group of students so that..
[LeetCode/Kotlin]Easy - 506. Relative Ranks
·
LeetCode/Kotlin | Easy
LeetCode - The World's Leading Online Programming Learning Platform Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com 문제 You are given an integer array score of size n, where score[i] is the score of the ith athlete in a competition. All the scores are guaranteed to be unique. The athletes ar..
뿌꾸 빵
'LeetCode' 카테고리의 글 목록