[LeetCode] 58. Length of Last Word (Easy)
문제로 내가 푼 것 1 2 3 4 5 6 class Solution { public int lengthOfLastWord(String s) { String[] array = s.trim().split(" "); return array[array.length-1].length(); } }
문제로 내가 푼 것 1 2 3 4 5 6 class Solution { public int lengthOfLastWord(String s) { String[] array = s.trim().split(" "); return array[array.length-1].length(); } }
문제로 내가 푼 것 1 2 3 4 5 6 7 8 9 10 11 class Solution { public int strStr(String haystack, String needle) { for(int i = 0; i <= haystack.length() - needle.length(); i++) { String temp = haystack.substring(i, i + needle.length()); if(temp.equals(needle)) return i; } return -1; } } 처음으로 Submit 한 번에 통과했다
문제로 내가 푼 것 1 2 3 4 5 6 7 8 9 10 11 class Solution { public int strStr(String haystack, String needle) { for(int i = 0; i <= haystack.length() - needle.length(); i++) { String temp = haystack.substring(i, i + needle.length()); if(temp.equals(needle)) return i; } return -1; } }
문제로 내가 푼 것 1 2 3 4 5 6 7 8 9 10 11 12 class Solution { public int removeElement(int[] nums, int val) { int count = 0; for(int i = 0; i < nums.length-1; i ++) { if(nums[i] != val) { nums[count++] = nums[i]; } } return count; } } 답안1 1 2 3 4 5 6 7 8 9 10 11 12 class Solution { public int removeElement(int[] nums, int val) { int index = 0; for (int i = 0; i < nums.length; i++) { if (nums[i] != val) { nums[index] = nums[i]; index++; } } return index; } } 풀다보니 무슨 말인지 이해가 가는 것 같다
문제로 내가 푼 것 틀림 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 class Solution { public int removeDuplicates(int[] nums) { Set<Integer> remover = new HashSet<>(); for(int i = 0; i < nums.length; i++) { remover.add(nums[i]); } return remover.size(); } } ## 답안1 ```java class Solution { public int removeDuplicates(int[] nums) { int j = 1; for (int i = 1; i < nums.length; i++) { if (nums[i] != nums[i - 1]) { nums[j] = nums[i]; j++; } } return j; } } 참고 배열도 수정해야 정답처리된다
문제로 내가 푼 것 못품 답안1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode result = new ListNode(); ListNode cur = result; while(list1 != null && list2 != null) { if(list1.val > list2.val) { cur.next = list2; list2 = list2.next; } else { cur.next = list1; list1 = list1.next; } cur = cur.next; } cur.next = (list1 != null) ? list1 : list2; return result.next; } } 답안2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { if(list1 == null || list2 == null) { return (list1 != null) ? list1 : list2; } if(list1.val > list2.val) { ListNode temp = list1; list1 = list2; list2 = temp; } list1.next = mergeTwoLists(list1.next, list2); return list1; } } 2번 답을 보고 벽을 느꼈다. 난 범부다.
문제로 내가 푼 것 못품 답안1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class Solution { public String longestCommonPrefix(String[] strs) { StringBuilder ans = new StringBuilder(); Arrays.sort(strs); String first = strs[0]; String last = strs[strs.length-1]; for(int i = 0; i < Math.min(first.length(), last.length()); i++){ if(first.charAt(i) != last.charAt(i)) { return ans.toString(); } ans.append(first.charAt(i)); } return ans.toString(); } } 참고 문서로(java.util.Arrays){: target="_blank"} ...
문제로 내가 푼 것 틀림 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 class Solution { public boolean isValid(String s) { Map<Character, Integer> patterns = new HashMap(); Stack<Character> stack = new Stack<>(); int sum = 0; patterns.put('(', 1); patterns.put(')', 2); patterns.put('{', 3); patterns.put('}', 4); patterns.put('[', 5); patterns.put(']', 6); if(s.length() % 2 == 1) return false; for(int i = 0; i < s.length(); i++, i++) { if(patterns.get(s.charAt(i)) % 2 == 1) { stack.push(s.charAt(i)); // sum += patterns.get(s.charAt(i)).intValue() + 1; } else if(patterns.get(s.charAt(i)) % 2 == 0) { // sum -= patterns.get(s.charAt(i)).intValue(); if(stack.isEmpty()) return false; Character temp = stack.pop(); if(patterns.get(temp) + 1 != patterns.get(s.charAt(i))) return false; } } if(sum != 0) return false; return true; } } 답안1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<>(); for(char c : s.toCharArray()) { if(c == '(') stack.push(')'); else if(c == '[') stack.push(']'); else if(c == '{') stack.push('}'); else if(stack.isEmpty()) return false; else if(stack.pop() != c) return false; } return stack.isEmpty(); } } 참고 1 else if(stack.isEmpty()) return false; 답안에서 위 조건과 아래 조건의 순서가 바뀌면 에러가 발생한다. ...
문제로 내가 푼 것 못품 답안1 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 class Solution { public int romanToInt (String s){ Map<Character, Integer> m = new HashMap<>(); m.put('I', 1); m.put('V', 5); m.put('X', 10); m.put('L', 50); m.put('C', 100); m.put('D', 500); m.put('M', 1000); int ans = 0; for (int i = 0; i < s.length(); i++) { if(i < s.length() - 1 && m.get(s.charAt(i)) < m.get(s.charAt(i+1))){ ans -= m.get(s.charAt(i)); } else { ans += m.get(s.charAt(i)); } } return ans; } }
layout: post title: 객단가 #image: path: /assets/img/blog/jeremy-bishop@0,5x.jpg description: > sitemap: false 미용실 유목민은 머리카락를 자르는 주기가 올 때마다 곤혹스럽지 않을 수 없다. 장인어른은 이발과 염색까지 이만원에 해주는 맛집(?) 있고 동서(사실 형님으로 모시고 있다)는 남양주에서 잠실까지 원정을 떠난다고 한다. ...