[LeetCode/릿코드] 136. Single Number 문제 풀이 1 2 3 4 5 6 7 8 9 10 11 12 import java.util.Arrays; import java.util.Collections; class Solution { public int singleNumber(int[] nums) { int answer = 0; for(int i : nums) { answer ^= i; } return answer; } } Colored by Color Scripter cs [JAVA] LeetCode/Easy 2023. 1. 24. 22:33
[LeetCode/릿코드] 125. Valid Palindrome 문제 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { public boolean isPalindrome(String s) { String lower = s.toLowerCase(); String replace = lower.replaceAll("[^a-z0-9]", ""); StringBuilder sb = new StringBuilder(replace); String reverse = sb.reverse().toString(); if(replace.equals(reverse)){ return true; } return false; } } Colored by Color Scripter cs [JAVA] LeetCode/Easy 2023. 1. 24. 22:29
[LeetCode/릿코드] 121. Best Time to Buy and Sell Stock 문제 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { public int maxProfit(int[] prices) { int buy = prices[0]; int max = 0; for(int i : prices) { if(i - buy > max) { max = i - buy; } if(buy > i) { buy = i; } } return max; } } Colored by Color Scripter cs [JAVA] LeetCode/Easy 2023. 1. 24. 22:26
[LeetCode/릿코드] 119. Pascal's Triangle II 문제 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 import java.util.ArrayList; import java.util.List; class Solution { public List getRow(int rowIndex) { List first = new ArrayList(); first.add(1); List second = new ArrayList(); second.add(1); second.add(1); if(rowIndex == 0) return first; if(rowIndex == 1) return second; List previous = new ArrayList();.. [JAVA] LeetCode/Easy 2023. 1. 24. 22:24
[LeetCode/릿코드] 118. Pascal's Triangle 문제 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 import java.util.ArrayList; import java.util.List; class Solution { public List generate(int numRows) { List answer = new ArrayList(); List first = new ArrayList(); first.add(1); List second = new ArrayList(); second.add(1); second.add(1); if(numRows == 1) { answer.add(first); ret.. [JAVA] LeetCode/Easy 2023. 1. 24. 22:22
[LeetCode/릿코드] 88. Merge Sorted Array 문제 풀이 1 2 3 4 5 6 7 8 9 10 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int index = 0; for(int i=m; i [JAVA] LeetCode/Easy 2023. 1. 24. 22:19
[LeetCode/릿코드] 70. Climbing Stairs 문제 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 class Solution { public int climbStairs(int n) { if(n == 1) return 1; int[] arr = new int[n+1]; arr[1]=1; arr[2]=2; for(int i=3; i [JAVA] LeetCode/Easy 2023. 1. 24. 18:56
[LeetCode/릿코드] 69. Sqrt(x) 문제 풀이 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { public int mySqrt(int x) { int answer = 0; for(long i=1; i x) { answer = (int) (i-1); break; }else if(i * i == x) { answer = (int) i; break; } } return answer; } } cs [JAVA] LeetCode/Easy 2023. 1. 24. 18:53