joonbread의 등록된 링크

키자드에 등록된 총 1673개의 포스트를 확인하실 수 있습니다.

Naver Blog

JAVA_Find Pivot Index_LeetCode 724

JAVA_Find Pivot Index_LeetCode 724 풀이 class Solution { public int pivotIndex(int[] nums) { int total = 0, temp = 0; for (int num : nums) total += num; for (int i = 0; i < nums.length; temp += nums[i++]) if (nums[i]==total-2*temp) return i; return -1; } } * 출처 Find Pivot Index - LeetCode Can you solve this real interview question? Find Pivot Index - Given an array of integers nums, calculate the pivot index of this array. The pivot index is the index where the sum of all the numbers strictly to t

Naver Blog

JAVA_Two Sum IV - Input is a BST_LeetCode 653

JAVA_Two Sum IV - Input is a BST_LeetCode 653 풀이 class Solution { Set<Integer> res = new HashSet<>(); public boolean findTarget(TreeNode root, int k) { if(root == null) return false; if(res.contains(k-root.val)) return true; res.add(root.val); return findTarget(root.left,k) || findTarget(root.right,k); } } * 출처 Two Sum IV - Input is a BST - LeetCode Two Sum IV - Input is a BST - Given the root of a binary search tree and an integer k, return true if there exist two elements in the BST such that

Naver Blog

JAVA_Robot Return to Origin_LeetCode 657

JAVA_Robot Return to Origin_LeetCode 657 풀이 class Solution { public boolean judgeCircle(String moves) { int[] ch = new int[86]; for (char d : moves.toCharArray()) { ch[d]++; } return ch[68] == ch[85] && ch[76] == ch[82]; } } * 출처 Robot Return to Origin - LeetCode Robot Return to Origin - There is a robot starting at the position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves. You are given a string moves that r

Naver Blog

JAVA_Image Smoother_LeetCode 661

