Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
wxnacy committed Aug 10, 2020
1 parent fc8cf88 commit b28b0aa
Show file tree
Hide file tree
Showing 35 changed files with 31,156 additions and 18 deletions.
57 changes: 57 additions & 0 deletions notebook/LeetCode/-0005-最长回文子串.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"难度:中等\n",
"\n",
"知识点:字符串、动态规划\n",
"\n",
"地址:[https://leetcode-cn.com/problems/longest-palindromic-substring/](https://leetcode-cn.com/problems/longest-palindromic-substring/)\n",
"\n",
"\n",
"给定一个字符串 s,找到 s 中最长的回文子串。你可以假设 s 的最大长度为 1000。\n",
"\n",
"示例 1:\n",
"\n",
" 输入: \"babad\"\n",
" 输出: \"bab\"\n",
" 注意: \"aba\" 也是一个有效答案。\n",
" \n",
"示例 2:\n",
"\n",
" 输入: \"cbbd\"\n",
" 输出: \"bb\"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
130 changes: 130 additions & 0 deletions notebook/LeetCode/-0008-字符串转换整数(atoi).ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"难度:中等\n",
"\n",
"知识点:数学、字符串\n",
"\n",
"地址:[https://leetcode-cn.com/problems/string-to-integer-atoi/](https://leetcode-cn.com/problems/string-to-integer-atoi/)\n",
"\n",
"\n",
"请你来实现一个 atoi 函数,使其能将字符串转换成整数。\n",
"\n",
"首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。\n",
"\n",
"当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。\n",
"\n",
"该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。\n",
"\n",
"注意:\n",
" \n",
" 假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。\n",
" 在任何情况下,若函数不能进行有效的转换时,请返回 0。\n",
"\n",
"说明:\n",
"\n",
" 假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 [−2^31, 2^31 − 1]。如果数值超过这个范围,qing返回 INT_MAX (231 − 1) 或 INT_MIN (−231) 。\n",
"\n",
"示例 1:\n",
"\n",
" 输入: \"42\"\n",
" 输出: 42\n",
"\n",
"示例 2:\n",
"\n",
" 输入: \" -42\"\n",
" 输出: -42\n",
" 解释: 第一个非空白字符为 '-', 它是一个负号。\n",
" 我们尽可能将负号与后面所有连续出现的数字组合起来,最后得到 -42 。\n",
"\n",
"示例 3:\n",
"\n",
" 输入: \"4193 with words\"\n",
" 输出: 4193\n",
" 解释: 转换截止于数字 '3' ,因为它的下一个字符不为数字。\n",
"\n",
"示例 4:\n",
"\n",
" 输入: \"words and 987\"\n",
" 输出: 0\n",
" 解释: 第一个非空字符是 'w', 但它不是数字或正、负号。\n",
" 因此无法执行有效的转换。\n",
"\n",
"示例 5:\n",
"\n",
" 输入: \"-91283472332\"\n",
" 输出: -2147483648\n",
" 解释: 数字 \"-91283472332\" 超过 32 位有符号整数范围。\n",
" 因此返回 INT_MIN (−2^31) 。\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "AssertionError",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-90c33c7ccd9f>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 34\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmyAtoi\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\" -42\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m42\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 35\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmyAtoi\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"4193 with words\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m4193\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 36\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmyAtoi\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"words and 987\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m42\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 37\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0ms\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmyAtoi\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"-91283472332\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0mMIN\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAssertionError\u001b[0m: "
]
}
],
"source": [
"from config import MAX\n",
"from config import MIN\n",
"\n",
"NUMS = {\"1\", \"2\", '3', '4', '5', '6', '7', '8', '9', '0'}\n",
"INTS = {\"1\", \"2\", '3', '4', '5', '6', '7', '8', '9'}\n",
"class Solution:\n",
" def myAtoi(self, s: str) -> int:\n",
" \n",
" singal = 1\n",
" ans = 0\n",
" for a in s:\n",
" if a == ' ':\n",
" continue\n",
" if ans == 0 and a not in INTS:\n",
" \n",
" \n",
" return ans\n",
"\n",
"s = Solution()\n",
"assert s.myAtoi(\"42\") == 42\n",
"assert s.myAtoi(\" -42\") == -42\n",
"assert s.myAtoi(\"4193 with words\") == 4193\n",
"assert s.myAtoi(\"words and 987\") == 42\n",
"assert s.myAtoi(\"-91283472332\") == MIN"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
81 changes: 81 additions & 0 deletions notebook/LeetCode/-0014-最长公共前缀.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"难度:简单\n",
"\n",
"知识点:字符串\n",
"\n",
"地址:[https://leetcode-cn.com/problems/longest-common-prefix/](https://leetcode-cn.com/problems/longest-common-prefix/)\n",
"\n",
"\n",
"编写一个函数来查找字符串数组中的最长公共前缀。\n",
"\n",
"如果不存在公共前缀,返回空字符串 \"\"\n",
"\n",
"示例 1:\n",
"\n",
" 输入: [\"flower\",\"flow\",\"flight\"]\n",
" 输出: \"fl\"\n",
"示例 2:\n",
"\n",
" 输入: [\"dog\",\"racecar\",\"car\"]\n",
" 输出: \"\"\n",
" 解释: 输入不存在公共前缀。\n",
"说明:\n",
"\n",
" 所有输入只包含小写字母 a-z 。\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Solution:\n",
" def longestCommonPrefix(self, strs) -> str:\n",
" strs.sort(key = lambda x: len(x))\n",
" res = strs[0]\n",
" i = len(res)\n",
" while i >= 0:\n",
" for s in strs[1:]:\n",
" if not s.startswith(res[:i]):\n",
" i -= 1\n",
" break\n",
" if i < 0:\n",
" return ''\n",
" return res[:i]\n",
" \n",
" \n",
"\n",
"s = Solution()\n",
"assert s.longestCommonPrefix([\"flower\",\"flow\",\"flight\"]) == 'fl'\n",
"assert s.longestCommonPrefix([\"dog\",\"racecar\",\"car\"]) == ''"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
121 changes: 121 additions & 0 deletions notebook/LeetCode/0001-两数之和.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"难度:简单\n",
"\n",
"地址:[https://leetcode-cn.com/problems/two-sum/](https://leetcode-cn.com/problems/two-sum/)\n",
"\n",
"给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。\n",
"\n",
"你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。\n",
"\n",
"示例:\n",
"\n",
" 给定 nums = [2, 7, 11, 15], target = 9\n",
" 因为 nums[0] + nums[1] = 2 + 7 = 9\n",
" 所以返回 [0, 1]\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Solution:\n",
" def twoSum(self, nums, target):\n",
" '''\n",
" 两遍哈希表 时间复杂度 O(n)\n",
" 执行用时 : 56 ms, 在Two Sum的Python3提交中击败了89.57% 的用户\n",
" 内存消耗 : 14.7 MB, 在Two Sum的Python3提交中击败了7.13% 的用户\n",
" '''\n",
" length = len(nums)\n",
" m = {}\n",
" for i in range(length):\n",
" m[nums[i]] = i\n",
" for i in range(length):\n",
" x = nums[i]\n",
" y = target - x\n",
" if y in m:\n",
" return [i, m[y]]\n",
" \n",
" \n",
"\n",
"s = Solution()\n",
"s.twoSum([2, 7, 11, 15], 9)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class Solution:\n",
" def twoSum(self, nums, target):\n",
" '''\n",
" 一遍哈希表 时间复杂度 O(n)\n",
" 执行用时 : 44 ms, 在Two Sum的Python3提交中击败了99.33% 的用户\n",
" 内存消耗 : 14 MB, 在Two Sum的Python3提交中击败了56.25% 的用户\n",
" '''\n",
" m = {}\n",
" for i in range(len(nums)):\n",
" x = nums[i]\n",
" y = target - x\n",
" if y in m:\n",
" return [m[y], i]\n",
" m[x] = i\n",
" \n",
"\n",
"s = Solution()\n",
"s.twoSum([2, 7, 11, 15], 9)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Loading

0 comments on commit b28b0aa

Please sign in to comment.