본문 바로가기

Delete Node in a Linked List Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list -- head = [4,5,1,9], which looks like following: 4 -> 5 -> 1 -> 9 Example 1:Input: head = [4,5,1,9], node = 5 Output: [4,1,9] Explanation: You are given the second node with value 5, the linked list should become 4 -> 1 -> 9 after calling your function. Example 2:Input: h.. 더보기
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings.If there is no common prefix, return an empty string "".Example 1:Input: ["flower","flow","flight"] Output: "fl" Example 2:Input: ["dog","racecar","car"] Output: "" Explanation: There is no common prefix among the input strings. Note:All given inputs are in lowercase letters a-z. My solution:class Solution: def.. 더보기
Count and Say The count-and-say sequence is the sequence of integers with the first five terms as following:1. 1 2. 11 3. 21 4. 1211 5. 111221 1 is read off as "one 1" or 11. 11 is read off as "two 1s" or 21. 21 is read off as "one 2, then one 1" or 1211.Given an integer n where 1 ≤ n ≤ 30, generate the nth term of the count-and-say sequence.Note: Each term of the sequence of integers will be represented as a.. 더보기
Implement strStr() Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll" Output: 2 Example 2:Input: haystack = "aaaaa", needle = "bba" Output: -1 Clarification:What should we return when needle is an empty string? This is a great question to ask during an interview.For the purpose of this problem.. 더보기
String to Integer (atoi) Implement atoi which converts a string to an integer.The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.The string can contain additional characters after .. 더보기
Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Note: For the purpose of this problem, we define empty string as valid palindrome.Example 1:Input: "A man, a plan, a canal: Panama" Output: true Example 2:Input: "race a car" Output: false My solution:class Solution: def isPalindrome(self, s): """ :type s: str :rtype: bool """ size_s = le.. 더보기
Valid Anagram 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.Follow up: What if the inputs contain unicode characters? How would you adapt your solution to such case? My first solution:class Solution:.. 더보기
First Unique Character in a String 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. Note: You may assume the string contain only lowercase letters. My solutions:import sys class Solution: def firstUniqChar(self, s): """ :type s: str :rtype: int """ unique_map = dict() sort_list = list() size = len(s) i.. 더보기
Reverse Integer 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 Note: Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231, 231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overfl.. 더보기
Reverse String Write a function that takes a string as input and returns the string reversed.Example 1:Input: "hello" Output: "olleh" Example 2:Input: "A man, a plan, a canal: Panama" Output: "amanaP :lanac a ,nalp a ,nam A" My solutions:class Solution: def reverseString(self, s): """ :type s: str :rtype: str """ size = len(s) result = "" for i in range(size): result += s[size - i - 1] return result 더보기