From d19a7ebdb408268f8f537d925b6f8f262917b9e7 Mon Sep 17 00:00:00 2001 From: Liling <841935390@qq.com> Date: Fri, 7 Dec 2018 17:15:40 +0800 Subject: [PATCH] 20181207 --- ...44\346\225\260\344\271\213\345\222\214.py" | 41 +++++++ codingling/LeetCode-01-twosum/readme.md | 54 +++++++++ ...\345\212\240(\351\223\276\350\241\250).py" | 0 .../LeetCode-02-addtwonumbers/README.md | 112 ++++++++++++++++++ codingling/addtwonumbers/README.md | 15 --- codingling/readme.md | 4 +- 6 files changed, 210 insertions(+), 16 deletions(-) create mode 100644 "codingling/LeetCode-01-twosum/01.TwoSum\344\270\244\346\225\260\344\271\213\345\222\214.py" create mode 100644 codingling/LeetCode-01-twosum/readme.md rename "codingling/addtwonumbers/2.AddTwoNumbers\344\270\244\346\225\260\347\233\270\345\212\240(\351\223\276\350\241\250).py" => "codingling/LeetCode-02-addtwonumbers/02.AddTwoNumbers\344\270\244\346\225\260\347\233\270\345\212\240(\351\223\276\350\241\250).py" (100%) create mode 100644 codingling/LeetCode-02-addtwonumbers/README.md delete mode 100644 codingling/addtwonumbers/README.md diff --git "a/codingling/LeetCode-01-twosum/01.TwoSum\344\270\244\346\225\260\344\271\213\345\222\214.py" "b/codingling/LeetCode-01-twosum/01.TwoSum\344\270\244\346\225\260\344\271\213\345\222\214.py" new file mode 100644 index 0000000..65f7e1a --- /dev/null +++ "b/codingling/LeetCode-01-twosum/01.TwoSum\344\270\244\346\225\260\344\271\213\345\222\214.py" @@ -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)) \ No newline at end of file diff --git a/codingling/LeetCode-01-twosum/readme.md b/codingling/LeetCode-01-twosum/readme.md new file mode 100644 index 0000000..c4c5321 --- /dev/null +++ b/codingling/LeetCode-01-twosum/readme.md @@ -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)。 + diff --git "a/codingling/addtwonumbers/2.AddTwoNumbers\344\270\244\346\225\260\347\233\270\345\212\240(\351\223\276\350\241\250).py" "b/codingling/LeetCode-02-addtwonumbers/02.AddTwoNumbers\344\270\244\346\225\260\347\233\270\345\212\240(\351\223\276\350\241\250).py" similarity index 100% rename from "codingling/addtwonumbers/2.AddTwoNumbers\344\270\244\346\225\260\347\233\270\345\212\240(\351\223\276\350\241\250).py" rename to "codingling/LeetCode-02-addtwonumbers/02.AddTwoNumbers\344\270\244\346\225\260\347\233\270\345\212\240(\351\223\276\350\241\250).py" diff --git a/codingling/LeetCode-02-addtwonumbers/README.md b/codingling/LeetCode-02-addtwonumbers/README.md new file mode 100644 index 0000000..693a6b9 --- /dev/null +++ b/codingling/LeetCode-02-addtwonumbers/README.md @@ -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分别代表两个链表的长度。 \ No newline at end of file diff --git a/codingling/addtwonumbers/README.md b/codingling/addtwonumbers/README.md deleted file mode 100644 index ab6a3ca..0000000 --- a/codingling/addtwonumbers/README.md +++ /dev/null @@ -1,15 +0,0 @@ -## 2.AddTwoNumbers两数相加(链表) - -### 题目描述 - -给定两个非空链表来表示两个非负整数。位数按照逆序方式存储,它们的每个节点只存储单个数字。将两数相加返回一个新的链表。 - -你可以假设除了数字 0 之外,这两个数字都不会以零开头。 - -示例: - -输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) -输出:7 -> 0 -> 8 -原因:342 + 465 = 807 - -这里给出了三种解法 \ No newline at end of file diff --git a/codingling/readme.md b/codingling/readme.md index cac534e..a76c870 100644 --- a/codingling/readme.md +++ b/codingling/readme.md @@ -1,3 +1,5 @@ # codingling贡献内容 -1.AddTwoNumbers两数相加(链表) \ No newline at end of file +- AddTwoNumbers两数相加(链表),2018/12/04 + +- TwoSum两数之和,2018/12/07 \ No newline at end of file