본문 바로가기

Python130

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.
CodeSignal [37/60] arrayMaxConsecutiveSum Problem: Given array of integers, find the maximal possible sum of some of its k consecutive elements. Example For inputArray = [2, 3, 5, 1, 6] and k = 2, the output should be arrayMaxConsecutiveSum(inputArray, k) = 8. All possible sums of 2 consecutive elements are: 2 + 3 = 5; 3 + 5 = 8; 5 + 1 = 6; 1 + 6 = 7. Thus, the answer is 8. -Summary- 1. Set the max_sum as first k consecutive sum from th.. 2020. 5. 25.
CodeSignal [36/60] differentSymbolsNaive Problem: Given a string, find the number of different characters in it. Example For s = "cabca", the output should be differentSymbolsNaive(s) = 3. There are 3 different characters a, b and c. -Summary- 1. Create a temporary set variable to not have a duplicate value 2. Using for loop, if the character is already in the set, we skip and continue. If the first character faced, we add in to set an.. 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.
CodeSignal [35/60] firstDigit Problem: Find the leftmost digit that occurs in a given string. Example For inputString = "var_1__Int", the output should be firstDigit(inputString) = '1'; For inputString = "q2q-q", the output should be firstDigit(inputString) = '2'; For inputString = "0ss", the output should be firstDigit(inputString) = '0'. -Summary- 1. isdigit() 함수를 이용하여 char 하나하나 비교후, if we found a 'True' value, then we ret.. 2020. 5. 25.
CodeSignal [34/60] extractEachKth Problem: Given array of integers, remove each kth element from it. Example For inputArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] and k = 3, the output should be extractEachKth(inputArray, k) = [1, 2, 4, 5, 7, 8, 10]. -Summary- 1. Create a 'ans' list for answer list 2. While loop을 돌리면서 index 가 지워지는 번호에 해당하지 않는경우 ans list에 넣어준다. 3. loop을 다 돌리면 지워지는 Kth index element만 지워진 'ans' list값을 return. Additional.. 2020. 5. 24.