[LeetCode] 67. Add Binary (Easy)

문제로 내가 푼 것 틀림 정답 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 String addBinary(String a, String b) { StringBuilder result = new StringBuilder(); int i = a.length() - 1; int j = b.length() - 1; int carry = 0; while (i >= 0 || j >= 0) { int digitA = (i >= 0) ? a.charAt(i) - '0' : 0; int digitB = (j >= 0) ? b.charAt(j) - '0' : 0; int sum = digitA + digitB + carry; result.append(sum % 2); carry = sum / 2; i--; j--; } if (carry != 0) { result.append(carry); } return result.reverse().toString(); } } 참고 1 2 int digitA = (i >= 0) ? a.charAt(i) - '0' : 0; int digitB = (j >= 0) ? b.charAt(j) - '0' : 0; a.charAt(i)와 b.charAt(j)는 char 타입의 값을 반환합니다. 이는 문자열의 특정 위치에 있는 문자입니다. ‘0’는 또한 char 타입입니다. 하지만, 자바에서 char 타입은 실제로는 정수 값(유니코드 값)을 가지며, ‘0’의 유니코드 값은 48입니다. 따라서, a.charAt(i) - ‘0’는 char 타입 값에서 char 타입 값 ‘0’을 빼는 연산을 수행합니다. 이 때, 자바는 자동으로 char 타입을 int 타입으로 변환하여 빼기 연산을 수행합니다. 예를 들어, a.charAt(i)가 ‘1’이면, 이는 유니코드 값 49를 가집니다. 따라서 49 - 48은 1이 되어, 결국 digitA는 1이 됩니다. 결과적으로, a.charAt(i) - ‘0’는 문자 ‘0’에서 ‘9’ 사이의 문자를 해당하는 정수 값 0에서 9로 변환합니다. 이는 자동 형변환을 통해 이루어집니다. 1 2 result.append(sum % 2); carry = sum / 2; sum은 int 타입이며, 2는 리터럴 정수 타입입니다. % 연산자와 / 연산자는 int 타입끼리의 연산을 수행하며, 결과도 int 타입입니다. 이 과정에서 특별한 형변환은 일어나지 않지만, 연산의 결과가 int 타입으로 유지됩니다.

2024년 6월 17일 · 김태영

[LeetCode] 66. Plus One (Easy)

문제로 내가 푼 것1(틀림_) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 class Solution { public int[] plusOne(int[] digits) { StringBuffer s = new StringBuffer(); for(int n : digits){ s.append(n); } long nums = Long.parseLong(s.toString()) + 1l; int length = String.valueOf(nums).length(); int[] result = new int[length]; for(int i = length-1; i >= 0; i--){ result[i] = (int) nums % 10; nums = nums/10; } return result; } } 내가 푼 것2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 class Solution { public int[] plusOne(int[] digits) { for(int i = digits.length-1; i >= 0 ; i--) { if(digits[i] + 1 <= 9){ digits[i] += 1; return digits; } else { digits[i] = 0; } } int[] result = new int[digits.length + 1]; result[0] = 1; return result; } }

2024년 6월 16일 · 김태영

SQLD 자격증 합격 후기

sqld 시험에 응시했다. 시간이라는 한정된 자원을 최대한 활용하기 위해 목표가 필요했다. 자격증이 내 삶을 대단케하지는 않지만 목표를 이뤄나가는 과정이 단단한 뿌리가 되리라 믿는다. ...

2024년 6월 15일 · 김태영

[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(); } }

2024년 6월 14일 · 김태영

[LeetCode] 35. Search Insert Position (Easy)

문제로 내가 푼 것 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 한 번에 통과했다

2024년 6월 12일 · 김태영

[LeetCode] 28. Find the Index of the First Occurrence in a String (Easy)

문제로 내가 푼 것 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; } }

2024년 6월 11일 · 김태영

[LeetCode] 27. Remove Element (Easy)

문제로 내가 푼 것 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; } } 풀다보니 무슨 말인지 이해가 가는 것 같다

2024년 6월 10일 · 김태영

[LeetCode] 26. Remove Duplicates from Sorted Array (Easy)

문제로 내가 푼 것 틀림 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; } } 참고 배열도 수정해야 정답처리된다

2024년 6월 9일 · 김태영

[LeetCode] 21. Merge Two Sorted Lists (Easy)

문제로 내가 푼 것 못품 답안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번 답을 보고 벽을 느꼈다. 난 범부다.

2024년 6월 3일 · 김태영

[LeetCode] 14. Longest Common Prefix (Easy)

문제로 내가 푼 것 못품 답안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"} ...

2024년 6월 2일 · 김태영