본문 바로가기

반응형

leetcode

(14)
[LeetCode] Plus One | 난이도: Easy 문제 주어진 정수 배열 digits에 있는 값을 하나의 숫자로 보고 1을 더한 결과를 배열로 리턴한다. 배열에 담기는 숫자는 0부터 9까지이다. 만약 배열에 값 9만 담겨 있다고 가정해보자.( digits[9] ) 여기에 1을 더하면 10이 된다. 따라서 결과는 digits[1, 0]이 된다. 이렇게 자릿수가 바뀔 수 있다는 점에 유의해야 한다. 예시 Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Incrementing by one gives 123 + 1 = 124. Thus, the result should be [1,2,4]. Example 2: Input: dig..
[LeetCode] Length of Last Word | 난이도: Easy 문제 Given a string s consisting of some words separated by some number of spaces, return the length of the last word in the string. A word is a maximal substring consisting of non-space characters only. 예시 Example 1: Input: s = "Hello World" Output: 5 Explanation: The words are "Hello" and "World", both of length 5. Example 2: Input: s = " fly me to the moon " Output: 4 Explanation: The longest w..
[LeetCode] Maximum Subarray | 난이도: Easy 문제 Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum. A subarray is a contiguous part of an array. 주어진 배열의 부분 배열(subarray)의 합 중 최대값을 구하시오. 부분 매열은 연속적인(contiguous) 배열이어야 한다. [1,2,3,4] 배열을 예시로 들자면, [1,2]는 부분배열로 가능하지만, [1,3]또는 [1,4]와 같이 연속적이지 않고 서로 떨어져 있는 값들은 조건에 어긋난다. 예시 Example 1: Input: nums = [-2,1,-3,4,-1,2,1..
[LeetCode] Search Insert Position | 난이도: Easy 문제 Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You must write an algorithm with O(log n) runtime complexity. 배열 nums에 target 값과 일치하는 값이 있다면 해당 값의 인덱스를 리턴. 만약 일치하는 값이 없다면 target 값이 있어야 할 위치의 인덱스를 리턴. O(log n) 시간복잡도를 고려한 알고리즘을 작성해야 한다. 예시 Input: nums = [1,3,5,6], targ..
[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 Element | 난이도: Easy 문제 Given an integer array nums and an integer val, remove all occurrences of val in nums in-place. The relative order of the elements may be changed. 정수 배열 nums, 정수 타입의 변수 val가 있다. 배열 nums의 값 중에서 변수 val의 값과 일치하는 값을 뺀 배열의 길이를 구하시오. 예시 Input: nums = [3,2,2,3], val = 3 Output: 2, nums = [2,2,_,_] 답안 class Solution { public int removeElement(int[] nums, int val) { int result = 0; for(int i = 0; i i+..
[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] Merge Two Sorted List | 난이도: Easy 문제 Merge two sorted linked lists and return it as a sorted list. The list should be made by splicing together the nodes of the first two lists. 주어진 두 연결 리스트(Linked List)를 합쳐서 정렬된 하나의 리스트로 만들어라. 예시 Input: l1 = [1,2,4], l2 = [1,3,4] Output: [1,1,2,3,4,4] Input: l1 = [], l2 = [0] Output: [0] Input: l1 = [], l2 = [] Output: [] 답안 /** * Definition for singly-linked list. * public class ListNode { * i..

반응형