Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions codingling/LeetCode-01-twosum/01.TwoSum两数之和.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
# @Author: LiLing
# @Date: 2018-10-02 19:07:51
# @Last Modified by: Liling
# @Last Modified time: 2018-10-02 19:16:24
"""
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]
"""
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums)):
res = nums[i+1:]
resnum = target-nums[i]
if resnum in res:
j = res.index(resnum)
return [i,i+j+1]

class Solution():
def twoSum(self, nums, target):
hashdict = {}
for i, item in enumerate(nums):
if (target - item) in hashdict:
return (hashdict[target - item], i)
hashdict[item] = i
return (-1, -1)
s = Solution()
print(s.twoSum([2, 7, 11, 15], 9))
54 changes: 54 additions & 0 deletions codingling/LeetCode-01-twosum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
## 01.TwoSum两数之和

### 题目描述

给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。

你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。

示例:

给定 nums = [2, 7, 11, 15], target = 9

因为 nums[0] + nums[1] = 2 + 7 = 9
所以返回 [0, 1]

### 思路

第一种思路是比较直观也是复杂度比较高的解法,遍历列表中的每个元素,在剩下的元素中找有没有使得两个数之和为目标值的数。

```python
class Solution:
def twoSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[int]
"""
for i in range(len(nums)):
res = nums[i+1:]
resnum = target-nums[i]
if resnum in res:
j = res.index(resnum)
return [i,i+j+1]
```

这种解法的时间复杂度是$O(n^2)$,因为需要遍历,遍历的同时还要在剩下的元素中查找。空间复杂度是O(1)。



第二种思路是用哈希,也是要遍历列表,但是遍历的同时将当前值及其index存入哈希字典,并且在哈希字典中查找有没有想要的值。

```python
class Solution():
def twoSum(self, nums, target):
hashdict = {}
for i, item in enumerate(nums):
if (target - item) in hashdict:
return (hashdict[target - item], i)
hashdict[item] = i
return (-1, -1)
```

这种解法的时间复杂度是O(n),空间复杂度是O(n)。

112 changes: 112 additions & 0 deletions codingling/LeetCode-02-addtwonumbers/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
## 2.AddTwoNumbers两数相加(链表)

### 题目描述

给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。

你可以假设除了数字 0 之外,这两个数字都不会以零开头。

示例:

输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807



单链表的定义是这样的:

```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
```

###思路

这里给出了三种解法

解法一:只需要按逆序的顺序将各位数加起来即可,有进位的地方标志一下。

```python
class Solution(object):
def addTwoNumbers(self, l1, l2):
pNode = ListNode(0)
pHead = pNode
val = 0
while l1 or l2 or val:
if l1:
val += l1.val
l1 = l1.next
if l2:
val += l2.val
l2 = l2.next
pNode.next = ListNode(val % 10)
val /= 10
pNode = pNode.next
return pHead.next
```

思路一样,但换了种写法:

```python
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""
node1 = l1
node2 = l2
dummyRoot = ListNode(0)
ptr = dummyRoot
carry = 0
while node1 or node2:
ptr.next = ListNode((node1.val if node1 else 0) + (node2.val if node2 else 0) + carry)
ptr = ptr.next
carry = ptr.val //10
ptr.val %= 10
node1 = node1.next if node1 else None
node2 = node2.next if node2 else None
if carry:
ptr.next = ListNode(carry)
return dummyRoot.next
```

解法二:先把两个数字用int型表示出来,相加之和再存成链表形式:

```python
class Solution:
def addTwoNumbers(self, l1, l2):
"""
:type l1: ListNode
:type l2: ListNode
:rtype: ListNode
"""

sl1 = ''
sl2 = ''

while l1:
sl1 += str(l1.val)
l1 = l1.next

while l2:
sl2 += str(l2.val)
l2 = l2.next

sum = str(int(sl1[::-1]) + int(sl2[::-1]))

head = tail = ListNode(0)

for cha in sum[::-1]:
cur = ListNode(cha)
tail.next = cur
tail = cur

return head.next
```

上述解法的时间复杂度都是O(max(m,n)),空间复杂度也是O(max(m,n)),其中m和n分别代表两个链表的长度。
15 changes: 0 additions & 15 deletions codingling/addtwonumbers/README.md

This file was deleted.

4 changes: 3 additions & 1 deletion codingling/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# codingling贡献内容

1.AddTwoNumbers两数相加(链表)
- AddTwoNumbers两数相加(链表),2018/12/04

- TwoSum两数之和,2018/12/07