Algorithm/LeetCode
[LeetCode] - Water Bottles
물병으로 물사먹기 문제 물병과 교환비율이 주어지고 물병을 다 마시고 난 빈병을 교환 하여 다시 물병을 구할 수 있다. 이를 반복하여 마신 총 병의 갯수를 구하라. Input: numBottles = 9, numExchange = 3 Output: 13 Explanation: You can exchange 3 empty bottles to get 1 full water bottle. Number of water bottles you can drink: 9 + 3 + 1 = 13. Constraints: 1 = numExchange){ numBottles = exchange(emptyBottles, numExchange); int temp = numBottles + (emptyBottles % numExch..
[LeetCode] - happy number
Write an algorithm to determine if a number n is happy. A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits. Repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy. R..
[LeetCode] - Missing Number
0~n까지의 범위를 가지고 있는 nums 배열이 있다. 이 배열에서 빠진수를 구하여 리턴하시오 공간복잡도 O(1), 시간복잡도 O(N) Constraints: n == nums.length 1
[LeetCode] - Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Clarification: Confused why the returned value is an integer but your ..
[LeetCode] Longest common prefix
단어가 들어있는 String 배열에서 가장 긴 공통된 접두어를 구하라. sudo code 가장 긴 단어와 가장 짧은 단어를 고르고 가장 짧은 단어의 길이만큼 긴 단어의 앞에서 부터 비교 차감하면서 비교 같지 않다면 "" 리턴 1차 시도있는 String 배열에서 가장 긴 공통된 접두어를 구하라. sudo code 가장 긴 단어와 가장 짧은 단어를 고르고 가장 짧은 단어의 길이만큼 긴 단어의 앞에서 부터 비교 차감하면서 비교 같지 않다면 "" 리턴 1차 시도 class Solution { public String getShortestWord(String[] strs) { if (strs == null || strs.length < 1) { return ""; } String shortestWord = str..
[LeetCode] - Reverse Linked List
Single LinkedList 순서를 역순으로 /* 일단 순회부터 while(head.next != null) { System.out.println(head.val); head = head.next; 1 = 2, 2 = 3, 3 = 4, 4 = 5, 5.next = null } */ // return head; /* public class Main { public static void main(String[] args) { ListNode head = new ListNode(5,null); head = new ListNode(4,head); head = new ListNode(3,head); head = new ListNode(2,head); head = new ListNode(1,head); List..