Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
1
1_homework_安晓东
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
安晓东
1_homework_安晓东
Commits
17f55037
Commit
17f55037
authored
Jan 05, 2019
by
安晓东
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
宝石于石头
parent
dbc9f404
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
227 additions
and
0 deletions
+227
-0
01~04.ipynb
+227
-0
No files found.
01~04.ipynb
0 → 100755
View file @
17f55037
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 宝石与石头\n",
"\n",
"给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头。 S 中每个字符代表了一种你拥有的石头的类型,你想知道你拥有的石头中有多少是宝石。\n",
"J 中的字母不重复,J 和 S中的所有字符都是字母。字母区分大小写,因此\"a\"和\"A\"是不同类型的石头。\n",
"\n",
"示例 1:\n",
"输入: J = \"aA\", S = \"aAAbbbb\"\n",
"输出: 3\n",
"\n",
"示例 2:\n",
"输入: J = \"z\", S = \"ZZ\"\n",
"输出: 0\n",
"\n",
"注意:\n",
"S 和 J 最多含有50个字母,J 中的字符不重复。"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def numJewelsInStones(J, S):\n",
" \"\"\"\n",
" :type J: str\n",
" :type S: str\n",
" :rtype: int\n",
" \"\"\"\n",
" count = 0\n",
" for i in S:\n",
" if i in J:\n",
" count+=1\n",
" return count\n",
"\n",
"numJewelsInStones(\"aA\",\"aAAbbbb\") # 预期输出 3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 山脉数组的峰顶索引\n",
"\n",
"我们把符合下列属性的数组 A 称作山脉:\n",
"A.length >= 3\n",
"存在 0 < i < A.length - 1 使得A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1]\n",
"给定一个确定为山脉的数组,返回任何满足 A[0] < A[1] < ... A[i-1] < A[i] > A[i+1] > ... > A[A.length - 1] 的 i 的值。\n",
"\n",
"示例 1:\n",
"输入:[0,1,0]\n",
"输出:1\n",
"\n",
"示例 2:\n",
"输入:[0,2,1,0]\n",
"输出:1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def peakIndexInMountainArray(A):\n",
" \"\"\"\n",
" :type A: List[int]\n",
" :rtype: int\n",
" \"\"\"\n",
" \n",
"\n",
"peakIndexInMountainArray([0,1,0]) # 预期输出 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### 唯一摩尔斯密码词\n",
"\n",
"国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: \"a\" 对应 \".-\", \"b\" 对应 \"-...\", \"c\" 对应 \"-.-.\", 等等。\n",
"\n",
"为了方便,所有26个英文字母对应摩尔斯密码表如下:\n",
"[\".-\",\"-...\",\"-.-.\",\"-..\",\".\",\"..-.\",\"--.\",\"....\",\"..\",\".---\",\"-.-\",\".-..\",\"--\",\"-.\",\"---\",\".--.\",\"--.-\",\".-.\",\"...\",\"-\",\"..-\",\"...-\",\".--\",\"-..-\",\"-.--\",\"--..\"]\n",
"\n",
"给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,\"cab\" 可以写成 \"-.-..--...\",(即 \"-.-.\" + \"-...\" + \".-\"字符串的结合)。我们将这样一个连接过程称作单词翻译。\n",
"\n",
"返回我们可以获得所有词不同单词翻译的数量。\n",
"\n",
"例如:\n",
"\n",
"输入: words = [\"gin\", \"zen\", \"gig\", \"msg\"]\n",
"输出: 2\n",
"\n",
"解释: \n",
"各单词翻译如下:\n",
"\"gin\" -> \"--...-.\"\n",
"\"zen\" -> \"--...-.\"\n",
"\"gig\" -> \"--...--.\"\n",
"\"msg\" -> \"--...--.\"\n",
"\n",
"共有 2 种不同翻译, \"--...-.\" 和 \"--...--.\".\n",
" \n",
"注意:\n",
"单词列表words 的长度不会超过 100。\n",
"每个单词 words[i]的长度范围为 [1, 12]。\n",
"每个单词 words[i]只包含小写字母。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def uniqueMorseRepresentations(words):\n",
" \"\"\"\n",
" :type words: List[str]\n",
" :rtype: int\n",
" \"\"\"\n",
" \n",
"\n",
"\n",
"uniqueMorseRepresentations([\"gin\", \"zen\", \"gig\", \"msg\"]) # 预期输出 2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Z 字形变换\n",
"\n",
"将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。\n",
"\n",
"比如输入字符串为 \"LEETCODEISHIRING\" 行数为 3 时,排列如下:\n",
"\n",
"L C I R\n",
"E T O E S I I G\n",
"E D H N\n",
"之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:\"LCIRETOESIIGEDHN\"。\n",
"\n",
"请你实现这个将字符串进行指定行数变换的函数:\n",
"\n",
"string convert(string s, int numRows);\n",
"\n",
"示例 1:\n",
"输入: s = \"LEETCODEISHIRING\", numRows = 3\n",
"输出: \"LCIRETOESIIGEDHN\"\n",
"\n",
"示例 2:\n",
"输入: s = \"LEETCODEISHIRING\", numRows = 4\n",
"输出: \"LDREOEIIECIHNTSG\"\n",
"\n",
"解释:\n",
"\n",
"L D R\n",
"E O E I I\n",
"E C I H N\n",
"T S G"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def convert(s, numRows):\n",
" \"\"\"\n",
" :type s: str\n",
" :type numRows: int\n",
" :rtype: str\n",
" \"\"\"\n",
" \n",
" \n",
"\n",
"convert('LEETCODEISHIRING', 3) # 预期输出 'LCIRETOESIIGEDHN'"
]
}
],
"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.6.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment