Saturday, November 4, 2017

35. Search Insert Position

class Solution:
    def searchInsert(self, nums, target):
        """
        :type nums: List[int]
        :type target: int
        :rtype: int
        """
        if nums[-1] < target:
            return len(nums)
        elif nums[0] >= target:
            return 0
        else:
            start_index = 0
            end_index = len(nums)-1          
            while start_index + 1 <= end_index:
                middle_index = (start_index + end_index +1)//2
                if start_index + 1 == end_index:
                    return end_index
                else:
                    if nums[middle_index] == target:
                        return middle_index
                    elif nums[middle_index] < target:
                        start_index = middle_index
                    else:
                        end_index = middle_index  

217. Contains Duplicate

class Solution:
    def containsDuplicate(self, nums):
        """
        :type nums: List[int]
        :rtype: bool
        """
        result = False
        a_dict = {}
        for i in range(len(nums)):
            if nums[i] in a_dict:
                result = True
                break
            else:
                a_dict[nums[i]] = 1
        return result

389. Find the Difference

class Solution:
    def findTheDifference(self, s, t):
        """
        :type s: str
        :type t: str
        :rtype: str
        """
        def construct_dict(a_string):
            a_dict = {}
            for i in range(len(a_string)):
                if a_string[i] in a_dict:
                    a_dict[a_string[i]] += 1
                else:
                    a_dict[a_string[i]] = 1
            return a_dict
        dict_1 = construct_dict(s)
        dict_2 = construct_dict(t)
        for a_key in dict_2.keys():
            try:
                dict_1[a_key]
            except KeyError:
                return a_key
            else:
                if dict_1 [a_key] != dict_2 [a_key]:
                    return a_key  

409. Longest Palindrome

class Solution:
    def longestPalindrome(self, s):
        """
        :type s: str
        :rtype: int
        """
        a_dict = {}
        for i in range (len(s)):
            if s[i] in a_dict:
                a_dict[s[i]] += 1
            else:
                a_dict[s[i]] = 1
        count = 0
        unit = 0
        for a_value in a_dict.values():
            if a_value % 2 == 0:
                count += a_value
            else:
                count += (a_value//2)*2
                unit = 1
        return count + unit  

Friday, November 3, 2017

557. Reverse Words in a String III

class Solution:
    def reverseWords(self, s):
        """
        :type s: str
        :rtype: str
        """
        new_list = []
        new_list = s.split()
        new_string=""
        for i in range(0,len(new_list)):
            new_list[i] = new_list[i][::-1]
            new_string = " ".join(new_list)
        return new_string

Thursday, November 2, 2017

344. Reverse String

class Solution:
    def reverseString(self, s):
        """
        :type s: str
        :rtype: str
        """
        return s[::-1]

9. Palindrome Number

class Solution:
    def isPalindrome(self, x):
        """
        :type x: int
        :rtype: bool
        """
        if x < 0:
            return False
        if x == int(str(x)[::-1]):
            return True
        else:
            return False