JAVA_Image Smoother_LeetCode 661 풀이 class Solution { public int[][] imageSmoother(int[][] img) { int[][] result = new int[img.length][img[0].length]; for(int i =0; i< img.length; i++){ for(int j=0; j<img[i].length; j++){ result[i][j] = findAverage(img, i, j); } } return result; } private static int findAverage(int[][] img, int row, int col){ int tot = 0; int totPoint = 0; for(int i = row - 1; i <= row+1; i++){ for(int j=col-1; j <= col +1; j++){ if(i >=0 && i < img.length && j >=0 && j < img[i].

Naver Blog

JAVA_Second Minimum Node In a Binary Tree_LeetCode 671

JAVA_Second Minimum Node In a Binary Tree_LeetCode 671 풀이 class Solution { int min = Integer.MAX_VALUE; int min2 = Integer.MAX_VALUE; boolean chk = false; public int findSecondMinimumValue(TreeNode root) { if(root == null) return 0; if(root.val < min) { min = root.val; } if(root.val > min && root.val <= min2) { chk = true; min2 = root.val; } findSecondMinimumValue(root.left); findSecondMinimumValue(root.right); return !chk ? -1 : min2 ; } } * 출처 Second Minimum Node In a Binary Tree - LeetCode Se

Naver Blog

JAVA_Longest Continuous Increasing Subsequence_LeetCode 674

JAVA_Longest Continuous Increasing Subsequence_LeetCode 674 풀이 class Solution { public int findLengthOfLCIS(int[] nums) { int num = 1; int num2 = 1; for(int i = 1; i < nums.length; i++) { num2 = nums[i] > nums[i - 1] ? num2 + 1 : 1; num = Math.max(num, num2); } return nums.length == 0 ? 0 : num; } } * 출처 Longest Continuous Increasing Subsequence - LeetCode Longest Continuous Increasing Subsequence - Given an unsorted array of integers nums, return the length of the longest continuous increasing

Naver Blog

JAVA_Valid Palindrome II_LeetCode 680

JAVA_Valid Palindrome II_LeetCode 680 풀이 class Solution { public boolean validPalindrome(String s) { int left = 0; int right = s.length() - 1; while (left < right) { if (s.charAt(left) != s.charAt(right)) return chk(left + 1, right, s) || chk(left, right - 1, s); left++; right--; } return true; } private boolean chk(int left, int right, String s) { while (left < right) { if (s.charAt(left++) != s.charAt(right--)) return false; } return true; } } * 출처 Valid Palindrome II - LeetCode Valid Palindro

Naver Blog

JAVA_Baseball Game_LeetCode 682

JAVA_Baseball Game_LeetCode 682 풀이 class Solution { public int calPoints(String[] operations) { Stack<Integer> stack = new Stack<>(); for (String s : operations) { if (s.equals("+")) { int a = stack.pop(); int newScore = a + stack.peek(); stack.push(a); stack.push(newScore); }else if (s.equals("D")) { stack.push(2 * stack.peek()); }else if (s.equals("C")) { stack.pop(); }else stack.push(Integer.parseInt(s)); } int totalScore = 0; while (!stack.isEmpty()) totalScore += stack.pop(); return totalSc

Naver Blog

JAVA_Binary Number with Alternating Bits_LeetCode 693

JAVA_Binary Number with Alternating Bits_LeetCode 693 풀이 class Solution { public boolean hasAlternatingBits(int n) { String str = Integer.toBinaryString(n); for(int i = 0;i < str.length() - 1;i++){ if(str.charAt(i) == str.charAt(i + 1)) return false; } return true; } } * 출처 Binary Number with Alternating Bits - LeetCode Binary Number with Alternating Bits - Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values. Example 1:

Naver Blog

JAVA_Count Binary Substrings_LeetCode 696

JAVA_Count Binary Substrings_LeetCode 696 풀이 class Solution { public int countBinarySubstrings(String s) { int num1 = 1, num2 = 0, cnt = 0; for (int i = 1; i < s.length(); i++) { if (s.charAt(i) == s.charAt(i - 1)){ num1++; } else { cnt += Math.min(num1, num2); num2 = num1; num1 = 1; } } return cnt + Math.min(num1, num2); } } * 출처 Count Binary Substrings - LeetCode Count Binary Substrings - Given a binary string s, return the number of non-empty substrings that have the same number of 0's and 1'

Naver Blog

JAVA_Degree of an Array_LeetCode 697

JAVA_Degree of an Array_LeetCode 697 풀이 class Solution { public int findShortestSubArray(int[] nums) { HashMap<Integer,Integer> map = new HashMap<>(); HashMap<Integer,Integer> map2 = new HashMap<>(); int degree = 0; int res = 0; for(int i = 0;i < nums.length;i++){ map2.putIfAbsent(nums[i],i); map.put(nums[i],map.getOrDefault(nums[i],0) + 1); if(map.get(nums[i]) > degree){ degree = map.get(nums[i]); res = i - map2.get(nums[i])+1; }else if(map.get(nums[i])== degree){ res = Math.min(res , i-map2.ge

Naver Blog

JAVA_Construct String from Binary Tree_LeetCode 606

JAVA_Construct String from Binary Tree_LeetCode 606 풀이 class Solution { public String tree2str(TreeNode root) { if(root == null) return ""; if(root.left==null && root.right==null) return root.val + ""; if(root.right == null) return root.val + "(" + tree2str(root.left) + ")"; return root.val + "(" + tree2str(root.left) + ")(" + tree2str(root.right) + ")"; } } * 출처 Construct String from Binary Tree - LeetCode Construct String from Binary Tree - Given the root of a binary tree, construct a string c

Naver Blog

JAVA_Merge Two Binary Trees_LeetCode 617

JAVA_Merge Two Binary Trees_LeetCode 617 풀이 class Solution { public TreeNode mergeTrees(TreeNode root1, TreeNode root2) { if (root1 == null && root2 == null) return null; else if (root1 == null) return root2; else if (root2 == null) return root1; return new TreeNode(root1.val + root2.val, mergeTrees(root1.left, root2.left), mergeTrees(root1.right, root2.right)); } } * 출처 Merge Two Binary Trees - LeetCode Merge Two Binary Trees - You are given two binary trees root1 and root2. Imagine that when y

Naver Blog

JAVA_Maximum Product of Three Numbers_LeetCode 628

JAVA_Maximum Product of Three Numbers_LeetCode 628 풀이 class Solution { public int maximumProduct(int[] nums) { Arrays.sort(nums); int n = nums.length; return Math.max(nums[n-1] * nums[n-2] * nums[n-3] , nums[0] * nums[1] * nums[n-1]); } } * 출처 Maximum Product of Three Numbers - LeetCode Maximum Product of Three Numbers - Given an integer array nums, find three numbers whose product is maximum and return the maximum product. Example 1: Input: nums = [1,2,3] Output: 6 Example 2: Input: nums = [1,2

Naver Blog

JAVA_Average of Levels in Binary Tree_LeetCode 637

JAVA_Average of Levels in Binary Tree_LeetCode 637 풀이 class Solution { public List<Double> averageOfLevels(TreeNode root) { Queue<TreeNode> q = new LinkedList<>(List.of(root)); List<Double> ans = new ArrayList<>(); while (q.size() > 0) { double len = q.size(), row = 0; for (int i = 0; i < len; i++) { TreeNode curr = q.poll(); row += curr.val; if (curr.left != null) q.offer(curr.left); if (curr.right != null) q.offer(curr.right); } ans.add(row/len); } return ans; } } * 출처 Average of Levels in Bin

Naver Blog

JAVA_Maximum Average Subarray I_LeetCode 643

JAVA_Maximum Average Subarray I_LeetCode 643 풀이 class Solution { public double findMaxAverage(int[] nums, int k) { double max = Integer.MIN_VALUE; double avg = 0; double sum = 0; for(int i = 0; i<nums.length; i++) { sum += nums[i]; if(i >= k - 1) { avg = sum / k; max = Math.max(max,avg); sum -= nums[i - (k - 1)]; } } return max; } } * 출처 Maximum Average Subarray I - LeetCode Maximum Average Subarray I - You are given an integer array nums consisting of n elements, and an integer k. Find a contig

Naver Blog

JAVA_Set Mismatch_LeetCode 645

JAVA_Set Mismatch_LeetCode 645 풀이 class Solution { public int[] findErrorNums(int[] nums) { int[] res = new int[2]; for (int i = 0; i < nums.length; i++) { int index = Math.abs(nums[i]) - 1; if (nums[index] < 0) res[0] = (index + 1); else nums[index] = -1 * nums[index]; } for (int i = 0; i < nums.length; i++) { if (nums[i] > 0) res[1] = (i + 1); } return res; } } * 출처 Set Mismatch - LeetCode Set Mismatch - You have a set of integers s, which originally contains all the numbers from 1 to n. Unfor

Naver Blog

JAVA_Reverse String II_LeetCode 541

JAVA_Reverse String II_LeetCode 541 풀이 class Solution { public String reverseStr(String s, int k) { StringBuilder str = new StringBuilder(s); for (int index = 0; index < s.length(); index += k << 1) { int leftIndex = index - 1, rightIndex = Math.min(index + k, s.length()); while (++leftIndex < --rightIndex) { str.setCharAt(leftIndex, s.charAt(rightIndex)); str.setCharAt(rightIndex, s.charAt(leftIndex)); } } return str.toString(); } } * 출처 Reverse String II - LeetCode Reverse String II - Given a

Naver Blog

JAVA_Diameter of Binary Tree_LeetCode 543

JAVA_Diameter of Binary Tree_LeetCode 543 풀이 class Solution { private int len = 0; public int diameterOfBinaryTree(TreeNode root) { diameter(root); return len; } private int diameter(TreeNode root){ if (root == null) return 0; int left_height = diameter(root.left); int right_height = diameter(root.right); len = Math.max(len , left_height+right_height); return Math.max(left_height , right_height) + 1; } } * 출처 Diameter of Binary Tree - LeetCode Diameter of Binary Tree - Given the root of a binary

Naver Blog

JAVA_Student Attendance Record I_LeetCode 551

JAVA_Student Attendance Record I_LeetCode 551 풀이 class Solution { public boolean checkRecord(String s) { return !s.contains("LLL") && s.indexOf('A') == s.lastIndexOf('A'); } } * 출처 Student Attendance Record I - LeetCode Student Attendance Record I - You are given a string s representing an attendance record for a student where each character signifies whether the student was absent, late, or present on that day. The record only contains the following three characters: * 'A': Absent. * 'L': Late.

Naver Blog

JAVA_Reverse Words in a String III_LeetCode 557

JAVA_Reverse Words in a String III_LeetCode 557 풀이 class Solution { public String reverseWords(String s) { String[] str = s.split(" "); for (int i = 0; i < str.length; i++) { str[i] = new StringBuilder(str[i]).reverse().toString(); } return String.join(" ", str); } } * 출처 Reverse Words in a String III - LeetCode Reverse Words in a String III - Given a string s, reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order. Example 1: Inpu

Naver Blog

JAVA_Maximum Depth of N-ary Tree_LeetCode 559

JAVA_Maximum Depth of N-ary Tree_LeetCode 559 풀이 class Solution { public int maxDepth(Node root) { if(root==null) return 0; int max=0; for(Node child:root.children) max=Math.max(max,maxDepth(child)); return max+1; } } * 출처 Maximum Depth of N-ary Tree - LeetCode Maximum Depth of N-ary Tree - Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Nary-Tree input serialization is represented in t

Naver Blog

JAVA_Array Partition_LeetCode 561

JAVA_Array Partition_LeetCode 561 풀이 class Solution { public int arrayPairSum(int[] nums) { Arrays.sort(nums); int result = 0; for(int num = 0; num < nums.length; num += 2){ result += nums[num]; } return result; } } * 출처 Array Partition - LeetCode Array Partition - Given an integer array nums of 2n integers, group these integers into n pairs (a1, b1), (a2, b2), ..., (an, bn) such that the sum of min(ai, bi) for all i is maximized. Return the maximized sum. Example 1: Input: nums = [1,4,3,2] Outp

Naver Blog

JAVA_Relative Ranks_LeetCode 506

JAVA_Relative Ranks_LeetCode 506 풀이 class Solution { public String[] findRelativeRanks(int[] score) { Map<Integer,Integer> map = new TreeMap<>(); for(int i=0 ; i< score.length ; i++){ map.put(score[i],i); } int i = 0; String[] rank = new String[score.length]; for(Integer val: map.values()){ if(i == score.length -3){ rank[val] = "Bronze Medal"; }else if(i == score.length -2){ rank[val] = "Silver Medal"; }else if(i == score.length -1){ rank[val] = "Gold Medal"; }else{ rank[val] = String.valueOf(sc

Naver Blog

JAVA_Teemo Attacking_LeetCode 495

JAVA_Teemo Attacking_LeetCode 495 풀이 class Solution { public int findPoisonedDuration(int[] timeSeries, int duration) { int ans=duration; for(int i=1;i<timeSeries.length;i++){ ans = (timeSeries[i-1] + duration - 1 < timeSeries[i]) ? ans + duration : ans + timeSeries[i] - timeSeries[i-1]; } return ans; } } * 출처 Teemo Attacking - LeetCode Teemo Attacking - Our hero Teemo is attacking an enemy Ashe with poison attacks! When Teemo attacks Ashe, Ashe gets poisoned for a exactly duration seconds. More

Naver Blog

JAVA_Next Greater Element I_LeetCode 496

JAVA_Next Greater Element I_LeetCode 496 풀이 class Solution { public int[] nextGreaterElement(int[] nums1, int[] nums2) { int[] ans = new int[nums1.length]; Stack<Integer> stack = new Stack<>(); HashMap<Integer, Integer> map = new HashMap<>(); for(int num : nums2) { while(!stack.isEmpty() && num > stack.peek()) map.put(stack.pop(), num); stack.add(num); } int i = 0; for(int num : nums1) { ans[i++] = map.getOrDefault(num, -1); } return ans; } } * 출처 Next Greater Element I - LeetCode Next Greater E

Naver Blog

JAVA_Perfect Number_LeetCode 507

JAVA_Perfect Number_LeetCode 507 풀이 class Solution { public boolean checkPerfectNumber(int num) { if(num == 1) return false; int sum = 1; for(int i=2; i<=Math.sqrt(num); i++){ if(num%i == 0) sum+=i + num/i; } return sum == num; } } * 출처 Perfect Number - LeetCode Perfect Number - A perfect number [https://en.wikipedia.org/wiki/Perfect_number] is a positive integer that is equal to the sum of its positive divisors, excluding the number itself. A divisor of an integer x is an integer that can divid

Naver Blog

JAVA_Fibonacci Number_LeetCode 509

JAVA_Fibonacci Number_LeetCode 509 풀이 class Solution { public int fib(int n) { if(n <= 1) return n; int a = 0, b = 1; for(int i = 2;i <= n;i++) { int sum = a + b; a = b; b = sum; } return b; } } * 출처 Fibonacci Number - LeetCode Fibonacci Number - The Fibonacci numbers, commonly denoted F(n) form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1. That is, F(0) = 0, F(1) = 1 F(n) = F(n - 1) + F(n - 2), for n > 1. Given n, c

Naver Blog

JAVA_Detect Capital_LeetCode 520

JAVA_Detect Capital_LeetCode 520 풀이 class Solution { public boolean detectCapitalUse(String word) { int cnt = 0; for(char ch : word.toCharArray()) { if(Character.isUpperCase(ch)) cnt++; } if(cnt == word.length() || cnt == 0) return true; return cnt == 1 && Character.isUpperCase(word.charAt(0)); } } * 출처 Detect Capital - LeetCode Detect Capital - We define the usage of capitals in a word to be right when one of the following cases holds: * All letters in this word are capitals, like "USA". * All

Naver Blog

JAVA_Longest Uncommon Subsequence I_LeetCode 521

JAVA_Longest Uncommon Subsequence I_LeetCode 521 풀이 class Solution { public int findLUSlength(String a, String b) { return a.equals(b) ? -1 : Math.max(a.length(), b.length()); } } * 출처 Longest Uncommon Subsequence I - LeetCode Longest Uncommon Subsequence I - Given two strings a and b, return the length of the longest uncommon subsequence between a and b. If the longest uncommon subsequence does not exist, return -1. An uncommon subsequence between two strings is a string that is a subsequence o

Naver Blog

JAVA_Minimum Absolute Difference in BST_LeetCode 530

JAVA_Minimum Absolute Difference in BST_LeetCode 530 풀이 class Solution { Integer ans = Integer.MAX_VALUE; TreeNode prev; public int getMinimumDifference(TreeNode root) { dfs(root); return ans; } public void dfs(TreeNode node) { if (node == null) return; dfs(node.left); if (prev != null) { ans = Math.min(ans, node.val - prev.val); } prev = node; dfs(node.right); } } * 출처 Minimum Absolute Difference in BST - LeetCode Minimum Absolute Difference in BST - Given the root of a Binary Search Tree (BST)

Naver Blog

JAVA_Hamming Distance_LeetCode 461

JAVA_Hamming Distance_LeetCode 461 풀이 class Solution { public int hammingDistance(int x, int y) { return Integer.bitCount(x^y); } } * 출처 Hamming Distance - LeetCode Hamming Distance - The Hamming distance [https://en.wikipedia.org/wiki/Hamming_distance] between two integers is the number of positions at which the corresponding bits are different. Given two integers x and y, return the Hamming distance between them. Example 1: Input: x = 1, y = 4 Output: 2 Exp... leetcode.com

Naver Blog

JAVA_Island Perimeter_LeetCode 463

JAVA_Island Perimeter_LeetCode 463 풀이 class Solution { public int islandPerimeter(int[][] grid) { int cnt=0; for(int i = 0;i < grid.length;i++){ for(int j = 0;j < grid[i].length;j++){ if(grid[i][j] == 1){ cnt+=4; if(i > 0 && grid[i-1][j] == 1) cnt-=2; if(j > 0 && grid[i][j-1] == 1) cnt-=2; } } } return cnt; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Number Complement_LeetCode 476

JAVA_Number Complement_LeetCode 476 풀이 class Solution { public int findComplement(int num) { return ~num & (Integer.highestOneBit(num) - 1); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_License Key Formatting_LeetCode 482

JAVA_License Key Formatting_LeetCode 482 풀이 class Solution { public String licenseKeyFormatting(String s, int k) { StringBuilder sb = new StringBuilder(); for (int i = s.length() - 1; i >= 0; i--) { if (s.charAt(i) != '-') { if (sb.length() % (k + 1) == k) { sb.append('-'); } sb.append(Character.toUpperCase(s.charAt(i))); } } return sb.reverse().toString(); } } * 출처 License Key Formatting - LeetCode License Key Formatting - You are given a license key represented as a string s that consists of o

Naver Blog

JAVA_Max Consecutive Ones_LeetCode 485

JAVA_Max Consecutive Ones_LeetCode 485 풀이 class Solution { public int findMaxConsecutiveOnes(int[] nums) { int max=0 ,cnt =0; for(int n : nums) max=Math.max(max,cnt = n == 0 ? 0 : cnt+1); return max; } } * 출처 Max Consecutive Ones - LeetCode Max Consecutive Ones - Given a binary array nums, return the maximum number of consecutive 1's in the array. Example 1: Input: nums = [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s. The maximum number of

Naver Blog

JAVA_Construct the Rectangle_LeetCode 492

JAVA_Construct the Rectangle_LeetCode 492 풀이 class Solution { public int[] constructRectangle(int area) { for(int j = (int)Math.sqrt(area);j >= 1;j--){ if(area % j == 0) return new int[]{area / j, j}; } return null; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Keyboard Row_LeetCode 500

JAVA_Keyboard Row_LeetCode 500 풀이 class Solution { public String[] findWords(String[] words) { List<String> result = new ArrayList<>(); String[] levels = {"qwertyuiop", "asdfghjkl", "zxcvbnm"}; for (String str : words) { for (String level : levels) { int lettersCount = str.length(); for (int k = 0; k < str.length(); k++) { char c = Character.toLowerCase(str.charAt(k)); if (level.indexOf(c) < 0) { break; } lettersCount--; } if (lettersCount == 0) { result.add(str); break; } } } return result.toAr

Naver Blog

JAVA_Find Mode in Binary Search Tree_LeetCode 501

JAVA_Find Mode in Binary Search Tree_LeetCode 501 풀이 class Solution { Map<Integer,Integer> map = new HashMap<>(); public int[] findMode(TreeNode root) { dfs(root); int max = 0; List<Integer> k= new ArrayList<>(); for(int i : map.values()) max = Math.max(i,max); for(int key: map.keySet()){ if(map.get(key)==max) k.add(key); } int[] ans = new int[k.size()]; for(int i = 0;i<k.size();i++){ ans[i] = k.get(i); } return ans; } public void dfs(TreeNode root){ if (root==null) return; map.put(root.val,map.

Naver Blog

JAVA_Base 7_LeetCode 504

JAVA_Base 7_LeetCode 504 풀이 class Solution { public String convertToBase7(int num) { return Integer.toString(num, 7); } } * 출처 Base 7 - LeetCode Base 7 - Given an integer num, return a string of its base 7 representation. Example 1: Input: num = 100 Output: "202" Example 2: Input: num = -7 Output: "-10" Constraints: * -107 <= num <= 107 leetcode.com

Naver Blog

JAVA_Fizz Buzz_LeetCode 412

JAVA_Fizz Buzz_LeetCode 412 풀이 class Solution { public List<String> fizzBuzz(int n) { List ans = new ArrayList<>(); for(int i = 1; i <= n; i++){ ans.add( i % 15 == 0 ? "FizzBuzz" : i % 5 == 0 ? "Buzz" : i % 3 == 0 ? "Fizz" : String.valueOf(i) ); } return ans; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Third Maximum Number_LeetCode 414

JAVA_Third Maximum Number_LeetCode 414 풀이 class Solution { public int thirdMax(int[] nums) { Set<Integer> hash = new HashSet<>(); for (int num : nums) { hash.add(num); } if (hash.size() >= 3) { hash.remove(Collections.max(hash)); hash.remove(Collections.max(hash)); } return Collections.max(hash); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Add Strings_LeetCode 415

JAVA_Add Strings_LeetCode 415 풀이 class Solution { public String addStrings(String num1, String num2) { int i = num1.length() - 1; int k = num2.length() - 1; int res = 0; StringBuilder sb = new StringBuilder(); while(i >= 0 || k >= 0){ int n1 = i < 0 ? 0 : num1.charAt(i--) - '0'; int n2 = k < 0 ? 0 : num2.charAt(k--) - '0'; int sum = n1 + n2 + res; sb.append(sum % 10); res = sum > 9 ? 1 : 0; } if(res > 0) sb.append(1); return sb.reverse().toString(); } } * 출처 Level up your coding skills and quick

Naver Blog

JAVA_Number of Segments in a String_LeetCode 434

JAVA_Number of Segments in a String_LeetCode 434 풀이 class Solution { public int countSegments(String s) { return s.trim().length() == 0 ? 0 : s.trim().split("\\s+").length; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Arranging Coins_LeetCode 441

JAVA_Arranging Coins_LeetCode 441 풀이 class Solution { public int arrangeCoins(int n) { int res =0; for(res=1;res<=n;res++){ n=n-res; } return res-1; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Find All Numbers Disappeared in an Array_LeetCode 448

JAVA_Find All Numbers Disappeared in an Array_LeetCode 448 풀이 class Solution { public List<Integer> findDisappearedNumbers(int[] nums) { int[] res = new int[nums.length + 1]; for (int num : nums) { res[num] = num; } List<Integer> result = new ArrayList<>(res.length); for (int i = 1; i < res.length; i++) { if (res[i] == 0) { result.add(i); } } return result; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next

Naver Blog

JAVA_Assign Cookies_LeetCode 455

JAVA_Assign Cookies_LeetCode 455 풀이 class Solution { public int findContentChildren(int[] g, int[] s) { Arrays.sort(g); Arrays.sort(s); int count = 0; for(int i = g.length-1 , j = s.length-1; j >= 0 && i >= 0 ;i--) { if(s[j] >= g[i]) { count++; j--; } } return count; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Repeated Substring Pattern_LeetCode 459

JAVA_Repeated Substring Pattern_LeetCode 459 풀이 class Solution { public boolean repeatedSubstringPattern(String s) { return (s + s).indexOf(s, 1) < s.length(); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Ransom Note_LeetCode 383

JAVA_Ransom Note_LeetCode 383 풀이 class Solution { public boolean canConstruct(String ransomNote, String magazine) { if (ransomNote.length() > magazine.length()) return false; int[] cnt = new int[26]; for (char c : magazine.toCharArray()) cnt[c-'a']++; for (char c : ransomNote.toCharArray()){ if (cnt[c-'a'] == 0) return false; cnt[c-'a']--; } return true; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next int

Naver Blog

JAVA_First Unique Character in a String_LeetCode 387

JAVA_First Unique Character in a String_LeetCode 387 풀이 class Solution { public int firstUniqChar(String s) { for(char c : s.toCharArray()){ int index = s.indexOf(c); int lastIndex = s.lastIndexOf(c); if(index == lastIndex) return index; } return -1; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Find the Difference_LeetCode 389

JAVA_Find the Difference_LeetCode 389 풀이 class Solution { public char findTheDifference(String s, String t) { char c = 0; for(char c1 : s.toCharArray()) c ^= c1; for(char c2 : t.toCharArray()) c ^= c2; return c; } } ^ 연산자(XOR)를 이용한 문제풀이 * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Is Subsequence_LeetCode 392

JAVA_Is Subsequence_LeetCode 392 풀이 class Solution { public boolean isSubsequence(String s, String t) { int j = 0; for(int i = 0; i < t.length() && j < s.length(); i++){ if(s.charAt(j) == t.charAt(i)) j++; } return j == s.length(); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Binary Watch_LeetCode 401

JAVA_Binary Watch_LeetCode 401 풀이 class Solution { public List<String> readBinaryWatch(int turnedOn) { List<String> time = new ArrayList<>(); for (int h=0; h<12; h++){ for (int m=0; m<60; m++){ if (Integer.bitCount(h * 64 + m) == turnedOn) time.add(String.format("%d:%02d", h, m)); } } return time; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Longest Palindrome_LeetCode 409

JAVA_Longest Palindrome_LeetCode 409 풀이 class Solution { public int longestPalindrome(String s) { HashMap<Character, Integer> map = new HashMap(); for(int i=0; i<s.length(); i++){ char temp = s.charAt(i); if(map.get(temp) != null){ map.remove(temp); }else{ map.put(temp, i); } } if(map.size() <= 1) return s.length(); return s.length() - map.size() + 1; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interv

Naver Blog

JAVA_Sum of Left Leaves_LeetCode 404

JAVA_Sum of Left Leaves_LeetCode 404 풀이 class Solution { public int sumOfLeftLeaves(TreeNode root) { if (root == null) return 0; int sum = 0; sum += (root.left != null && root.left.left == null && root.left.right == null) ? root.left.val : sumOfLeftLeaves(root.left); sum += sumOfLeftLeaves(root.right); return sum; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Convert a Number to Hexadecimal_LeetCode 405

JAVA_Convert a Number to Hexadecimal_LeetCode 405 풀이 class Solution { public String toHex(int num) { if (num == 0) return "0"; StringBuilder sb = new StringBuilder(); while(num != 0) { int hexDigit = num & 0xF; if (hexDigit < 10) sb.append(hexDigit); else sb.append((char)('a' + hexDigit - 10)); num = num >>> 4; } return sb.reverse().toString(); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. le

Naver Blog

JAVA_Power of Three_LeetCode 326

JAVA_Power of Three_LeetCode 326 풀이 class Solution { public boolean isPowerOfThree(int n) { return (Math.log10(n) / Math.log10(3)) % 1 == 0; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Counting Bits_LeetCode 338

JAVA_Counting Bits_LeetCode 338 풀이 class Solution { public int[] countBits(int n) { int[] num = new int[n + 1]; for(int i = 1; i <= n; i++){ num[i] = num[i/2]; if(i%2 == 1) num[i]++; } return num; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Power of Four_LeetCode 342

JAVA_Power of Four_LeetCode 342 풀이 class Solution { public boolean isPowerOfFour(int n) { return (Math.log(n) / Math.log(4)) % 1 == 0; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Reverse String_LeetCode 344

JAVA_Reverse String_LeetCode 344 풀이 class Solution { public void reverseString(char[] s) { for(int i=0 ; i <s.length/2 ; i++){ char temp = s[i]; s[i] = s[s.length-1-i]; s[s.length-1-i] = temp; } } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Reverse Vowels of a String_LeetCode 345

JAVA_Reverse Vowels of a String_LeetCode 345 풀이 class Solution { public String reverseVowels(String s) { int x=0; int y=s.length()-1; List<Character> list=Arrays.asList('a','e','i','o','u','A','E','I','O','U'); char[] arr=s.toCharArray(); while(x<y){ if(!list.contains(arr[x])){ x++; } if(!list.contains(arr[y])){ y--; } if(list.contains(arr[x]) && list.contains(arr[y])){ char temp=arr[x]; arr[x]=arr[y]; arr[y]=temp; x++; y--; } } return new String(arr); } } * 출처 Level up your coding skills and qu

Naver Blog

JAVA_Intersection of Two Arrays_LeetCode 349

JAVA_Intersection of Two Arrays_LeetCode 349 풀이 class Solution { public int[] intersection(int[] nums1, int[] nums2) { Set<Integer> set1 = new HashSet(); Set<Integer> set2 = new HashSet(); for(int i : nums1) set1.add(i); for(int i : nums2) set2.add(i); set1.retainAll(set2); int n = set1.size(); int[] res = new int[n]; int index = 0; for(int i : set1) res[index++] = i; return res; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get

Naver Blog

JAVA_Intersection of Two Arrays II_LeetCode 350

JAVA_Intersection of Two Arrays II_LeetCode 350 풀이 class Solution { public int[] intersect(int[] nums1, int[] nums2) { if(nums1.length==0) return nums1; if(nums2.length==0) return nums2; Arrays.sort(nums1); Arrays.sort(nums2); int i=0,j=0,k=0; while(i<nums1.length && j<nums2.length){ if(nums1[i]<nums2[j] ) i++; else if(nums2[j]<nums1[i]) j++; else if(nums1[i]==nums2[j]){ nums1[k++]=nums1[i]; i++; j++; } } return Arrays.copyOfRange(nums1, 0, k); } } * 출처 Level up your coding skills and quickly la

Naver Blog

JAVA_Valid Perfect Square_LeetCode 367

JAVA_Valid Perfect Square_LeetCode 367 풀이 class Solution { public boolean isPerfectSquare(int num) { double sqrt = Math.sqrt(num); return ((sqrt - Math.floor(sqrt)) == 0); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Guess Number Higher or Lower_LeetCode 374

JAVA_Guess Number Higher or Lower_LeetCode 374 풀이 public class Solution extends GuessGame { public int guessNumber(int n) { return binarySearch(1,n); } public int binarySearch(int low,int high){ int mid=low+(high-low)/2; if(guess(mid)==0) return mid; if(guess(mid)==1){ return binarySearch(mid+1,high); }else{ return binarySearch(low,mid-1); } } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetc

Naver Blog

JAVA_Word Pattern_LeetCode 290

JAVA_Word Pattern_LeetCode 290 풀이 class Solution { public boolean wordPattern(String pattern, String s) { if(pattern.length() != s.split(" ").length) return false; for(int i=0; i<pattern.length(); i++){ for(int j=i+1; j<pattern.length(); j++){ if(pattern.charAt(j)==pattern.charAt(i)){ if(!s.split(" ")[j].equals(s.split(" ")[i])) return false; }else if(s.split(" ")[j].equals(s.split(" ")[i])) return false; } } return true; } } * 출처 Level up your coding skills and quickly land a job. This is the b

Naver Blog

JAVA_Nim Game_LeetCode 292

JAVA_Nim Game_LeetCode 292 풀이 class Solution { public boolean canWinNim(int n) { return (n % 4 != 0); } } * 출처 https://leetcode.com/problems/nim-game/ Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_First Bad Version_LeetCode 278

JAVA_First Bad Version_LeetCode 278 풀이 public class Solution extends VersionControl { public int firstBadVersion(int n) { int start = 1; int end = n; while (start <= end) { int mid = start + (end - start) / 2; if (isBadVersion(mid)) { end = mid - 1; } else { start = mid + 1; } } return start; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Range Sum Query_Immutable_LeetCode 303

JAVA_Range Sum Query_Immutable_LeetCode 303 풀이 class NumArray { int[] num; public NumArray(int[] nums) { num = nums; for (int i = 1; i < nums.length; i++) { num[i] += num[i - 1]; } } public int sumRange(int left, int right) { return left == 0 ? num[right] : num[right] - num[left - 1]; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Move Zeroes_LeetCode 283

JAVA_Move Zeroes_LeetCode 283 풀이 class Solution { public void moveZeroes(int[] nums) { for (int i = 0, j = 0; j < nums.length; j++) { if (nums[j] != 0) { nums[i] = nums[j]; if (i++ != j) nums[j] = 0; } } } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Power of Two_LeetCode 231

JAVA_Power of Two_LeetCode 231 풀이 class Solution { public boolean isPowerOfTwo(int n) { if(n<=0) return false; return (n & (n-1)) == 0; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Implement Queue using Stacks_LeetCode 232

JAVA_Implement Queue using Stacks_LeetCode 232 풀이 class MyQueue { Stack<Integer> s1 = new Stack<>(); Stack<Integer> s2 = new Stack<>(); public void push(int x) { while(!s1.isEmpty()){ s2.push(s1.pop()); } s1.push(x); while(!s2.isEmpty()){ s1.push(s2.pop()); } } public int pop() { if(s1.isEmpty()){ return -1; } return s1.pop(); } public int peek() { if(s1.isEmpty()){ return -1; } return s1.peek(); } public boolean empty() { return s1.isEmpty(); } } * 출처 Level up your coding skills and quickly lan

Naver Blog

JAVA_Palindrome Linked List_LeetCode 234

JAVA_Palindrome Linked List_LeetCode 234 풀이 class Solution { ListNode node; public boolean isPalindrome(ListNode head) { if(head == null) return true; if(node == null) node = head; boolean bool = isPalindrome(head.next); if(head.val == node.val) node = node.next; else bool = false; return bool; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Valid Anagram_LeetCode 242

JAVA_Valid Anagram_LeetCode 242 풀이 class Solution { public boolean isAnagram(String s, String t) { int[] alphabet = new int[26]; for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++; for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--; for (int i : alphabet) if (i != 0) return false; return true; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Binary Tree Paths_LeetCode 257

JAVA_Binary Tree Paths_LeetCode 257 풀이 class Solution { public List<String> binaryTreePaths(TreeNode root) { List<String> answer = new ArrayList<String>(); if (root != null) searchBT(root, "", answer); return answer; } private void searchBT(TreeNode root, String path, List<String> answer) { if (root.left == null && root.right == null) answer.add(path + root.val); if (root.left != null) searchBT(root.left, path + root.val + "->", answer); if (root.right != null) searchBT(root.right, path + root.v

Naver Blog

JAVA_Add Digits_LeetCode 258

JAVA_Add Digits_LeetCode 258 풀이 class Solution { public int addDigits(int num) { return 1 + (num - 1) % 9; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Ugly Number_LeetCode 263

JAVA_Ugly Number_LeetCode 263 풀이 class Solution { public boolean isUgly(int n) { if (n > 0){ for (int i : new int[] { 2, 3, 5 }) { while (n % i == 0) n /= i; } } return n == 1; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Missing Number_LeetCode 268

JAVA_Missing Number_LeetCode 268 풀이 class Solution { public int missingNumber(int[] nums) { int res = nums.length; for(int i=0; i<nums.length; i++){ res ^= i; res ^= nums[i]; } return res; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Summary Ranges_LeetCode 228

JAVA_Summary Ranges_LeetCode 228 풀이 class Solution { public List<String> summaryRanges(int[] nums) { List<String> list = new ArrayList<>(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < nums.length; i++) { int start = nums[i]; while (i + 1 < nums.length && (nums[i + 1] - nums[i]) == 1) i++; sb.append(start); if (nums[i] != start) sb.append("->").append(nums[i]); list.add(sb.toString()); sb.setLength(0); } return list; } } * 출처 https://leetcode.com/problems/summary-ranges/ Level up

Naver Blog

JAVA_Minimum Depth of Binary Tree_LeetCode 111

JAVA_Minimum Depth of Binary Tree_LeetCode 111 Minimum Depth of Binary Tree 풀이 class Solution { public int minDepth(TreeNode root) { if(root == null) return 0; int left = minDepth(root.left); int right = minDepth(root.right); return (left == 0 || right == 0) ? left + right + 1 : Math.min(left, right) + 1; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Path Sum_LeetCode 112

JAVA_Path Sum_LeetCode 112 풀이 class Solution { public boolean hasPathSum(TreeNode root, int sum) { if(root==null)return false; sum = sum - root.val; if(root.left==null && root.right==null){ boolean bool = sum == 0 ? true : false; return bool; } return hasPathSum(root.left,sum) || hasPathSum(root.right,sum); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Pascal's Triangle_LeetCode 118

JAVA_Pascal's Triangle_LeetCode 118 Pascal's Triangle 풀이 class Solution { public List<List<Integer>> generate(int numRows) { List<List<Integer>> res = new ArrayList<List<Integer>>(); for (int i = 0; i < numRows; i++) { List<Integer> row = new ArrayList<Integer>(); for (int j = 0; j <= i; j++) if (j == 0 || j == i){ row.add(1); }else{ row.add(res.get(i-1).get(j-1) + res.get(i-1).get(j)); } res.add(row); } return res; } } * 출처 Level up your coding skills and quickly land a job. This is the best pl

Naver Blog

JAVA_Pascal's Triangle II_LeetCode 119

JAVA_Pascal's Triangle II_LeetCode 119 Pascal's Triangle II 풀이 class Solution { public List<Integer> getRow(int rowIndex) { List<Integer> ret = new LinkedList<Integer>(); for (int row = 0; row <= rowIndex; row++) { ret.add(0, 1); for (int i = 1; i < row; i++) ret.set(i, ret.get(i) + ret.get(i + 1)); } return ret; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Best Time to Buy and Sell Stock_LeetCode 121

JAVA_Best Time to Buy and Sell Stock_LeetCode 121 Best Time to Buy and Sell Stock 풀이 class Solution { public int maxProfit(int[] prices) { int max=0,min=prices[0]; for(int i=1;i<prices.length;i++) { if(min<prices[i]){ max=Math.max(prices[i]-min,max); }else{ min=prices[i]; } } return max; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Remove Duplicates from Sorted List_LeetCode 83

JAVA_Remove Duplicates from Sorted List_LeetCode 83 Remove Duplicates from Sorted List 풀이 class Solution { public ListNode deleteDuplicates(ListNode head) { if (head == null){ return head; } head.next = deleteDuplicates(head.next); return head.next != null && head.val == head.next.val ? head.next : head; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Merge Sorted Array_LeetCode 88

JAVA_Merge Sorted Array_LeetCode 88 Merge Sorted Array 풀이 class Solution { public void merge(int[] nums1, int m, int[] nums2, int n) { int i = m-1; int j = n-1; int k = m+n-1; while(i>=0 && j>=0){ nums1[k--] = nums1[i] < nums2[j] ? nums2[j--] : nums1[i--]; } while(j>=0){ nums1[k--] = nums2[j--]; } } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Binary Tree Inorder Traversal_LeetCode 94

JAVA_Binary Tree Inorder Traversal_LeetCode 94 Binary Tree Inorder Traversal 풀이 class Solution { public List<Integer> inorderTraversal(TreeNode root) { List<Integer> inorder = new ArrayList<>(); inorderTraversal(root, inorder); return inorder; } public void inorderTraversal(TreeNode root, List<Integer> inorder) { if (root == null) return; inorderTraversal(root.left, inorder); inorder.add(root.val); inorderTraversal(root.right, inorder); } } * 출처 Level up your coding skills and quickly land a job

Naver Blog

JAVA_Same Tree_LeetCode 100

JAVA_Same Tree_LeetCode 100 Same Tree 풀이 class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { boolean res = (p == null || q == null) ? p == q : p.val == q.val && isSameTree(p.left,q.left) && isSameTree(p.right,q.right); return res; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Symmetric Tree_LeetCode 101

JAVA_Symmetric Tree_LeetCode 101 Symmetric Tree 풀이 class Solution { public boolean isSymmetric(TreeNode root) { return symmetricTree( root.left, root.right ); } public boolean symmetricTree(TreeNode left, TreeNode right) { if( left == null || right == null ) return left == right; if( left.val != right.val ) return false; return symmetricTree(left.left, right.right) && symmetricTree(left.right, right.left); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to exp

Naver Blog

JAVA_Maximum Depth of Binary Tree_LeetCode 104

JAVA_Maximum Depth of Binary Tree_LeetCode 104 Maximum Depth of Binary Tree 풀이 class Solution { public int maxDepth(TreeNode root) { return root==null ? 0 : 1 + Math.max(maxDepth(root.left),maxDepth(root.right)); } } * 출처 https://leetcode.com/problems/maximum-depth-of-binary-tree/ Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Convert Sorted Array to Binary Search Tree_LeetCode 108

JAVA_Convert Sorted Array to Binary Search Tree_LeetCode 108 Convert Sorted Array to Binary Search Tree 풀이 class Solution { public TreeNode sortedArrayToBST(int[] nums) { return addNode(nums, 0, nums.length - 1); } public TreeNode addNode(int[] nums, int left, int right) { if (left > right) return null; int mid = left + ((right - left) / 2); TreeNode tree = new TreeNode(nums[mid]); tree.left = addNode(nums, left, mid - 1); tree.right = addNode(nums, mid + 1, right); return tree; } } * 출처 Level u

Naver Blog

JAVA_Balanced Binary Tree_LeetCode 110

JAVA_Balanced Binary Tree_LeetCode 110 Balanced Binary Tree 풀이 class Solution { public boolean isBalanced(TreeNode root) { return tree(root) != -1; } private int tree(TreeNode node) { if(node == null) return 1; int v1 = tree(node.left); int v2 = tree(node.right); if(v1 == -1 || v2 == -1) return -1; return Math.abs(v1 - v2) > 1 ? -1 : ((int) Math.max(v1, v2)) + 1; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your

Naver Blog

JAVA_Remove Element_LeetCode 27

JAVA_Remove Element_LeetCode 27 Remove Element 풀이 class Solution { public int removeElement(int[] nums, int val) { int i = 0; for (int n : nums){ if (n != val){ nums[i++] = n; } } return i; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Search Insert Position_LeetCode 35

JAVA_Search Insert Position_LeetCode 35 Search Insert Position 풀이 class Solution { public int searchInsert(int[] nums, int target) { for(int i = 0; i < nums.length; i++){ if(nums[i] >= target) { return i; } } return nums.length; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Length of Last Word_LeetCode 58

JAVA_Length of Last Word_LeetCode 58 Length of Last Word 풀이 class Solution { public int lengthOfLastWord(String s) { String[] words = s.split(" "); return words[words.length-1].length(); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Plus One_LeetCode 66

JAVA_Plus One_LeetCode 66 Plus One 풀이 class Solution { public int[] plusOne(int[] digits) { for(int i = digits.length - 1; i >= 0; i--){ if(++digits[i] != 10){ return digits; } digits[i] = 0; } int [] res = new int[digits.length + 1]; res[0] = 1; return res; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Add Binary_LeetCode 67

JAVA_Add Binary_LeetCode 67 Add Binary 풀이 import java.math.BigInteger; class Solution { public String addBinary(String a, String b) { BigInteger n1 = new BigInteger(a,2); BigInteger n2 = new BigInteger(b,2); BigInteger sum = n1.add(n2); return sum.toString(2); } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Sqrt(x)_LeetCode 69

JAVA_Sqrt(x)_LeetCode 69 Sqrt(x) 풀이 class Solution { public int mySqrt(int x) { if(x<2) return x; long r = x/2; while (r*r > x) r = (r + x/r) / 2; return (int)r; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

JAVA_Climbing Stairs_LeetCode 70

JAVA_Climbing Stairs_LeetCode 70 Climbing Stairs 풀이 class Solution { public int climbStairs(int n) { if (n <= 2) { return n; } int prev1 = 1; int prev2 = 2; for (int i = 3; i <= n; i++) { int newValue = prev1 + prev2; prev1 = prev2; prev2 = newValue; } return prev2; } } * 출처 Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview. leetcode.com

Naver Blog

Python_Arrays_HackerRank

Python_Arrays_HackerRank Arrays 풀이 import numpy def arrays(arr): return(numpy.array(list(reversed(arr)), float)) arr = input().strip().split(' ') result = arrays(arr) print(result) * 출처 Arrays | HackerRank Convert a list to an array using the NumPy package. www.hackerrank.com

1 2 3 4 5 6 7 8 9 10