본문 바로가기

반응형

Programming/알고리즘 공부

(15)
[LeetCode] Implement strStr() | 난이도: Easy 문제 Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. Clarification: What should we return when needle is an empty string? This is a great question to ask during an interview. For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf(). haystack에..
[LeetCode] Remove Duplicates from Sorted Array | 난이도: Easy 문제 주어진 내림차순 정수 배열을 중복값 없이 배열의 길이를 구하시오. 예시 Input: nums = [1,1,2] Output: 2, nums = [1,2,_] 답안 class Solution { public int removeDuplicates(int[] nums) { int count = 0; for (int i = 0; i < nums.length; i++ ) { if (nums[i] != nums[count]) { count++; nums[count] = nums[i]; } } return count+1; } } 바로 옆 인덱스 값과 비교하여 값이 중복되지 않는다면 변수 count 1증가시키고 큰 값을 작은 값에 대입한다.
[LeetCode] Longest Common Prefix | 난이도: Easy 문제 Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string "". 문자 배열에서 가장 긴 공통 접두사를 찾는 함수를 만드시오. 공통 접두사라 없을 시 ""를 리턴. 예시 Input: strs = ["flower","flow","flight"] Output: "fl" Input: strs = ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. 제약조건 1
[LeetCode] Roman to Integer | 난이도: Easy 문제 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 로마자를 정수로 변환. 예시 Example 1 Input: s = "III" Output: 3 Example 2 Input: s = "IV" Output: 4 Example 3 Input: s = "IX" Output: 9 Example 4 Input: s = "LVIII" Output: 58 Explanation: L = 50, V= 5, III = 3. Example 5 Input: s = "MCMXCIV" Output: 1994 Explanation: M = ..
[LeetCode] Palindrome Number | 난이도: Easy 문제 Given an integer x, return true if x is palindrome integer. An integer is a palindrome when it reads the same backward as forward. For example, 121 is palindrome while 123 is not. 주어진 정수 x의 값이 palindrome(회문(回文))일 경우 true를 리턴한다. *회문(回文) : madam이나 nurses run처럼 앞에서부터 읽으나 뒤에서부터 읽으나 동일한 단어나 구(출처: 네이버) 예시 Input: x = 121 Output: true 예시 Input: x = -121 Output: false Explanation: From left to right, ..
[LeetCode] Reverse Integer | 난이도: Easy 문제 Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [−2³¹, 2³¹ − 1], then return 0. 주어진 32비트 정수 x값이 있다. 정수 x 자릿수를 역으로 바꾸어라.만약 역으로 바꾼 정수의 값의 범위가 −2³¹, 2³¹ − 1를 넘어갔을 경우, 0을 반환한다. 예시 Input: x = 123 Output: 321 말 그대로 숫자를 반대로 뒤집는 것. 답안 class Solution { public int reverse(int x) { long res = 0; while(x != ..
[LeetCode] Two Sum | 난이도: Easy 문제 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 use the same element twice. You can return the answer in any order. 여기 주어진 정수 배열 nums와 정수 target이 있다. 정수 배열 nums의 인덱스 값의 합이 target과 동일한 경우 해당 인덱스 값을 리턴하시오. 예시 Input: nums = [2,7,11,15], target ..

반응형