본문 바로가기

LeetCode91

LeetCode 242. Valid Anagram Problem: Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: s = "anagram", t = "nagaram" Output: true Example 2: Input: s = "rat", t = "car" Output: false Note: You may assume the string contains only lowercase alphabets. -Summary- 1. Create two python dictionary to count each character in the string 's' and 't'. 2. If two dictionary is the same .. 2020. 5. 25.
LeetCode 387. First Unique Character in a String Problem: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. -Summary- 1. Create a dictionary to save each character count in 's' 2. Using for loop, if we find any index of character's count is 1, then we return the index value. (After loop is done, if answer not found.. 2020. 5. 25.
LeetCode 7. Reverse Integer Problem: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 -Summary- 1. Typecast the integer 'x' to the string 'x' to make iterable. 2. Create a 'res' variable to iterate each number in x and keep adding into it. (Used res = res*10 + x[i]) 3. if the character is '-' then we multiply .. 2020. 5. 25.
LeetCode 350. Intersection of Two Arrays II Problem: Given two arrays, write a function to compute their intersection. Example 1: Input: nums1 = [1,2,2,1], nums2 = [2,2] Output: [2,2] Example 2: Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4] Output: [4,9] -Summary- Using the dictionary 1. Create a dictionary to hold an all numbers as a 'key' in the first nums1 list (Count +1 for each key) 2. Loop the second nums2 list and only append to answ.. 2020. 5. 24.
LeetCode 136. Single Number Problem Given a non-empty array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Example 1: Input: [2,2,1] Output: 1 Example 2: Input: [4,1,2,1,2] Output: 4 -Summary- #By using bit manipulation (no extra memory) 1. for each num in the nums list, do XOR bi.. 2020. 5. 23.
LeetCode 217. Contains Duplicate Problem Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. Example 1: Input: [1,2,3,1] Output: true Example 2: Input: [1,2,3,4] Output: false Example 3: Input: [1,1,1,3,3,4,3,2,4,2] Output: true -Summary- 1. Compare the length of the orig.. 2020. 5. 23.