Commit ec7dbdf2 authored by whzecomjm's avatar whzecomjm
Browse files

add leetcode

parent 4954e4ba
Loading
Loading
Loading
Loading
+57 −0
Original line number Diff line number Diff line
%% Cell type:markdown id: tags:

# My LeetCode

## 1. twoSum

%% Cell type:code id: tags:

``` python
class Solution:
    def twoSum(self, nums, target):
        for i in range(len(nums)):
            if target - nums[i] in nums:
                return [i, nums.index(target-nums[i])]

# test
if __name__ == '__main__':
    s = Solution()
    print(s.twoSum([2,8,11,7,15],9))
```

%% Output

    [0, 3]

%% Cell type:markdown id: tags:

## 2. addTwoNumbers
以下我的方法是针对 List 而非 ListNode. orz

%% Cell type:code id: tags:

``` python
class Solution:
    def addTwoNumbers(self, l1, l2):
        num1 = 0
        for i in range(len(l1)):
            num1 = num1 + l1[i] * 10**(i)
        num2 = 0
        for j in range(len(l2)):
            num2 = num2 + l2[j] * 10**(j)
        nnum = num1+num2
        return [int(x) for x in str(nnum)[::-1]]

if __name__ == '__main__':
    s = Solution()
    print(s.addTwoNumbers([1,2,3],[0,8,4]))
```

%% Output

    [1, 0, 8]

%% Cell type:code id: tags:

``` python
```