본문 바로가기

easy9

[Algorithm][Array] 문제 풀이 #11 - Valid Palindrome 문제 링크 https://leetcode.com/problems/valid-palindrome/ 내 풀이 public boolean isPalindrome(String s) { if(s.length() 2022. 7. 20.
[Algorithm][Array] 문제 풀이 #10 - Valid Anagram 문제 링크 https://leetcode.com/problems/valid-anagram/ 내 풀이 public boolean isAnagram(String s, String t) { if(s.length() != t.length()) return false; int[] alphabet = new int[256]; for(int i = 0; i < s.length(); i++) { alphabet[s.charAt(i)]++; } for(int i = 0; i < t.length(); i++) { alphabet[t.charAt(i)]--; } for(int i = 0; i < alphabet.length; i++) { if(alphabet[i] != 0) return false; } return true.. 2022. 7. 20.
[Algorithm][Array] 문제 풀이 #5 - Contains Duplicate 문제 링크 https://leetcode.com/problems/contains-duplicate/ 내 문제 풀이 public static boolean containsDuplicate(int[] nums) { Set set = new HashSet(); for(int num : nums) { if(!set.add(num)) return true; } return false; } 결과 Medium 난이도도 이렇게 쉽게 풀리는 날이 오겠지?.... 2022. 7. 17.
[Algorithm][Array] 문제 풀이 #3 - Best Time to Buy and Sell Stock 문제 링크 https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ 내 코드 public int maxProfit(int[] prices) { if (prices == null || prices.length == 0) return 0; int min = prices[0]; int benefit = 0; for (int i = 1; i = prices[i]) { min = prices[i]; continue; } benefit = Math.max(prices[i] - min, benefit); } return benefit; } 테스트 케이스 결과 통계 아직은 쉬운 문제들이라 괜찮지만 틈틈히 .. 2022. 7. 17.