본문 바로가기

알고리즘55

Valid Anagram https://leetcode.com/problems/valid-anagram/ Valid Anagram - LeetCode 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 anagram은 숫서만 다른 문자를 말한다. dog god 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { public boolean isAnagram(String s, String t) { if (s == null || s == null || s.length().. 2020. 8. 25.
K th largest element in an Array https://leetcode.com/problems/kth-largest-element-in-an-array/submissions/ Kth Largest Element in an Array - LeetCode 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 1 2 3 4 5 6 class Solution { public int findKthLargest(int[] nums, int k) { Arrays.sort(nums); return nums[nums.length - k]; .. 2020. 8. 25.
Unique한 문자인가 1. Hash Map 1 2 3 4 5 6 7 8 9 10 11 12 13 14 public boolean solve(String s) { Map map = new HashMap(); char[] chars = s.toCharArray(); for (char c : chars) { if (map.containsKey(c)) { return false; } else { map.put(c, map.getOrDefault(c, 0) + 1); } } return true; } Colored by Color Scripter cs 2. Hash Set 1 2 3 4 5 6 7 8 9 10 11 12 13 public boolean solve(String s) { HashSet set = new HashSet();.. 2020. 8. 24.
String to Num 문자열을 정수로 바꾸기 - Integer.parse를 쓰지않는다. 1 2 3 4 5 6 7 8 9 10 11 12 13 public int solve(String stringNum) { int intNum = 0; char[] chars = stringNum.toCharArray(); for (char c : chars) { intNum = intNum * 10; intNum += c - '0'; } return intNum; } } Colored by Color Scripter cs 1. toChar로 변형한다. 2. 10자리씩 늘어나기때문에 10씩 곱한다. 3. char에 -'0'을 하면 parse를 할 필요없다. 2020. 8. 24.