반응형

개발/코딩테스트 29

[LeetCode] 69. Sqrt(x) #Easy #Java

문제Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator. For example, do not use pow(x, 0.5) in c++ or x ** 0.5 in python.예제 1 Input: x = 4Output: 2// Explanation: The square root of 4 is 2, so we return 2. 예제 2Input: x = 8Output: 2// Explana..

[LeetCode] 67. Add Binary #Easy #Java

문제Given two binary strings a and b, return their sum as a binary string.예제 1Input: a = "11", b = "1"Output: "100"예제 2Input: a = "1010", b = "1011"Output: "10101"제약조건✅ 1 ✅ a and b consist only of '0' or '1' characters. ✅ Each string does not contain leading zeros except for the zero itself. 문제풀이 1import java.math.BigInteger;class Solution {    public String addBinary(String a, String b) {        ..

[LeetCode] 58. Length of Last Word #Easy #Java

문제Given a string s consisting of words and spaces, return the length of the last word in the string.A word is a maximal substring consisting of non-space characters only.예제 1Input: s = "Hello World"Output: 5Explanation: The last word is "World" with length 5.예제 2Input: s = " fly me to the moon "Output: 4Explanation: The last word is "moon" with length 4.예제 3Input: s = "luffy is still joyb..

[LeetCode] 35. Search Insert Position #Easy #Java

문제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.예제1Input: nums = [1,3,5,6], target = 5Output: 2예제2Input: nums = [1,3,5,6], target = 2Output: 1예제3Input: nums = [1,3,5,6], target = 7Output: 4제약조건✅ 1 ✅ -104 ✅ ..

[LeetCode] 28. Find the Index of the First Occurrence in a String #Easy #Java

문제Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.예제 1Input: haystack = "sadbutsad", needle = "sad"Output: 0// Explanation: "sad" occurs at index 0 and 6.// The first occurrence is at index 0, so we return 0.예제 2 Input: haystack = "leetcode", needle = "leeto"Output: -1// Explanation: "leeto" did not oc..

[LeetCode] 26. Remove Duplicates from Sorted Array #Easy #Java

문제Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums. Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things: Change the array nums such that the..

[LeetCode] 21. Merge Two Sorted Lists #Easy #Java

문제You are given the heads of two sorted linked lists list1 and list2.Merge the two lists into one sorted list. The list should be made by splicing together the nodes of the first two lists.Return the head of the merged linked list.예제 1Input: list1 = [1,2,4], list2 = [1,3,4]Output: [1,1,2,3,4,4]예제 2Input: list1 = [], list2 = []Output: []예제 3Input: list1 = [], list2 = [0]Output: [0]제약조건✅ The numbe..