From 124dedeaa0eeb23ee1b0e2cb8d6cdc7946d7bae8 Mon Sep 17 00:00:00 2001 From: RIKKA <793160615@qq.com> Date: Mon, 10 Dec 2018 21:02:53 +0800 Subject: [PATCH 01/41] =?UTF-8?q?=E5=85=AC=E7=B2=AE=203=20=E5=8F=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wangggong/readme.md | 3 +- ...54\345\215\240\346\227\266\351\227\264.md" | 92 +++++++++++++++++++ 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 "wangggong/\345\207\275\346\225\260\347\232\204\347\213\254\345\215\240\346\227\266\351\227\264.md" diff --git a/wangggong/readme.md b/wangggong/readme.md index e0f9a2e..d119280 100644 --- a/wangggong/readme.md +++ b/wangggong/readme.md @@ -1,4 +1,5 @@ # 王小炒贡献内容 1. 根据二叉树创建字符串 (20181204) - +2. 较大分组的位置 (20181206) +3. 函数的独占时间 (20181210) diff --git "a/wangggong/\345\207\275\346\225\260\347\232\204\347\213\254\345\215\240\346\227\266\351\227\264.md" "b/wangggong/\345\207\275\346\225\260\347\232\204\347\213\254\345\215\240\346\227\266\351\227\264.md" new file mode 100644 index 0000000..d7af02f --- /dev/null +++ "b/wangggong/\345\207\275\346\225\260\347\232\204\347\213\254\345\215\240\346\227\266\351\227\264.md" @@ -0,0 +1,92 @@ +公粮. + +题目: + +给出一个非抢占单线程 CPU 的 *n* 个函数运行日志, 找到函数的独占时间. + +每个函数都有一个唯一的 Id, 从 0 到 n-1, 函数可能会递归调用或者被其他函数调用. + +日志是具有以下格式的字符串: *function_id: start_or_end: timestamp*. 例如: "0:start:0" 表示函数 0 从 0 时刻开始运行. "0:end:0" 表示函数 0 在 0 时刻结束. + +函数的独占时间定义是在该方法中花费的时间, 调用其他函数花费的时间不算该函数的独占时间. 你需要根据函数的 Id 有序地返回每个函数的独占时间. + +示例: + +> ** 输入: ** +> n = 2 +> logs = +> ["0:start:0", +> "1:start:2", +> "1:end:5", +> "0:end:6"] +> ** 输出: [3, 4]** +> ** 说明: ** +> 函数 0 在时刻 0 开始, 在执行了 2个时间单位结束于时刻 1. +> 现在函数 0 调用函数 1, 函数 1 在时刻 2 开始, 执行 4 个时间单位后结束于时刻 5. +> 函数 0 再次在时刻 6 开始执行, 并在时刻 6 结束运行, 从而执行了 1 个时间单位. +> 所以函数 0 总共的执行了 2 + 1 = 3 个时间单位, 函数 1 总共执行了 4 个时间单位. + +挺重要的一点是 "**函数允许被递归调用**", 这个告诉我们可以考虑 *栈* 的结构. 划重点! + +问题是要往栈里面放啥呢? 首先你要放 *Id*. 然后还要放开始时间. + +那什么时候入栈什么时候出栈呢? 就先装作 **"start"** 的时候入栈, **"end"** 的时候出栈吧. + +然后模拟一下: + + i | logs[i] | Stack | Result +-----|-------------|------------------|---------- + -1 | / | [] | [] + 0 | "0:start:0" | [(0, 0)] | [0, 0] + 1 | "1:start:2" | [(0, 0), (1, 2)] | [0, 0] + 2 | "1:end:5" | [(0, 0)] | [0, (5-2)] + 3 | "0:end:6" | [] | [(6-0), 3] + +等等不对啊... 仔细看一下有两点需要注意: + +1. 出栈的时候更新时间, **结束时间 - 开始时间** 后是要 **+1s** 的 (简单的数学) +2. 出栈的时候如果 **栈顶有东西**, 这代表 **当前结束的函数 (函数 1) 是栈顶的函数 (函数 0) 调用的**, 这时注意时间不要重复计算 (就像上面的 (6-0)). + +第一点好说, 第二点怎么办呢? + +``` +如果 栈里有东西(栈非空) { + 如果 入栈 (该命令为 ** start **) { + 栈顶函数对应时间 += 当前时间戳 - 栈内时间戳 + } + 如果 出栈 (该命令为 ** end **) { + 栈内时间戳 = 当前时间戳 + 1 + } +} +``` + +综上, 能写出这么个东西: + +``` +vector exclusiveTime(int n, vector& logs) { + vector res(n); + stack> S; + for (string log : logs) { + size_t p = log.find(":"); + size_t q = log.find(":", p+1); + int processId = stoi(log.substr(0, p)); + int timestamp = stoi(log.substr(q+1)); + if (log[p+1] == 's') { + if (!S.empty()) { + auto top = S.top(); + res[top.first] += timestamp-top.second; + } + S.push(make_pair(processId, timestamp)); + } else { + auto top = S.top(); S.pop(); + res[top.first] += (timestamp+1) -top.second; + if (!S.empty()) { + auto top = S.top(); S.pop(); + S.push(make_pair(top.first, timestamp+1)); + } + } + + } + return res; +} +``` From ddf82aa45f7224d741d225fa095e230a8d6b826c Mon Sep 17 00:00:00 2001 From: light-city <455954986@qq.com> Date: Wed, 12 Dec 2018 11:18:43 +0800 Subject: [PATCH 02/41] update --- .../levelsearch1/bfs1.py" | 28 ++++++++++++++++++ .../levelsearch1/bfs2.py" | 20 +++++++++++++ .../levelsearch1/collection.py" | 22 ++++++++++++++ .../levelsearch1/dfs.py" | 15 ++++++++++ .../levelsearch2/bfs1.py" | 29 +++++++++++++++++++ .../levelsearch2/bfs2.py" | 20 +++++++++++++ .../levelsearch2/collection.py" | 22 ++++++++++++++ .../levelsearch2/dfs.py" | 19 ++++++++++++ .../search/binary_search.py" | 17 +++++++++++ .../search/search_bn.py" | 27 +++++++++++++++++ 10 files changed, 219 insertions(+) create mode 100644 "\345\205\211\345\237\216/levelsearch1/bfs1.py" create mode 100644 "\345\205\211\345\237\216/levelsearch1/bfs2.py" create mode 100644 "\345\205\211\345\237\216/levelsearch1/collection.py" create mode 100644 "\345\205\211\345\237\216/levelsearch1/dfs.py" create mode 100644 "\345\205\211\345\237\216/levelsearch2/bfs1.py" create mode 100644 "\345\205\211\345\237\216/levelsearch2/bfs2.py" create mode 100644 "\345\205\211\345\237\216/levelsearch2/collection.py" create mode 100644 "\345\205\211\345\237\216/levelsearch2/dfs.py" create mode 100644 "\345\205\211\345\237\216/search/binary_search.py" create mode 100644 "\345\205\211\345\237\216/search/search_bn.py" diff --git "a/\345\205\211\345\237\216/levelsearch1/bfs1.py" "b/\345\205\211\345\237\216/levelsearch1/bfs1.py" new file mode 100644 index 0000000..462e47e --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch1/bfs1.py" @@ -0,0 +1,28 @@ +class Solution: + def levelOrder(self, root): + # write your code here + res = [] + + if root is None: + return res + queue = [root] + # 列表为空时,循环终止 + while queue and len(queue) != 0: + # 使用列表存储同层节点 + level_val = [] + # 同层节点的个数 + length = len(queue) + for i in range(length): + # 将同层节点依次出队 + h = queue.pop(0) + if h.left: + # 左孩子入队 + queue.append(h.left) + if h.right: + # 右孩子入队 + queue.append(h.right) + level_val.append(h.val) + res.append(level_val) + return res + + diff --git "a/\345\205\211\345\237\216/levelsearch1/bfs2.py" "b/\345\205\211\345\237\216/levelsearch1/bfs2.py" new file mode 100644 index 0000000..8c54967 --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch1/bfs2.py" @@ -0,0 +1,20 @@ +class Solution: + def levelOrder(self, root): + # write your code here + res = [] + if not root: + return [] + res_list = [] + root_list = [root] + while root_list and len(root_list)!=0: + val_list = [] + level_list = [] + for node in root_list: + val_list.append(node.val) + if node.left: + level_list.append(node.left) + if node.right: + level_list.append(node.right) + root_list = level_list + res_list.append(val_list) + return res_list \ No newline at end of file diff --git "a/\345\205\211\345\237\216/levelsearch1/collection.py" "b/\345\205\211\345\237\216/levelsearch1/collection.py" new file mode 100644 index 0000000..762a431 --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch1/collection.py" @@ -0,0 +1,22 @@ +class Solution: + def levelOrder(self, root): + # write your code here + res = [] + if not root: + return res + import collections + queue = collections.deque() + queue.append(root) + # visited = set(root) + while queue: + length = len(queue) + level_val = [] + for _ in range(length): + node = queue.popleft() + level_val.append(node.val) + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + res.append(level_val) + return res \ No newline at end of file diff --git "a/\345\205\211\345\237\216/levelsearch1/dfs.py" "b/\345\205\211\345\237\216/levelsearch1/dfs.py" new file mode 100644 index 0000000..de75175 --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch1/dfs.py" @@ -0,0 +1,15 @@ +class Solution: + def levelOrder(self, root): + if not root: + return [] + self.result = [] + self.dfs(root, 0) + return self.result + def dfs(self, root, level): + if len(self.result) < level + 1: + self.result.append([]) + self.result[level].append(root.val) + if root.left: + self.dfs(root.left, level + 1) + if root.right: + self.dfs(root.right, level + 1) \ No newline at end of file diff --git "a/\345\205\211\345\237\216/levelsearch2/bfs1.py" "b/\345\205\211\345\237\216/levelsearch2/bfs1.py" new file mode 100644 index 0000000..76ebf85 --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch2/bfs1.py" @@ -0,0 +1,29 @@ +class Solution: + def levelOrderBottom(self, root): + """ + :type root: TreeNode + :rtype: List[List[int]] + """ + res = [] + + if root is None: + return res + queue = [root] + # 列表为空时,循环终止 + while queue and len(queue) != 0: + # 使用列表存储同层节点 + level_val = [] + # 同层节点的个数 + length = len(queue) + for i in range(length): + # 将同层节点依次出队 + h = queue.pop(0) + if h.left: + # 左孩子入队 + queue.append(h.left) + if h.right: + # 右孩子入队 + queue.append(h.right) + level_val.append(h.val) + res.insert(0,level_val) + return res \ No newline at end of file diff --git "a/\345\205\211\345\237\216/levelsearch2/bfs2.py" "b/\345\205\211\345\237\216/levelsearch2/bfs2.py" new file mode 100644 index 0000000..c0beaf1 --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch2/bfs2.py" @@ -0,0 +1,20 @@ +class Solution: + def levelOrderBottom(self, root): + # write your code here + res = [] + if not root: + return [] + res_list = [] + root_list = [root] + while root_list and len(root_list)!=0: + val_list = [] + level_list = [] + for node in root_list: + val_list.append(node.val) + if node.left: + level_list.append(node.left) + if node.right: + level_list.append(node.right) + root_list = level_list + res_list.insert(0,val_list) + return res_list \ No newline at end of file diff --git "a/\345\205\211\345\237\216/levelsearch2/collection.py" "b/\345\205\211\345\237\216/levelsearch2/collection.py" new file mode 100644 index 0000000..c4afcd7 --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch2/collection.py" @@ -0,0 +1,22 @@ +class Solution: + def levelOrderBottom(self, root): + # write your code here + res = [] + if not root: + return res + import collections + queue = collections.deque() + queue.append(root) + # visited = set(root) + while queue: + length = len(queue) + level_val = [] + for _ in range(length): + node = queue.popleft() + level_val.append(node.val) + if node.left: + queue.append(node.left) + if node.right: + queue.append(node.right) + res.insert(0,level_val) + return res \ No newline at end of file diff --git "a/\345\205\211\345\237\216/levelsearch2/dfs.py" "b/\345\205\211\345\237\216/levelsearch2/dfs.py" new file mode 100644 index 0000000..b5d3bbd --- /dev/null +++ "b/\345\205\211\345\237\216/levelsearch2/dfs.py" @@ -0,0 +1,19 @@ +class Solution: + def levelOrderBottom(self, root): + if not root: + return [] + self.result = [] + self.dfs(root, 0) + return self.result + def dfs(self, root, level): + if len(self.result) < level + 1: + self.result.append([]) + self.result[level].append(root.val) + if root.left: + self.dfs(root.left, level + 1) + if root.right: + self.dfs(root.right, level + 1) + +t = [[3],[9,20],[15,7]] +t =[2,1] +print(t[::-1]) \ No newline at end of file diff --git "a/\345\205\211\345\237\216/search/binary_search.py" "b/\345\205\211\345\237\216/search/binary_search.py" new file mode 100644 index 0000000..24fc257 --- /dev/null +++ "b/\345\205\211\345\237\216/search/binary_search.py" @@ -0,0 +1,17 @@ +class BS: + def search(self,nums,target): + return self.bsearch(nums,0,len(nums)-1,target) + def bsearch(self,nums,low,high,target): + while(low<=high): + mid = (low+high)//2 + if nums[mid] == target: + return mid + elif nums[mid] < target: + low=mid+1 + elif nums[mid] > target: + high=mid-1 + return None +nums = [9,5,7,0,1,2,3,12] +target = 0 +bs = BS() +print(bs.search(nums,target)) \ No newline at end of file diff --git "a/\345\205\211\345\237\216/search/search_bn.py" "b/\345\205\211\345\237\216/search/search_bn.py" new file mode 100644 index 0000000..d3d4719 --- /dev/null +++ "b/\345\205\211\345\237\216/search/search_bn.py" @@ -0,0 +1,27 @@ +class Solution: + def search(self, nums, target): + """ + :type nums: List[int] + :type target: int + :rtype: int + """ + import sys + sys.setrecursionlimit(100000) # 这里设置为十万 + return self.zheban(nums, target, 0, len(nums) - 1) + + def zheban(self, nums, target, low, high): + if high < low: + return -1 + mid = (low + high) // 2 + if nums[mid] == target: + return mid + if nums[mid] >= nums[low]: + if nums[mid] >= target and target >= nums[low]: + return self.zheban(nums, target, low, mid - 1) + else: + return self.zheban(nums, target, mid + 1, high) + else: + if nums[mid] <= target and target <= nums[high]: + return self.zheban(nums, target, mid + 1, high) + else: + return self.zheban(nums, target, low, mid - 1) \ No newline at end of file From cee489f4c76a3164cf788e90e389a482fa48416f Mon Sep 17 00:00:00 2001 From: infisa Date: Wed, 12 Dec 2018 11:44:13 +0800 Subject: [PATCH 03/41] test-pr --- .../exist_duplicate/exist_duplicate.py | 42 +++++++++++++++++++ woyaoyangguang/exist_duplicate/reademe.md | 1 + 2 files changed, 43 insertions(+) create mode 100644 woyaoyangguang/exist_duplicate/exist_duplicate.py create mode 100644 woyaoyangguang/exist_duplicate/reademe.md diff --git a/woyaoyangguang/exist_duplicate/exist_duplicate.py b/woyaoyangguang/exist_duplicate/exist_duplicate.py new file mode 100644 index 0000000..e577ae8 --- /dev/null +++ b/woyaoyangguang/exist_duplicate/exist_duplicate.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +''' +给定一个整数数组,判断是否存在重复元素. +如果任何值在数组中出现至少两次,函数返回true.如果数组中每个元素不同,则返回false. + +示例 1: +输入: [1,2,3,1] +输出: true +示例 2: +输入: [1,2,3,4] +输出: false +''' + +''' +思路: +设定两个集合,通过循环数组判断元素是否在集合中,如果不在seen,将集合添加到seen集合中, +如果在将元素,放入到另一集合duplicated中,这个集合中的元素都是数组中重复的元素,然后 +对其做一个判断,如果不为空,返回True,如果为空,返回False. +''' +class Solution: + def containsDuplicate(self, nums): + """ + :type nums: List[int] + :rtype: bool + """ + seen=set() + duplicated=set() + for x in nums: + if x not in seen: + seen.add(x) + else: + duplicated.add(x) + if duplicated: + return True + else: + return False + + + +a=Solution() +print(a.containsDuplicate([1,2,3,4])) diff --git a/woyaoyangguang/exist_duplicate/reademe.md b/woyaoyangguang/exist_duplicate/reademe.md new file mode 100644 index 0000000..37ca395 --- /dev/null +++ b/woyaoyangguang/exist_duplicate/reademe.md @@ -0,0 +1 @@ +# 存在重复 \ No newline at end of file From 44b42536c4620043b15a1f5e980d18cf4816555e Mon Sep 17 00:00:00 2001 From: ParkFeng <1351315441@qq.com> Date: Wed, 12 Dec 2018 11:56:53 +0800 Subject: [PATCH 04/41] third commit --- ParkFeng/min_stack/Learn1.java | 43 ++++++++++++++++++++++++++++++++++ ParkFeng/min_stack/readme.md | 19 +++++++++++++++ ParkFeng/readme.md | 4 +++- 3 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 ParkFeng/min_stack/Learn1.java create mode 100644 ParkFeng/min_stack/readme.md diff --git a/ParkFeng/min_stack/Learn1.java b/ParkFeng/min_stack/Learn1.java new file mode 100644 index 0000000..6b6a17f --- /dev/null +++ b/ParkFeng/min_stack/Learn1.java @@ -0,0 +1,43 @@ +class MinStack { + + private List stack = new ArrayList<>(); + private List mins = new ArrayList<>(); + + /** initialize your data structure here. */ + public MinStack() { + stack = new ArrayList<>(); + mins = new ArrayList<>(); + } + + public void push(int x) { + stack.add(x); + if (mins.size() == 0){ + mins.add(x); + } else { + mins.add(Math.min(x, mins.get(mins.size()-1))); + } + } + + public void pop() { + int p = stack.get(stack.size()-1); + stack.remove(stack.size()-1); + mins.remove(mins.size()-1); + } + + public int top() { + return stack.get(stack.size()-1); + } + + public int getMin() { + return mins.get(mins.size()-1); + } +} + +/** + * Your MinStack object will be instantiated and called as such: + * MinStack obj = new MinStack(); + * obj.push(x); + * obj.pop(); + * int param_3 = obj.top(); + * int param_4 = obj.getMin(); + */ \ No newline at end of file diff --git a/ParkFeng/min_stack/readme.md b/ParkFeng/min_stack/readme.md new file mode 100644 index 0000000..4bcd278 --- /dev/null +++ b/ParkFeng/min_stack/readme.md @@ -0,0 +1,19 @@ +题目: + +设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 + +push(x) -- 将元素 x 推入栈中。 +pop() -- 删除栈顶的元素。 +top() -- 获取栈顶元素。 +getMin() -- 检索栈中的最小元素。 +示例: + +MinStack minStack = new MinStack(); +minStack.push(-2); +minStack.push(0); +minStack.push(-3); +minStack.getMin(); --> 返回 -3. +minStack.pop(); +minStack.top(); --> 返回 0. +minStack.getMin(); --> 返回 -2. + diff --git a/ParkFeng/readme.md b/ParkFeng/readme.md index e1d1353..a3fcc1b 100644 --- a/ParkFeng/readme.md +++ b/ParkFeng/readme.md @@ -4,4 +4,6 @@ 1.对角线遍历分析 -2.按权重随机选择 \ No newline at end of file +2.按权重随机选择 + +3.最小栈 \ No newline at end of file From 256086c42793c36cb2ceb252a1fdfc02d011cb1c Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 12 Dec 2018 19:48:24 +0800 Subject: [PATCH 05/41] test --- .../code.cpp" | 49 +++++++++++++++++++ .../readme.md" | 3 ++ 2 files changed, 52 insertions(+) create mode 100644 "\351\233\252\345\237\237\345\260\217\347\213\274/148. \346\216\222\345\272\217\351\223\276\350\241\250/code.cpp" create mode 100644 "\351\233\252\345\237\237\345\260\217\347\213\274/readme.md" diff --git "a/\351\233\252\345\237\237\345\260\217\347\213\274/148. \346\216\222\345\272\217\351\223\276\350\241\250/code.cpp" "b/\351\233\252\345\237\237\345\260\217\347\213\274/148. \346\216\222\345\272\217\351\223\276\350\241\250/code.cpp" new file mode 100644 index 0000000..0f7f167 --- /dev/null +++ "b/\351\233\252\345\237\237\345\260\217\347\213\274/148. \346\216\222\345\272\217\351\223\276\350\241\250/code.cpp" @@ -0,0 +1,49 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode(int x) : val(x), next(NULL) {} + * }; + */ +class Solution { +public: + ListNode* sortList(ListNode* head) { + if(head == NULL) + return NULL; + ListNode *right = head; + while(right->next) + right = right->next; + QuickSort(head, right); + return head; + } +private: + void QuickSort(ListNode *left, ListNode *right) + { + if(left != right) + { + ListNode *partion = GetPartion(left, right); + QuickSort(left, partion); + if(partion->next) + QuickSort(partion->next, right); + } + } + ListNode *GetPartion(ListNode *left, ListNode *right) + { + int key = left->val; + ListNode *p = left; + ListNode *q = p->next; + + while(q != right->next) + { + if(q->val < key) + { + p = p->next; + swap(p->val, q->val); + } + q = q->next; + } + swap(p->val, left->val); + return p; + } +}; \ No newline at end of file diff --git "a/\351\233\252\345\237\237\345\260\217\347\213\274/readme.md" "b/\351\233\252\345\237\237\345\260\217\347\213\274/readme.md" new file mode 100644 index 0000000..52b677a --- /dev/null +++ "b/\351\233\252\345\237\237\345\260\217\347\213\274/readme.md" @@ -0,0 +1,3 @@ +# 雪域小狼贡献内容 + +- #1. 两数求和 From 44dc956c204597547ae413878533120ab7191a07 Mon Sep 17 00:00:00 2001 From: SugarChl <31557469+SugarChl@users.noreply.github.com> Date: Wed, 12 Dec 2018 20:20:35 +0800 Subject: [PATCH 06/41] Add files via upload --- ...\225\350\257\215\346\213\206\345\210\206.cpp" | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 "SugarChl/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" diff --git "a/SugarChl/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" "b/SugarChl/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" new file mode 100644 index 0000000..6aca263 --- /dev/null +++ "b/SugarChl/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + bool wordBreak(string s, vector& wordDict) { + vector state(s.length()+1, false); + state[0] = true; + for (int i = 1; i <= s.length(); i++) { + //for (int j = 0; j <=i ; j++) + for (int j = i-1; j >=0 ; j--) { + if (state[j] && find(wordDict.begin(), wordDict.end(), s.substr(j, i-j)) != wordDict.end()) { + state[i] = true; + } + } + } + return state[s.length()]; + } +}; \ No newline at end of file From 4c2940f5fd69e4bc0850173051d7b97113cdd44c Mon Sep 17 00:00:00 2001 From: SugarChl <31557469+SugarChl@users.noreply.github.com> Date: Wed, 12 Dec 2018 20:21:12 +0800 Subject: [PATCH 07/41] Add files via upload --- ...5\350\257\215\346\213\206\345\210\206.cpp" | 16 ++++ .../readme.md" | 77 ++++++++++++------- 2 files changed, 64 insertions(+), 29 deletions(-) create mode 100644 "SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" new file mode 100644 index 0000000..5f8aae3 --- /dev/null +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/139.\345\215\225\350\257\215\346\213\206\345\210\206/139.\345\215\225\350\257\215\346\213\206\345\210\206.cpp" @@ -0,0 +1,16 @@ +class Solution { +public: + bool wordBreak(string s, vector& wordDict) { + vector state(s.length()+1, false); + state[0] = true; + for (int i = 1; i <= s.length(); i++) { + //for (int j = 0; j <=i ; j++) 原代码 + for (int j = i-1; j >=0 ; j--) { + if (state[j] && find(wordDict.begin(), wordDict.end(), s.substr(j, i-j)) != wordDict.end()) { + state[i] = true; + } + } + } + return state[s.length()]; + } +}; \ No newline at end of file diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" index a454c19..ae45898 100644 --- "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" @@ -1,29 +1,48 @@ -# 动态规划 个人归纳 - -做了一些题目之后,就无法忍受暴力的方法来解题了。 -针对可以使用动态规划的题目,做一些总结,请各位大佬指正~ - - -当一个大规模问题可以被分解为小规模子问题,那么一般来说,这个问题就可以使用动态规划来求解。 -要使用动态规划,那么就要构建状态转移方程。 -“状态转移方程,是动态规划中本阶段的状态往往是上一阶段状态和上一阶段决策的结果”(来自百度百科) -动态规划,是要将问题分解。 -那么,问题与子问题之间该怎么连接呢。 -这就要构建状态转移方程。 - - -## 221.最大正方形的问题。 ->选取矩阵中的一个点。x[i][j] ->x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 ->①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; ->②若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]均不为0,则DP[i][j]为三者中的最小值+1,因为三者中的最小值一定为三者所共有的不含0的部分,否则会造成缺角; - ->因此对于某一点(i,j), 若matrix[i][j]=1,则动态规划表达式为DP[i][j] = min{DP[i-1][j],DP[i][j-1],DP[i-1][j-1]} + 1; - -## 5.最长回文子串 ->对于字符串str,假设dp[i,j]=1表示str[i...j]是回文子串 ->那么dp[i+1,j-1] 也必然是回文子串 ->这样最长回文子串就被分解成了一系列规模小的子问题。这样就能求解了。 - ->状态转移方程: dp[i,j]={dp[i+1,j-1],str[i]==str[j];0,str[i]!=str[j]} - +# 动态规划 个人归纳 + +做了一些题目之后,就无法忍受暴力的方法来解题了。 +针对可以使用动态规划的题目,做一些总结,请各位大佬指正~ + + +当一个大规模问题可以被分解为小规模子问题,那么一般来说,这个问题就可以使用动态规划来求解。 +要使用动态规划,那么就要构建状态转移方程。 +“状态转移方程,是动态规划中本阶段的状态往往是上一阶段状态和上一阶段决策的结果”(来自百度百科) +动态规划,是要将问题分解。 +那么,问题与子问题之间该怎么连接呢。 +这就要构建状态转移方程。 + + +<<<<<<< HEAD +221.最大正方形的问题。 + 选取矩阵中的一个点。x[i][j] + x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 +   ①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; +======= +## 221.最大正方形的问题。 +>选取矩阵中的一个点。x[i][j] +>x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 +>①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; +>②若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]均不为0,则DP[i][j]为三者中的最小值+1,因为三者中的最小值一定为三者所共有的不含0的部分,否则会造成缺角; +>>>>>>> chl-pr/master + +>因此对于某一点(i,j), 若matrix[i][j]=1,则动态规划表达式为DP[i][j] = min{DP[i-1][j],DP[i][j-1],DP[i-1][j-1]} + 1; + +## 5.最长回文子串 +>对于字符串str,假设dp[i,j]=1表示str[i...j]是回文子串 +>那么dp[i+1,j-1] 也必然是回文子串 +>这样最长回文子串就被分解成了一系列规模小的子问题。这样就能求解了。 + +>状态转移方程: dp[i,j]={dp[i+1,j-1],str[i]==str[j];0,str[i]!=str[j]} + +<<<<<<< HEAD + 状态转移方程: dp[i,j]={dp[i+1,j-1] , str[i]==str[j] + 0 , str[i]!=str[j]} + + +139.单词拆分 +对于这个问题来说,一整个字符串是否能被拆分成单词,只要看两个子字符串能否被拆分。 +这段代码我是在网络上参考来的。运行时间需要20ms +经过深入的理解之后。 +我将代码改了一部分细节。(见备注)运行时间缩短到了4ms +======= +>>>>>>> chl-pr/master From 1b30753a0287ad8705d387f62ccac51ba54b9740 Mon Sep 17 00:00:00 2001 From: SugarChl <31557469+SugarChl@users.noreply.github.com> Date: Wed, 12 Dec 2018 20:22:21 +0800 Subject: [PATCH 08/41] Update readme.md --- .../readme.md" | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" index ae45898..0cc9110 100644 --- "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" @@ -12,7 +12,6 @@ 这就要构建状态转移方程。 -<<<<<<< HEAD 221.最大正方形的问题。 选取矩阵中的一个点。x[i][j] x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 @@ -23,7 +22,7 @@ >x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 >①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; >②若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]均不为0,则DP[i][j]为三者中的最小值+1,因为三者中的最小值一定为三者所共有的不含0的部分,否则会造成缺角; ->>>>>>> chl-pr/master + >因此对于某一点(i,j), 若matrix[i][j]=1,则动态规划表达式为DP[i][j] = min{DP[i-1][j],DP[i][j-1],DP[i-1][j-1]} + 1; @@ -33,10 +32,6 @@ >这样最长回文子串就被分解成了一系列规模小的子问题。这样就能求解了。 >状态转移方程: dp[i,j]={dp[i+1,j-1],str[i]==str[j];0,str[i]!=str[j]} - -<<<<<<< HEAD - 状态转移方程: dp[i,j]={dp[i+1,j-1] , str[i]==str[j] - 0 , str[i]!=str[j]} 139.单词拆分 @@ -45,4 +40,3 @@ 经过深入的理解之后。 我将代码改了一部分细节。(见备注)运行时间缩短到了4ms ======= ->>>>>>> chl-pr/master From 781827a5a49f13d38503e20d9b23383224010124 Mon Sep 17 00:00:00 2001 From: SugarChl <31557469+SugarChl@users.noreply.github.com> Date: Wed, 12 Dec 2018 20:23:19 +0800 Subject: [PATCH 09/41] Update readme.md --- .../readme.md" | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" index 0cc9110..b25cbec 100644 --- "a/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" +++ "b/SugarChl/\345\212\250\346\200\201\350\247\204\345\210\222/readme.md" @@ -12,11 +12,6 @@ 这就要构建状态转移方程。 -221.最大正方形的问题。 - 选取矩阵中的一个点。x[i][j] - x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 -   ①若DP[i-1][j],DP[i][j-1],DP[i-1][j-1]三者中存在0值,则DP[i][j]一定为0; -======= ## 221.最大正方形的问题。 >选取矩阵中的一个点。x[i][j] >x点是否是最大正方形的右下角的点 与x[i-1][j] x[i][j-1] x[i-1][j-1]这三个点有关 @@ -34,9 +29,8 @@ >状态转移方程: dp[i,j]={dp[i+1,j-1],str[i]==str[j];0,str[i]!=str[j]} -139.单词拆分 -对于这个问题来说,一整个字符串是否能被拆分成单词,只要看两个子字符串能否被拆分。 -这段代码我是在网络上参考来的。运行时间需要20ms -经过深入的理解之后。 -我将代码改了一部分细节。(见备注)运行时间缩短到了4ms -======= +## 139.单词拆分 +>对于这个问题来说,一整个字符串是否能被拆分成单词,只要看两个子字符串能否被拆分。 +>这段代码我是在网络上参考来的。运行时间需要20ms +>经过深入的理解之后。 +>我将代码改了一部分细节。(见备注)运行时间缩短到了4ms From 3b2deabe68e73e382cdf7e22497cc220753b4205 Mon Sep 17 00:00:00 2001 From: Liling <841935390@qq.com> Date: Wed, 12 Dec 2018 21:23:25 +0800 Subject: [PATCH 10/41] 20181212 --- ...00\351\225\277\345\255\220\344\270\262.py" | 45 ++++++++++++ .../readme.md | 69 +++++++++++++++++++ codingling/readme.md | 6 +- 3 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 "codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/03.LongestSubstringWithoutRepeatingCharacters\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" create mode 100644 codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/readme.md diff --git "a/codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/03.LongestSubstringWithoutRepeatingCharacters\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" "b/codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/03.LongestSubstringWithoutRepeatingCharacters\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" new file mode 100644 index 0000000..acb2e1b --- /dev/null +++ "b/codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/03.LongestSubstringWithoutRepeatingCharacters\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" @@ -0,0 +1,45 @@ +# -*- coding: utf-8 -*- +# @Author: LiLing +# @Date: 2018-10-02 19:20:50 +# @Last Modified by: Liling +# @Last Modified time: 2018-12-12 20:39:03 +""" +给定一个字符串,找出不含有重复字符的最长子串的长度。 + +示例 1: + +输入: "abcabcbb" +输出: 3 +解释: 无重复字符的最长子串是 "abc",其长度为 3。 +示例 2: + +输入: "bbbbb" +输出: 1 +解释: 无重复字符的最长子串是 "b",其长度为 1。 +示例 3: + +输入: "pwwkew" +输出: 3 +解释: 无重复字符的最长子串是 "wke",其长度为 3。 + 请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串 +""" +class Solution(object): + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + if s == None or len(s) <= 0: + return 0 + # charDict存储每个字符以及字符出现的最后的位置, res为当前最长的子串长度, st当前无重复子串的最左边字符的位置 + charDict, res, st = {}, 0, 0 + for i, ch in enumerate(s): + if ch not in charDict or charDict[ch] < st: + res = max(res, i - st + 1) + else: + st = charDict[ch] + 1 + charDict[ch] = i + return res + +s = Solution() +print(s.lengthOfLongestSubstring('pwwkew')) \ No newline at end of file diff --git a/codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/readme.md b/codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/readme.md new file mode 100644 index 0000000..bd41a1a --- /dev/null +++ b/codingling/LeetCode-03-LongestSubstringWithoutRepeatingCharacters/readme.md @@ -0,0 +1,69 @@ +## 03.Longest Substring Without Repeating Characters无重复字符的最长子串 + +### 题目描述 + +给定一个字符串,找出不含有重复字符的最长子串的长度。 + +示例 1: + +输入: "abcabcbb" +输出: 3 +解释: 无重复字符的最长子串是 "abc",其长度为 3。 +示例 2: + +输入: "bbbbb" +输出: 1 +解释: 无重复字符的最长子串是 "b",其长度为 1。 +示例 3: + +输入: "pwwkew" +输出: 3 +解释: 无重复字符的最长子串是 "wke",其长度为 3。 +​ 请注意,答案必须是一个子串,"pwke" 是一个子序列 而不是子串 + +### 思路 + +第一种方法是暴力解法,检查所有的子串是不是有重复的字符,需要写一个检查字符串是否有重复字符的函数。这种方法的时间复杂度是$O(n^3)$,空间复杂度是O(min(n,m))。n是字符串长度,m是charset/alphabet长度。 + + + +第二种方法。 + +在第一种方法中,我们需要重复检查一个子串是不是有重复的字符,但这个步骤是不必要的。如果一个子串$s_{ij}$ 的i到j-1是不重复的子串,我们只需要检查s[j]是不是已经在前面的子串中就可以了。 + +怎么检查呢?可以用哈希表作为滑动窗口来检查。用哈希表存储当前窗口[i,j)(初始时i=j)的字符,然后将索引j依次向右滑动,检查新的字符是否在哈希表中。如果不在,继续滑动。直到s[j]已经在哈希表中为止。这样就能找到以索引i开始的最长的无重复子串了。对于每个i重复上述操作就能得到结果。 + +这种方法的时间复杂度是O(2n)=O(n),最坏情况下每个字符会被访问两次(i和j),空间复杂度不变,O(min(m,n))。 + + + +上述方法还可以改进。 + +如果s[j]在[i,j)有一个重复的字符,其索引是j',我们不需要一点一点地增大i,而是可以直接跳过[i, j']这个范围,直接让i=j'+1。 + +我们可以不用哈希表来判断字符是否存在,我们可以定义一个字符到索引的映射字典。这样的话当我们遇到同样的字符时可以马上跳过这个字符。 + +代码如下。当前窗口是[st, i)。如果当前字符不在字典中,或它的上一个索引不在当前扫描窗口内,说明还没有遇到重复的字符,更新当前最长的子串长度res;否则(当前字符是重复字符),更新扫描窗口(注意在上一个字符的时候已经更新了res,所以这里不用再更新)。记住不管什么情况都要更新字符的最新索引。 + +```python +class Solution(object): + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + if s == None or len(s) <= 0: + return 0 + # charDict存储每个字符以及字符出现的最后的位置, res为当前最长的子串长度, st当前无重复子串的最左边字符的位置 + charDict, res, st = {}, 0, 0 + for i, ch in enumerate(s): + if ch not in charDict or charDict[ch] < st: + res = max(res, i - st + 1) + else: + st = charDict[ch] + 1 + charDict[ch] = i + return res +``` + + + diff --git a/codingling/readme.md b/codingling/readme.md index a76c870..014f4d8 100644 --- a/codingling/readme.md +++ b/codingling/readme.md @@ -1,5 +1,5 @@ # codingling贡献内容 -- AddTwoNumbers两数相加(链表),2018/12/04 - -- TwoSum两数之和,2018/12/07 \ No newline at end of file +- 02.AddTwoNumbers两数相加(链表),2018/12/04 +- 01.TwoSum两数之和,2018/12/07 +- 03.LongestSubstringWithoutRepeatingCharacters无重复字符的最长子串,2018/12/12 \ No newline at end of file From c2e85ba128d58202108adb7fa5963b0a6f3147db Mon Sep 17 00:00:00 2001 From: simkakashi Date: Wed, 12 Dec 2018 21:51:44 +0800 Subject: [PATCH 11/41] delete myself --- ...00\351\225\277\345\255\220\344\270\262.py" | 18 ------------ HughGuo/Leetcode-02/leetcode_02.py | 29 ------------------- HughGuo/Leetcode-02/readme.md | 17 ----------- HughGuo/readme.md | 4 --- 4 files changed, 68 deletions(-) delete mode 100644 "HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" delete mode 100644 HughGuo/Leetcode-02/leetcode_02.py delete mode 100644 HughGuo/Leetcode-02/readme.md delete mode 100644 HughGuo/readme.md diff --git "a/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" "b/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" deleted file mode 100644 index c90a041..0000000 --- "a/HughGuo/Leetcode-02/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" +++ /dev/null @@ -1,18 +0,0 @@ -class Solution: - def lengthOfLongestSubstring(self, s): - """ - :type s: str - :rtype: int - """ - temp = 0 - d = {} - start = 0 - for i in range(len(s)): - if s[i] in d and start <= d[s[i]] : - start = d[s[i]] +1 - temp = max(i-start+1,temp) - d[s[i]] = i - return temp - -## 维护两个字符串,一个是当前最大的字符串l,一个是当前字符串t,最初的时候这两个字符串都为空。每扫一个字符,如果这个字符不在字符串当中,就把当前字符串加上这个字符。如果在,当前字符串就不能再往前加字符了,然后比较当前字符串和当前最大字符串。再把当前字符串的开始地址替换掉因为不能有重复元素,##麻烦 -## 滑动窗口! 机智! diff --git a/HughGuo/Leetcode-02/leetcode_02.py b/HughGuo/Leetcode-02/leetcode_02.py deleted file mode 100644 index d0058f6..0000000 --- a/HughGuo/Leetcode-02/leetcode_02.py +++ /dev/null @@ -1,29 +0,0 @@ -def addTwoNumbers(self, l1, l2): - """ - :type l1: ListNode - :type l2: ListNode - :rtype: ListNode - """ - if l1==None and l2==None: - return None - if l1==None: - return l2 - if l2==None: - return l1 - q=ListNode(-1) - p=q - flag=0 - p1=l1 - p2=l2 - while p1 or p2 or flag: - sum=flag - if p1: - sum=sum+p1.val - p1=p1.next - if p2: - sum=sum+p2.val - p2=p2.next - p.next=ListNode(sum%10) - p=p.next - flag=sum/10 - return q.next diff --git a/HughGuo/Leetcode-02/readme.md b/HughGuo/Leetcode-02/readme.md deleted file mode 100644 index f909732..0000000 --- a/HughGuo/Leetcode-02/readme.md +++ /dev/null @@ -1,17 +0,0 @@ -# 两数相加 - -给出两个 **非空** 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 **逆序** 的方式存储的,并且它们的每个节点只能存储 **一位** 数字。 - -如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 - -您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 - - - -## 思路 - -1. 遍历,两元素相加,为和创建新链表元素,长度取短,考虑进位。 (弃用) - -2. sum=0,遍历时取sum%10为值,sum/10 为进位1,同时判断是否遍历结束 - - ...Python真是慢... \ No newline at end of file diff --git a/HughGuo/readme.md b/HughGuo/readme.md deleted file mode 100644 index 5d3aedf..0000000 --- a/HughGuo/readme.md +++ /dev/null @@ -1,4 +0,0 @@ -# HughGuo贡献内容 - -- #2. 两数求和 -- #3. 无重复字符的最长子串 From 6b1e32a96293c3c2531ff0ad49fd20d7788352c5 Mon Sep 17 00:00:00 2001 From: simkakashi Date: Wed, 12 Dec 2018 21:57:04 +0800 Subject: [PATCH 12/41] add myself --- ...44\346\225\260\347\233\270\345\212\240.py" | 29 ++++++++++++++ ...00\351\225\277\345\255\220\344\270\262.py" | 18 +++++++++ ...36\346\226\207\345\255\220\344\270\262.py" | 39 +++++++++++++++++++ HughGuo/readme.md | 1 + 4 files changed, 87 insertions(+) create mode 100644 "HughGuo/C002_\344\270\244\346\225\260\347\233\270\345\212\240.py" create mode 100644 "HughGuo/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" create mode 100644 "HughGuo/C005_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" create mode 100644 HughGuo/readme.md diff --git "a/HughGuo/C002_\344\270\244\346\225\260\347\233\270\345\212\240.py" "b/HughGuo/C002_\344\270\244\346\225\260\347\233\270\345\212\240.py" new file mode 100644 index 0000000..d0058f6 --- /dev/null +++ "b/HughGuo/C002_\344\270\244\346\225\260\347\233\270\345\212\240.py" @@ -0,0 +1,29 @@ +def addTwoNumbers(self, l1, l2): + """ + :type l1: ListNode + :type l2: ListNode + :rtype: ListNode + """ + if l1==None and l2==None: + return None + if l1==None: + return l2 + if l2==None: + return l1 + q=ListNode(-1) + p=q + flag=0 + p1=l1 + p2=l2 + while p1 or p2 or flag: + sum=flag + if p1: + sum=sum+p1.val + p1=p1.next + if p2: + sum=sum+p2.val + p2=p2.next + p.next=ListNode(sum%10) + p=p.next + flag=sum/10 + return q.next diff --git "a/HughGuo/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" "b/HughGuo/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" new file mode 100644 index 0000000..c90a041 --- /dev/null +++ "b/HughGuo/C003_\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262.py" @@ -0,0 +1,18 @@ +class Solution: + def lengthOfLongestSubstring(self, s): + """ + :type s: str + :rtype: int + """ + temp = 0 + d = {} + start = 0 + for i in range(len(s)): + if s[i] in d and start <= d[s[i]] : + start = d[s[i]] +1 + temp = max(i-start+1,temp) + d[s[i]] = i + return temp + +## 维护两个字符串,一个是当前最大的字符串l,一个是当前字符串t,最初的时候这两个字符串都为空。每扫一个字符,如果这个字符不在字符串当中,就把当前字符串加上这个字符。如果在,当前字符串就不能再往前加字符了,然后比较当前字符串和当前最大字符串。再把当前字符串的开始地址替换掉因为不能有重复元素,##麻烦 +## 滑动窗口! 机智! diff --git "a/HughGuo/C005_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" "b/HughGuo/C005_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" new file mode 100644 index 0000000..ee95260 --- /dev/null +++ "b/HughGuo/C005_\346\234\200\351\225\277\345\233\236\346\226\207\345\255\220\344\270\262.py" @@ -0,0 +1,39 @@ +class Solution: + def longestPalindrome(self, s): + """ + :type s: str + :rtype: str + """ + n = len(s) + maxL, maxR, max = 0, 0, 0 + for i in range(n): + # 长度为偶数的回文字符串 + start = i + end = i + 1 + while start >= 0 and end < n: + if s[start] == s[end]: + if end - start + 1 > max: + max = end - start + 1 + maxL = start + maxR = end + start -= 1 + end += 1 + else: + break + + # 长度为奇数的回文子串 + start = i - 1 + end = i + 1 + while start >= 0 and end < n: + if s[start] == s[end]: + if end - start + 1 > max: + max = end - start + 1 + maxL = start + maxR = end + start -= 1 + end += 1 + else: + break + return s[maxL:maxR+1] + +###遍历指针为i, j=i+1, i左移,j右移。判断是否相等将长度,下标赋给临时变量,最后切片返回。边界条件也需要处理好。 diff --git a/HughGuo/readme.md b/HughGuo/readme.md new file mode 100644 index 0000000..d5b6dd4 --- /dev/null +++ b/HughGuo/readme.md @@ -0,0 +1 @@ +# HughGuo贡献内容 \ No newline at end of file From 1ec49a8ab87e205876610b8b2168b89994372344 Mon Sep 17 00:00:00 2001 From: spareribs <370835062@qq.com> Date: Wed, 12 Dec 2018 22:08:26 +0800 Subject: [PATCH 13/41] =?UTF-8?q?=E5=88=A0=E9=99=A4.idea=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E5=A4=B9,=20=E4=BF=AE=E6=94=B9gitignore?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 119 ++++++ .idea/encodings.xml | 4 - .idea/learning-algorithm.iml | 11 - .idea/misc.xml | 7 - .idea/modules.xml | 8 - .idea/vcs.xml | 6 - .idea/workspace.xml | 362 ------------------ .../Readme.md | 0 Spareribs/0023 merge_k_Sorted_lists/Readme.md | 0 Spareribs/0053 maximums_subarray/Readme.md | 102 +++++ 10 files changed, 221 insertions(+), 398 deletions(-) delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/learning-algorithm.iml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/vcs.xml delete mode 100644 .idea/workspace.xml rename Spareribs/{0053 merge_two_sorted_lists => 0021 merge_two_sorted_lists}/Readme.md (100%) create mode 100644 Spareribs/0023 merge_k_Sorted_lists/Readme.md create mode 100644 Spareribs/0053 maximums_subarray/Readme.md diff --git a/.gitignore b/.gitignore index c7c7f14..c88077a 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,122 @@ *.json *.sqlite *.ipch +.idea + +# 20181212 排骨 +# ------------------- +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +.hypothesis/ +.pytest_cache/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 15a15b2..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,4 +0,0 @@ - - - - \ No newline at end of file diff --git a/.idea/learning-algorithm.iml b/.idea/learning-algorithm.iml deleted file mode 100644 index f3d7bc9..0000000 --- a/.idea/learning-algorithm.iml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 8656114..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 3627932..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/workspace.xml b/.idea/workspace.xml deleted file mode 100644 index d2a73e8..0000000 --- a/.idea/workspace.xml +++ /dev/null @@ -1,362 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - true - DEFINITION_ORDER - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -