본문 바로가기

Google4

LeetCode. Sort Colors Problems: Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively. Note: You are not suppose to use the library's sort function for this problem. Example: Input: [2,0,2,1,1,0] Out.. 2020. 6. 12.
LeetCode. Reverse Linked List [Linked List] Problem: Reverse a singly linked list. Example: Input: 1->2->3->4->5->NULL Output: 5->4->3->2->1->NULL -Summary- 1. Keep track of current / prev (copy the original list in the curr and prev) 2. Update and move the head (original) 3. Return prev -Other iterative solution- # Definition for singly-linked list. # class ListNode: # def __init__(self, val=0, next=None): # self.val = val # self.next = .. 2020. 6. 3.
[Arrays] firstDuplicate - Google Problem: Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more than 1 duplicated numbers, return the number for which the second occurrence has a smaller index than the second occurrence of the other number does. If there are no such elements, return -1... 2020. 5. 29.
LeetCode 01. Two Sum Problem: Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the same element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1]. Summary: 1. nums list를 돌며 target-num을 하였을때 다른 number가 dict 안에 있는지 확인. 2.. 2020. 5. 21.