Leetcode Hacking Practice -- Solution in Python Part 1
分页: 1, 2, 3 ... 30, 31, 32  下一页    :| |:
移民生活北美论坛 -> IT人生

#1: 作者: webdriver时间: 2014-9-29 23:12

LeetCode (used to call i has 1337 code) is a social platform for preparing IT technical interviews. We strive to provide you with the best learning experience in preparing interviews for companies in the IT industry.

To be successful in a technical interview, we believe it is mainly repeating these three important steps:

Code. Read. Discuss.

We strive to provide you the LeetCode platform as the ultimate solution for preparing technical interviews.

1. Code -> Code solution using the Online Judge system.
2. Read -> Read high quality article featuring in-depth thought process. Also read other LeetCoders’ code.
3. Discuss -> Discuss your thoughts about the problem with other LeetCoders.

We hope that through our platform, you will grow into a LeetCoder. Not only will you be successful in all of your interviews, and most importantly, you will be a better coder in general !

--------------

All solutions are in Python!

#2: 作者: webdriver时间: 2014-9-29 23:19

S算法问题: Add Binary
问题描述:
Given two binary strings, return their sum (also a binary string).
For example,
a = "11"
b = "1"
Return "100".

#3: 作者: webdriver时间: 2014-9-29 23:20

算法问题推荐解法来了...

'1'-'0' = 1.

解法(Python)
代码:
class Solution:
    def addBinary(self, a, b):
        return bin(int(a, 2) + int(b, 2)).split('b')[1]

    def addBinary(self, a, b):
       """Sometimes built-in function cheats too much.
       """
        res, carry, len_a, len_b, i = "", 0, len(a), len(b), 0
        for i in range(max(len_a, len_b)):
            sum = carry
            if i < len_a:
                sum += int(a[-(i + 1)])
            if i < len_b:
                sum += int(b[-(i + 1)])
            carry = sum / 2
            sum = sum % 2
            res = "{0}{1}".format(sum, res)
        if carry == 1:
            res = "1" + res
        return res

    # def addBinary(self, a, b):
    #    """Using carry without sum is also fine. But the readability sucks.
    #    """
    #     res, carry, len_a, len_b, i = "", 0, len(a), len(b), 0
    #     for i in range(max(len_a, len_b)):
    #         if i < len_a:
    #             carry += int(a[-(i + 1)])
    #         if i < len_b:
    #             carry += int(b[-(i + 1)])
    #         res = "{0}{1}".format(carry % 2, res)
    #         carry = carry / 2
    #     if carry == 1:
    #         res = "1" + res
    #     return res

#4: 作者: webdriver时间: 2014-9-29 23:22

S算法问题: Add Two Numbers
问题描述:
You are given two linked lists representing two non-negative numbers.
The digits are stored in reverse order and each of their nodes contain a single digit.
Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

#5: 作者: webdriver时间: 2014-9-29 23:23

算法问题推荐解法来了...

dummy head...


解法(Python)
代码:
class Solution:
    def addTwoNumbers(self, l1, l2):
        dummy = ListNode(0)
        current, carry = dummy, 0
        while l1 != None or l2 != None:
            res = carry
            if l1 != None:
                res += l1.val
                l1 = l1.next
            if l2 != None:
                res += l2.val
                l2 = l2.next
            carry, res = res / 10, res % 10
            current.next = ListNode(res)
            current = current.next
        if carry == 1:
            current.next = ListNode(1)
        return dummy.next

#6: 作者: webdriver时间: 2014-9-29 23:25

S算法问题: Anagrams
问题描述:
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.

#7: 作者: webdriver时间: 2014-9-29 23:26

算法问题推荐解法来了...

Sort the string to see if they're anagrams.
Solution 1 is simpler than 2.

解法(Python)
代码:
class Solution:
    def anagrams(self, strs):
        anagram_map, res = {}, []
        for str in strs:
            sorted_str = ("").join(sorted(str))
            if sorted_str in anagram_map:
                anagram_map[sorted_str].append(str)
            else:
                anagram_map[sorted_str] = [str]
        for anagrams in anagram_map.values():
            if len(anagrams) > 1:
                res += anagrams
        return res

    # def anagrams(self, strs):
    #     """List comprehension may be more elegant but less readable here.
    #     """
    #     anagram_map = {}
    #     for str in strs:
    #         sorted_str = ("").join(sorted(str))
    #         if sorted_str in anagram_map:
    #             anagram_map[sorted_str].append(str)
    #         else:
    #             anagram_map[sorted_str] = [str]
    #     return [anagram for anagrams in anagram_map.values() if len(anagrams) > 1 for anagram in anagrams]

#8: 作者: webdriver时间: 2014-9-29 23:29

S算法问题: Balanced Binary Tree
问题描述:
Given a binary tree, determine if it is height-balanced.
For this problem, a height-balanced binary tree is defined as a binary tree in which
the depth of the two subtrees of every node never differ by more than 1.

#9: 作者: webdriver时间: 2014-9-29 23:30

算法问题推荐解法来了...

DFS.

解法(Python)
代码:
class Solution:
    def isBalanced(self, root):
        return self.getHeight(root) != -1
   
    def getHeight(self, root):
        if root == None:
            return 0
        left_height, right_height = self.getHeight(root.left), self.getHeight(root.right)
        if left_height < 0 or right_height < 0 or math.fabs(left_height - right_height) > 1:
            return -1
        return max(left_height, right_height) + 1

#10: 作者: webdriver时间: 2014-9-29 23:33

S算法问题: Best Time to Buy and Sell Stock II
问题描述:
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like
(ie, buy one and sell one share of the stock multiple times).
However, you may not engage in multiple transactions at the same time
(ie, you must sell the stock before you buy again).




移民生活北美论坛 -> IT人生


output generated using printer-friendly topic mod, 所有的时间均为 美国太平洋时间

分页: 1, 2, 3 ... 30, 31, 32  下一页    :| |:
1页,共32

Powered by phpBB 2.0.8
Content received from: 加西网 (温哥华门户), https://www.westca.com