1. 8. String to Integer (atoi)

    class Solution(object):
        def myAtoi(self, str):
            """
            :type str: str
            :rtype: int
            """
            if len(str) == 0:
                return 0
            lst_str = list(str.strip())
            sign = 1
            digit = 1
            r_lst = []
            for i in lst_str:
                if i is '+':
                    sign *= 1
                if i is '-':
                    sign *= -1
                if i >= '0' and i <= '9':
                    r_lst.append(i ...
    read more
  2. 7. Reverse Intege

    class Solution(object):
        def reverse(self, x):
            """
            :type x: int
            :rtype: int
            """
            if abs(x)!=x:
                a=-1
            else:
                a=1
    
            x=abs(x)
            total=0
            while x>0:
                n=x%10
                total=total*10+n
                x=x//10
    
            return total*a
    if __name__ == "__main__":
        answer = Solution()
        print answer.reverse ...
    read more
  3. 6. ZigZag Conversion

    class Solution(object):
        def convert(self, s, numRows):
            """
            :type s: str
            :type numRows: int
            :rtype: str
            """
            if len(s)!=1 and numRows!=1:
                list=[[] for i in range(numRows)]
                for i in range(0,len(s)):
                    j=i%(2*numRows-2)
                    div=i//(2*numRows-2)
                    if j in range ...
    read more
  4. 5. Longest Palindromic Substring

    class Solution(object):
        def longestPalindrome(self, s):
            """
            :type s: str
            :rtype: str
            """
            long=""
            temp=""
            i=0
            gap = 2
            gap2=1
    
            if len(s)==1:
                return s
    
            while i < len(s)-gap:
                if s[i] == s[i+gap]:
                    temp=s[i:i+gap+1]
                    ii=i
                    ii-=1
                    gap+=2
    
                    while ...
    read more
  5. 2. Add Two Numbers

    # Definition for singly-linked list.
    class ListNode(object):
        def __init__(self, x):
            self.val = x
            self.next = None
    
    class Solution(object):
        def addTwoNumbers(self, l1, l2):
            """
            :type l1: ListNode
            :type l2: ListNode
            :rtype: ListNode
            """
            head = ListNode(0)
            ptr = head
            carry  = 0
            while True:
                if l1 != None:
                    carry += l1.val
                    l1 = l1 ...
    read more
  6. 1. Two Sum

    class Solution(object):
        def twoSum(self, nums, target):
            """
            :type nums: List[int]
            :type target: int
            :rtype: List[int]
            """
            for i in range(len(nums)):
                for j in range (i+1,len(nums)):
                    if target-nums[i]==nums[j]:
                        return [i,j]
    
    if __name__ == "__main__":
        answer = Solution()
        nums=[2, 7, 11 ...
    read more
  7. Download Bird

    import requests
    from bs4 import BeautifulSoup
    from urlparse import urljoin
    import re
    
    
    def generate_url_list(input_url):
        r = requests.get(input_url)
        soup = BeautifulSoup(r.content, 'html.parser')
    
        page_list = []
        for link in soup.select('a > img'):
            if "org" in link.parent.get("href"):
                page_list.append(link.parent.get("href"))
                continue
            page_list.append("http ...
    read more
  8. 0. Prime.py

    class Solution(object):
        def isPrime(self, integer):
            i=2
            while i<integer:
                if integer%i==0:
                    return False
                else:
                    i+=1
            return True
    
        def printPrime(self,n,m):
            list=[]
            i=n
            while i<=m:
                if self.isPrime(i):
                    list.append(i)
                i+=1
            return list
    
    if __name__ == "__main__":
        n = 100 ...
    read more
  9. 20. Valid Parentheses

    class Solution(object):
        def isValid(self, s):
            """
            :type s: str
            :rtype: bool
            """
    
            list = []
            l=["(","[","{"]
            n=[")","]","}"]
            issy=True
    
            if len(s) == 0:
                return False
    
            i=0
            while i<len(s) and issy:
                 if s[i] in l:
                    list.append(s[i])
                 elif len(list)!=0:
                    firsti=list[-1]
                    if l.index ...
    read more

« Page 21 / 22 »

blogroll

social