본문 바로가기

Computer Science/Leetcode

(6)
Leetcode #153 Find Minimum in Rotated Sorted Array This Problem we are trying to find mininum value from Rotated Sorted Array. For example, if there is an array [0, 1, 2, 3, 4, 5, 6, 7] that is already sorted it gets rotated by random number n. Case A If the array is [4, 5, 6, 7, 0, 1, 2, 3] it rotated 4 times. What we need are mid point, most left point, and most right point. 1. In this case mid point become 7 and we knows that the mid point is..
Leetcode #704 Binary Search Binary Search is a one of the efficient search algorithm that uses a lot for various reasons. It is super simple to demonstrate and how it works. First lets say, we have size of 8 sorted array [0, 1, 3, 7, 9, 14, 16, 19] and the target number is 16 and we are trying to find the index of the target. 1. We set most left and most right and then we set the mid point which is 9 in this case. 2. If th..
Leetcode #125 Valid Palindrome A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. class Solution(object): def isPalindrome(self, s): """ :type s: str :rtype: bool """ // Change input string to lower case s = s.lower() target = "" for el in s: // trim..
Leetcode #243 Valid Anagram An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. For example, rat and tar, a gentleman and elegant man are anagram. I used brute force way to solve this problem class Solution(object): def isAnagram(self, s, t): """ :type s: str :type t: str :rtype: bool """ if len(s) != len(t): return False // ..
Leetcode #217. Contains Duplicate Python set 의 특성을 잘 알고 있으면 상당히 쉽게 풀 수 있다. It is easy if you understand set in python. But I was not at the moment so I used Brute force way to solve this problem Brute force class Solution(object): def containsDuplicate(self, nums): """ :type nums: List[int] :rtype: bool """ count = 0 nums.sort() for i in range(len(nums)-1): if nums[i] == nums[i+1]: return True return False As you can see time co..
Leetcode #20. Valid Parentheses Leetcode 20. Given a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. Example 1: Input: s = "()" Output: true Example 2: Inp..