feat(init project): add all existing files

add all existing files

Signed-off-by: Ching <loooching@gmail.com>
This commit is contained in:
Ching 2022-02-02 19:04:18 +08:00
commit 9690121403
191 changed files with 27798 additions and 0 deletions

132
.gitignore vendored Normal file
View File

@ -0,0 +1,132 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
*.lcov
# nyc test coverage
.nyc_output
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Snowpack dependency directory (https://snowpack.dev/)
web_modules/
# TypeScript cache
*.tsbuildinfo
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional stylelint cache
.stylelintcache
# Microbundle cache
.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# parcel-bundler cache (https://parceljs.org/)
.cache
.parcel-cache
# Next.js build output
.next
out
# Nuxt.js build / generate output
.nuxt
dist
# Gatsby files
.cache/
# Comment in the public line in if your project uses Gatsby and not Next.js
# https://nextjs.org/blog/next-9-1#public-directory-support
# public
# vuepress build output
.vuepress/dist
# vuepress v2.x temp and cache directory
.temp
.cache
# Docusaurus cache and generated files
.docusaurus
# Serverless directories
.serverless/
# FuseBox cache
.fusebox/
# DynamoDB Local files
.dynamodb/
# TernJS port file
.tern-port
# Stores VSCode versions used for testing VSCode extensions
.vscode-test
# yarn v2
.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.deploy_git/

7
.npmignore Normal file
View File

@ -0,0 +1,7 @@
.DS_Store
Thumbs.db
db.json
*.log
node_modules/
public/
.deploy*/

90
_config.yml Normal file
View File

@ -0,0 +1,90 @@
# Hexo Configuration
## Docs: https://hexo.io/docs/configuration.html
## Source: https://github.com/hexojs/hexo/
# Site
title: MarkDown
subtitle: 「靡不有初,鲜克有终」
description:
author: Ching
language: zh-CN
timezone: Asia/Shanghai
# URL
## If your site is put in a subdirectory, set url as 'http://yoursite.com/child' and root as '/child/'
url: http://blog.tunpok.com/
root: /
permalink: :year/:month/:day/:title/
permalink_defaults:
# Directory
source_dir: source
public_dir: public
tag_dir: tags
archive_dir: archives
category_dir: categories
code_dir: downloads/code
i18n_dir: :lang
skip_render:
# Writing
new_post_name: :year-:month-:day-:title.md # File name of new posts
default_layout: post
titlecase: false # Transform title into titlecase
external_link: true # Open external links in new tab
filename_case: 0
render_drafts: false
post_asset_folder: false
relative_link: false
future: true
highlight:
enable: true
line_number: true
auto_detect: false
tab_replace:
# Category & Tag
default_category: uncategorized
category_map:
tag_map:
# Date / Time format
## Hexo uses Moment.js to parse and display date
## You can customize the date format as defined in
## http://momentjs.com/docs/#/displaying/format/
date_format: YYYY-MM-DD
time_format: HH:mm:ss
# Pagination
## Set per_page to 0 to disable pagination
per_page: 10
pagination_dir: page
# Extensions
## Plugins: https://hexo.io/plugins/
## Themes: https://hexo.io/themes/
theme: apollo
# theme: cactus
# Deployment
## Docs: https://hexo.io/docs/deployment.html
# deploy:
# type: git
# repo: https://github.com/looching/looching.github.io
deploy:
type: rsync
host: 104.238.151.113
user: captain
root: /var/www/tunpok-blog
port: 22 # Default is 22
delete: true # Default is true
verbose: true # Default is true
ignore_errors: false # Default is false
mathjax:
tags: none # or 'ams' or 'all'
single_dollars: true # enable single dollar signs as in-line math delimiters
cjk_width: 0.9 # relative CJK char width
normal_width: 0.6 # relative normal (monospace) width

1
db.json Normal file

File diff suppressed because one or more lines are too long

BIN
dump.rdb Normal file

Binary file not shown.

11685
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

27
package.json Normal file
View File

@ -0,0 +1,27 @@
{
"name": "hexo-site",
"version": "0.0.0",
"private": true,
"hexo": {
"version": "3.5.0"
},
"dependencies": {
"canvas": "^2.4.0",
"hexo": "^3.5.0",
"hexo-browsersync": "^0.2.0",
"hexo-deployer-rsync": "^0.1.3",
"hexo-filter-mathjax": "^0.4.1",
"hexo-generator-archive": "^0.1.4",
"hexo-generator-category": "^0.1.3",
"hexo-generator-feed": "^1.1.0",
"hexo-generator-index": "^0.2.0",
"hexo-generator-sitemap": "^1.1.2",
"hexo-generator-tag": "^0.2.0",
"hexo-inject": "^1.0.0",
"hexo-renderer-ejs": "^0.1.1",
"hexo-renderer-jade": "^0.1.0",
"hexo-renderer-marked": "^0.2.9",
"hexo-renderer-stylus": "^0.3.0",
"hexo-server": "^0.1.3"
}
}

View File

@ -0,0 +1,120 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> First Post · MarkDown</title><meta name="description" content="First Post - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">First Post</h1><div class="post-meta"><div class="post-time">2016年2月12日</div></div><div class="post-content"><p>This is the very first post I wrote,</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>with <a href="https://www.typora.io/" target="_blank" rel="noopener">Typora</a> &amp; <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>.</p>
</div></article></div></section><footer><div class="paginator"><a href="/2016/02/15/Flask-Day-1/" class="prev">PRVE</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,145 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> Flask Day 1 · MarkDown</title><meta name="description" content="Flask Day 1 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">Flask Day 1</h1><div class="post-meta"><div class="post-time">2016年2月15日</div></div><div class="post-content"><h3 id="u201CHello_World_u201D_in_Flask"><a href="#u201CHello_World_u201D_in_Flask" class="headerlink" title="“Hello World” in Flask"></a>“Hello World” in Flask</h3><p>Create a folder named <code>microblog</code> (or whatever you want). Then cd into that folder and run following prompt in terminal:</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m venv flask</span><br></pre></td></tr></table></figure>
<p>Now youll have a folder named <code>flask</code> inside <code>microblog</code>, containing a private version of Python interpreter.</p>
<p>And you should install <strong>flask</strong> and extensions by the commands below:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ flask/bin/pip install flask</span><br><span class="line">$ flask/bin/pip install flask-login</span><br><span class="line">$ flask/bin/pip install flask-openid</span><br><span class="line">$ flask/bin/pip install flask-mail</span><br><span class="line">$ flask/bin/pip install flask-sqlalchemy</span><br><span class="line">$ flask/bin/pip install sqlalchemy-migrate</span><br><span class="line">$ flask/bin/pip install flask-whooshalchemy</span><br><span class="line">$ flask/bin/pip install flask-wtf</span><br><span class="line">$ flask/bin/pip install flask-babel</span><br><span class="line">$ flask/bin/pip install guess_language</span><br><span class="line">$ flask/bin/pip install flipflop</span><br><span class="line">$ flask/bin/pip install coverage</span><br></pre></td></tr></table></figure>
<p>After that, lets create the basic structure for our application: <code>app</code> <code>app/static</code> <code>app/templates</code> <code>tmp</code>.</p>
<ol>
<li><code>app</code> — where the application package is</li>
<li><code>static</code> — stores static files like images, javascripts, and css.</li>
<li><code>templates</code> — where templates will go.</li>
</ol>
<p>Then you can start with <code>__init__.py</code> which should put into app folder (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>The views are the handlers that response to requests from web browsers or other clients. Each view function is mapped to one or more request URLs.</p>
<p>Lets see what a views function looks like (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Finally we should create a script to starts up the web server with our application(file <code>run.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!flask/bin/python</span></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line">app.run(debug=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
<p>To indicating that is an executable file you need to run this in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod a+x run.py</span><br></pre></td></tr></table></figure>
<p>Now the file structure should look like:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">microblog\</span><br><span class="line"> flask\</span><br><span class="line"> &lt;virtual environment files&gt;</span><br><span class="line"> app\</span><br><span class="line"> static\</span><br><span class="line"> templates\</span><br><span class="line"> __init__.py</span><br><span class="line"> views.py</span><br><span class="line"> tmp\</span><br><span class="line"> run.py</span><br></pre></td></tr></table></figure>
<p>Then start to write the template (file <code>app/templates/index.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#123;&#123; title &#125;&#125; - microblog<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; user.nickname &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Now lets write the view function that uses this template (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/')</span></span><br><span class="line"><span class="meta">@app.route('/index')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> user = &#123;<span class="string">'nickname'</span>: <span class="string">'ching'</span>&#125; <span class="comment"># fake user</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>,</span><br><span class="line"> title=<span class="string">'Home'</span>,</span><br><span class="line"> user=user)</span><br></pre></td></tr></table></figure>
<p><code>render_template</code> function is what we import from Flask framework to render the template. It uses <a href="http://jinja.pocoo.org/" target="_blank" rel="noopener">Jinja2</a> templating engine.</p>
</div></article></div></section><footer><div class="paginator"><a href="/2016/02/16/Flask-Day-2/" class="prev">PRVE</a><a href="/2016/02/12/first-post/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,129 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> Flask Day 2 · MarkDown</title><meta name="description" content="Flask Day 2 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">Flask Day 2</h1><div class="post-meta"><div class="post-time">2016年2月16日</div></div><div class="post-content"><p>To handle web forms we use <a href="http://packages.python.org/Flask-WTF" target="_blank" rel="noopener">Flask-WTF </a>. So we need to write a config file (file <code>config.py</code>):</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">WTF_CSRF_ENABLED = <span class="keyword">True</span></span><br><span class="line">SECRET_KEY = <span class="string">'you-will-never-guess'</span></span><br></pre></td></tr></table></figure>
<p>And then you need to use this config (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line">app.config.from_object(<span class="string">'config'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Lets build a simple form (file <code>app/forms.app</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask.ext.wtf <span class="keyword">import</span> Form</span><br><span class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, BooleanField</span><br><span class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> DataRequired</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginForm</span><span class="params">(Form)</span>:</span></span><br><span class="line"> openid = StringField(<span class="string">'openid'</span>, validators=[DataRequired()])</span><br><span class="line"> remember_me = BooleanField(<span class="string">'remember_me'</span>, default=<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
<p>The <code>DataRequired()</code> is a validator that checks the field is empty or not.</p>
<p>After that, we need a HTML page to show the form (file <code>app/templates/login.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- extend from base layout --&gt;</span></span><br><span class="line">&#123;% extends "base.html" %&#125;</span><br><span class="line"></span><br><span class="line">&#123;% block content %&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Sign In<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">""</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">name</span>=<span class="string">"login"</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.hidden_tag() &#125;&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line"> Please enter your OpenID:<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.openid(size=80) &#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; form.remember_me &#125;&#125; Remember Me<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Sign In"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line">&#123;% endblock %&#125;</span><br></pre></td></tr></table></figure>
<p>The final step is to code a view function that renders the template and receiving data from form (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"><span class="keyword">from</span> .forms <span class="keyword">import</span> LoginForm</span><br><span class="line"></span><br><span class="line"><span class="comment"># index view function suppressed for brevity</span></span><br><span class="line"></span><br><span class="line">app.route(<span class="string">'/login'</span>, methods=[<span class="string">'GET'</span>, <span class="string">'POST'</span>])</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">()</span>:</span></span><br><span class="line"> form = LoginForm()</span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> flash(<span class="string">'Login requested for OpenID="%s", remember_me=%s'</span> %</span><br><span class="line"> (form.openid.data, str(form.remember_me.data)))</span><br><span class="line"> <span class="keyword">return</span> redirect(<span class="string">'/index'</span>)</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'login.html'</span>, </span><br><span class="line"> title=<span class="string">'Sign In'</span>,</span><br><span class="line"> form=form)</span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2016/04/25/Django-Manager-Method/" class="prev">PRVE</a><a href="/2016/02/15/Flask-Day-1/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,132 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> Django Manager Method · MarkDown</title><meta name="description" content="Django Manager Method - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">Django Manager Method</h1><div class="post-meta"><div class="post-time">2016年4月25日</div></div><div class="post-content"><h4 id="Django_Manager"><a href="#Django_Manager" class="headerlink" title="Django Manager"></a>Django Manager</h4><p>Django 里会为每一个 model 生成一个 Manager默认名字为 objects一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理QuerySet 方法是对数据库操作的封装。</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.db <span class="keyword">import</span> models</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> ...</span><br><span class="line"> people = models.Manager()</span><br></pre></td></tr></table></figure>
<p>上面这个 model<code>Person.objects</code>会产生一个<code>AttributeError</code>,但是<code>Person.people</code>就可以正常操作。因为默认的 manager 已经变成 peopleobjects 这个 manager 没有重新声明,不起作用。</p>
<h4 id="u81EA_u5B9A_u4E49_Manager"><a href="#u81EA_u5B9A_u4E49_Manager" class="headerlink" title="自定义 Manager"></a>自定义 Manager</h4><p>通常需要自定义 manager 的情况有两点:</p>
<ol>
<li>需要修改/扩展 Django 的 manager 方法</li>
<li>需要修改返回的 QuerySet</li>
</ol>
<h4 id="u9ED8_u8BA4_Manager"><a href="#u9ED8_u8BA4_Manager" class="headerlink" title="默认 Manager"></a>默认 Manager</h4><p>如果使用自定义的 manager 需要注意的是Django 将 model 中定义的第一个 manager 认为是默认 manager而且 Django 框架中会用到默认 manager。</p>
<p>笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager并放在第一个。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> title = models.CharField(max_length=<span class="number">100</span>)</span><br><span class="line"> author = models.CharField(max_length=<span class="number">50</span>)</span><br><span class="line"> </span><br><span class="line"> objects = models.Manager() <span class="comment"># default manager</span></span><br><span class="line"> custom_objects = CustomBOokManager() <span class="comment"># custom manager</span></span><br></pre></td></tr></table></figure>
<p><a href="http://blog.csdn.net/sicofield/article/details/49283751" target="_blank" rel="noopener">source</a></p>
</div></article></div></section><footer><div class="paginator"><a href="/2016/05/04/Tastypie/" class="prev">PRVE</a><a href="/2016/02/16/Flask-Day-2/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,131 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> Tastypie · MarkDown</title><meta name="description" content="Tastypie - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">Tastypie</h1><div class="post-meta"><div class="post-time">2016年5月4日</div></div><div class="post-content"><h4 id="Resources_in_Tastypie"><a href="#Resources_in_Tastypie" class="headerlink" title="Resources in Tastypie"></a>Resources in Tastypie</h4><p>Resources are the heart of Tastypie. By defining a resource we can actually convert a model into an API stream. The data is automatically converted into API response.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Understanding the process of creating a resource.</p>
<ol>
<li>Import ModelResource from Tastypie.</li>
<li>Import models from services app</li>
<li>Create custom resource by inheriting ModelResource and link app model in inner Meta class of resource. </li>
</ol>
<p>Add API URL in the urls.py of app.</p>
<h4 id="Dehydrating_the_JSON_data"><a href="#Dehydrating_the_JSON_data" class="headerlink" title="Dehydrating the JSON data"></a>Dehydrating the JSON data</h4><p><img src="https://impythonist.files.wordpress.com/2016/04/tastypie_ill.png?w=800" alt="flow"></p>
<p>Dehydration in Tastypie means making alterations before sending data to the client. Suppose we need to send capitalized product names instead of small letters. Now we see two kinds of dehydrate methods.</p>
<h5 id="Dehydrate_field_method"><a href="#Dehydrate_field_method" class="headerlink" title="Dehydrate_field method"></a>Dehydrate_field method</h5><p>This <code>dehydrate_field</code> is uesd to modify field on the response JSON. </p>
<h5 id="Dehydrate_method"><a href="#Dehydrate_method" class="headerlink" title="Dehydrate method"></a>Dehydrate method</h5><p>Dehydrate method is useful for aadding additional fields to bundle (response data). </p>
<p>Similarly using <code>hydrate</code> method we can alter the bundle data which is generated from request at the time of PUT or POST methods.</p>
</div></article></div></section><footer><div class="paginator"><a href="/2016/05/10/TastyPie-Note-1/" class="prev">PRVE</a><a href="/2016/04/25/Django-Manager-Method/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,155 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> TastyPie Note 1 · MarkDown</title><meta name="description" content="TastyPie Note 1 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">TastyPie Note 1</h1><div class="post-meta"><div class="post-time">2016年5月10日</div></div><div class="post-content"><h3 id="Flow_Through_The_Request/Response_Cycle"><a href="#Flow_Through_The_Request/Response_Cycle" class="headerlink" title="Flow Through The Request/Response Cycle"></a>Flow Through The Request/Response Cycle</h3><p>Tastypie can be thought of as a set of class-based view that provide the API functionality. All routing/middleware/response-handling aspectss are the same as a typical Django app. Where the differs is in the view itself.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Walking through what a GET request to a list endpoint looks like:</p>
<ul>
<li><p>The <code>Resource.urls</code> are checked by Djangos url resolvers.</p>
</li>
<li><p>On a match for the list view, <code>Resource.wrap_view(&#39;dispatch_list&#39;)</code> is called. <code>wrap_view</code> provides basic error handling &amp; allows for returning serilized errors.</p>
</li>
<li><p>Because dispatch_list was passed to <code>wrap_view</code>, <code>Resource.dispatch_list</code> is called next. This is a thin wrapper around <code>Resource.dispatch</code>.</p>
</li>
<li><p><code>dispatch</code> does a bunch of havy lifting. It ensures:</p>
<ul>
<li>the requested HTTP method is in <code>allowed_methos</code> (<code>method_check</code>).</li>
<li>the class has a method that can handle the request(<code>get_list</code>)</li>
<li>the user is authenticated(<code>is_authenticated</code>)</li>
<li>the user has no exceeded their throttle(<code>throttle_check</code>).</li>
</ul>
<p>At this point, <code>dispatch</code> actually calls the requested method (<code>get_list</code>).</p>
</li>
<li><p><code>get_list</code> does the actual work of API. It does:</p>
<ul>
<li>A fetch of the available objects via <code>Resource.obj_get_list</code>. In the case of <code>ModelResource</code>, this builds the ORM filters to apply (<code>ModelResource.build_filters</code>). It then gets the <code>QuerySet</code> via <code>ModelResource.get_object_list</code> (which performs <code>Resource.authorized_read_list</code> to possibly limit the set the user can work with) and applies the built filters to it.</li>
<li>It then sorts the objects based on user input (<code>ModelResource.apply_sorting</code>).</li>
<li>Then it paginates the results using the supplied <code>Paginator</code> &amp; pulls out the data to be serialized.</li>
<li>The objects in the page have <code>full_dehydrate</code> applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.</li>
<li>Finally, it calls <code>Resource.create_response</code>.</li>
</ul>
</li>
<li><p><code>create_response</code> is a shortcut method that:</p>
<ul>
<li>Determines the desired response format (<code>Resource.determine_format</code>).</li>
<li>Serializes the data given to it in the proper format.</li>
<li>Returns a Django <code>HttpResponse</code> (200 OK) with the serialized data.</li>
</ul>
</li>
<li><p>We bubble back up the call stack to <code>dispatch</code>. The last thing <code>dispatch</code> does is potentially store that a request occured for future throttling (<code>Resource.log_throttled_access</code>) then either returns the <code>HttpResponse</code> or wraps whatever data came back in a response (so Django doesnt freak out).</p>
</li>
</ul>
</div></article></div></section><footer><div class="paginator"><a href="/2018/05/31/bash-function-and-awk/" class="prev">PRVE</a><a href="/2016/05/04/Tastypie/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,6 @@
<!DOCTYPE html><html lang="zh-cn"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> test · MarkDown</title><meta name="description" content="test - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">BLOG</a></li><li class="nav-list-item"><a href="https://github.com/looching" target="_blank" class="nav-list-link">GITHUB</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">test</h1><div class="post-meta"><div class="post-time">2018年5月16日</div></div><div class="post-content"><h1 id="test_title"><a href="#test_title" class="headerlink" title="test title"></a>test title</h1><hr>
<p>fadafasd</p>
<p>fasdasf</p>
<hr>
<p>fasdf</p>
</div></article></div></section><footer><div class="paginator"><a href="/2016/05/10/TastyPie-Note-1/" class="next">下一篇</a></div><div class="copyright"><p>© 2016 - 2018 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script></body></html>

View File

@ -0,0 +1,130 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> bash function and awk · MarkDown</title><meta name="description" content="bash function and awk - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">bash function and awk</h1><div class="post-meta"><div class="post-time">2018年5月31日</div></div><div class="post-content"><p>Ill come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So lets see what these functions can do to help me improve my productivity.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><h4 id="bash_functions"><a href="#bash_functions" class="headerlink" title="bash functions"></a>bash functions</h4><p>Bash functions are scripts like <code>alias</code>, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as <code>&amp;&amp;</code> I guess:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> run &#123;</span><br><span class="line"> <span class="built_in">source</span> ~/Develop/django/bin/activate</span><br><span class="line"> ./manage.py runserver</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>I activate django virtural enviroment first and then run django serve.</p>
<p>Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> ba &#123; hgba | grep <span class="string">"<span class="variable">$1</span>"</span> &#125;</span><br><span class="line"><span class="comment"># hgba is an alias for hg branches</span></span><br></pre></td></tr></table></figure>
<p>I can just type <code>ba release</code> then get current release branch info.</p>
<hr>
<p>Append:</p>
<p>I made some updates to the <code>ba</code> function and make it auto copying the branch result to my clipboard:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">function</span> ba &#123; var=<span class="string">"<span class="variable">$(hgba | grep $1)</span>"</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> | awk -F <span class="string">':'</span> END&#123;<span class="built_in">print</span>&#125; | awk -F <span class="string">':'</span> <span class="string">'&#123;print $NF&#125;'</span> | tr -d <span class="string">'\n'</span> | pbcopy &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> &#125;</span><br></pre></td></tr></table></figure>
<h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><p><code>awk</code> is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python. </p>
</div></article></div></section><footer><div class="paginator"><a href="/2019/03/12/Postgresql-Partitioning/" class="prev">PRVE</a><a href="/2016/05/10/TastyPie-Note-1/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,146 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> Postgresql Partitioning · MarkDown</title><meta name="description" content="Postgresql Partitioning - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">Postgresql Partitioning</h1><div class="post-meta"><div class="post-time">2019年3月12日</div></div><div class="post-content"><p><code>Partitioning</code> refers to splitting what is logically one large table inot smaller physical pieces.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Currently, PostgreSQL supports partitioning via table <a href="https://www.postgresql.org/docs/9.6/ddl-inherit.html" target="_blank" rel="noopener">inheritance</a>. Each partition must be created as a child table of a single parent table. <strong>The parent table itself is normally empty</strong>; It exists just to represent the entire data set.</p>
<p>There are two forms of partitioning can be implemented in PostgreSQL:</p>
<ul>
<li><p>Range Partitioning</p>
<p> The table is partitioning into “range” defined by a key column or a set of columns, with no overlap between the ranges of values assigned to different partitions. eg. partition by date ranges or by identifiers.</p>
</li>
<li><p>List Partitioning</p>
<p> The table is partitioned by explicitly listing which key values appear in each partition.</p>
</li>
</ul>
<h3 id="Implementing_Partitioning"><a href="#Implementing_Partitioning" class="headerlink" title="Implementing Partitioning"></a>Implementing Partitioning</h3><ol>
<li><p>Create the “master” / “parent” table, from which all the partitions will inherit.</p>
<p> This table will not contain any data. Do not define any check on this table, unless you intend them to be applied equally to all partitions. There is no point in defining any indexes or unique constraints on it either.</p>
</li>
<li><p>Create “child” tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master. </p>
</li>
<li><p>Add table constraints to the partition tables to define the allowed key values in each partitions. </p>
<p> Ensure that the constraints guarantee that there is no overlap between the key values premitted in different partitions. And there is no difference in syntax between range and list partitioning. </p>
</li>
<li><p>Create indexes on column(s) for each partitions.</p>
</li>
<li><p>Optionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.</p>
</li>
<li><p>Ensure hte <a href="https://www.postgresql.org/docs/9.6/runtime-config-query.html#GUC-CONSTRAINT-EXCLUSION" target="_blank" rel="noopener">constraint_exclusion</a> configuration parameter is not disabled in <code>postgresql.conf</code>. If it is, queries will not be optimized as desired.</p>
</li>
</ol>
<h3 id="Trigger"><a href="#Trigger" class="headerlink" title="Trigger"></a>Trigger</h3><p>As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.</p>
</div></article></div></section><footer><div class="paginator"><a href="/2019/11/14/AWS-KMS/" class="prev">PRVE</a><a href="/2018/05/31/bash-function-and-awk/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,121 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> AWS KMS · MarkDown</title><meta name="description" content="AWS KMS - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">AWS KMS</h1><div class="post-meta"><div class="post-time">2019年11月14日</div></div><div class="post-content"><p>We used to keep private credentials on production servers without any protection or encryption. Well, luckily we dont have any leak but this practice is not recommended for both security and easy of use reasons.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Since AWS finally provides <a href="https://www.amazonaws.cn/kms/" target="_blank" rel="noopener">KMS(Key Management Service)</a> in our local region, we try to encrypt every private credentials by KMS and store them on S3.</p>
<p><em>TBD</em></p>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/17/leetcode-121/" class="prev">PRVE</a><a href="/2019/03/12/Postgresql-Partitioning/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,125 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-121 · MarkDown</title><meta name="description" content="leetcode-121 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-121</h1><div class="post-meta"><div class="post-time">2020年3月17日</div></div><div class="post-content"><h3 id="121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A"><a href="#121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h3><p><a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>原始答案:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">maxProfit</span><span class="params">(self, prices)</span> -&gt; int:</span></span><br><span class="line"> profit = <span class="number">0</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> prices:</span><br><span class="line"> <span class="keyword">return</span> profit</span><br><span class="line"> min_buyin = prices[<span class="number">0</span>]</span><br><span class="line"> max_sellout = prices[<span class="number">0</span>]</span><br><span class="line"> l = len(prices)</span><br><span class="line"> i = <span class="number">0</span></span><br><span class="line"> <span class="keyword">for</span> buyin <span class="keyword">in</span> prices:</span><br><span class="line"> <span class="keyword">if</span> i == l:</span><br><span class="line"> <span class="keyword">return</span> profit</span><br><span class="line"> <span class="keyword">if</span> min_buyin &lt;= buyin:</span><br><span class="line"> max_sellout = max(prices[i:])</span><br><span class="line"> p = max_sellout - min_buyin</span><br><span class="line"> <span class="keyword">if</span> p &gt; profit:</span><br><span class="line"> profit = p</span><br><span class="line"> <span class="keyword">if</span> buyin &lt; min_buyin:</span><br><span class="line"> min_buyin = buyin</span><br><span class="line"> i += <span class="number">1</span></span><br><span class="line"> <span class="keyword">return</span> profit</span><br><span class="line"><span class="comment">#1880 ms 14.4 MB</span></span><br></pre></td></tr></table></figure>
<p>主要思路是找到波谷如果当前价格比前一天要低则还是在去往波谷的路上当价格比前一天高或相同时则到达了一个波谷计算波谷和之后的波峰的差就是这一段的利润。将从头至尾过一次就能找到所有波谷和其后波峰的差返回最大的即可。但是这个明显地在重复max时间复杂度是O(n^2),看起来就很傻逼。</p>
<p>仔细想想其实并不需要直接找出波谷后的波峰只要在for loop时保持波谷为最低的那个就能算出每一个后续与波谷的差找最大差即可。改了下代码变成这样</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">maxProfit</span><span class="params">(self, prices)</span> -&gt; int:</span></span><br><span class="line"> profit = <span class="number">0</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> prices:</span><br><span class="line"> <span class="keyword">return</span> profit</span><br><span class="line"> min_buyin = prices[<span class="number">0</span>]</span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">1</span>, len(prices)):</span><br><span class="line"> min_buyin = min(min_buyin, prices[i<span class="number">-1</span>])</span><br><span class="line"> profit = max(prices[i] - min_buyin, profit)</span><br><span class="line"> <span class="keyword">return</span> profit</span><br><span class="line"> <span class="comment">#44 ms 14.4 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/18/leetcode-206/" class="prev">PRVE</a><a href="/2019/11/14/AWS-KMS/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,128 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-206 · MarkDown</title><meta name="description" content="leetcode-206 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-206</h1><div class="post-meta"><div class="post-time">2020年3月18日</div></div><div class="post-content"><h3 id="206-__u53CD_u8F6C_u94FE_u8868"><a href="#206-__u53CD_u8F6C_u94FE_u8868" class="headerlink" title="206. 反转链表"></a>206. 反转链表</h3><p><a href="https://leetcode-cn.com/problems/reverse-linked-list/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>最简单的思路是遍历链表一个列表去做存储,通过倒序读取列表的同时改写链表。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ListNode</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, x)</span>:</span></span><br><span class="line"> self.val = x</span><br><span class="line"> self.next = <span class="keyword">None</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">reverseList</span><span class="params">(self, head)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> head <span class="keyword">or</span> <span class="keyword">not</span> head.next:</span><br><span class="line"> <span class="keyword">return</span> head</span><br><span class="line"> nl = []</span><br><span class="line"> <span class="keyword">while</span> head.next:</span><br><span class="line"> nl.append(head)</span><br><span class="line"> head = head.next</span><br><span class="line"> nl.append(head)</span><br><span class="line"> l = len(nl)</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> range(l):</span><br><span class="line"> <span class="keyword">if</span> x == <span class="number">0</span>:</span><br><span class="line"> nl[x].next = <span class="keyword">None</span></span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> nl[x].next = nl[x<span class="number">-1</span>]</span><br><span class="line"> <span class="keyword">if</span> x == (l - <span class="number">1</span>):</span><br><span class="line"> <span class="keyword">return</span> nl[x]</span><br></pre></td></tr></table></figure>
<p>仔细想想自己又傻逼了,何必要遍历两次呢,在第一遍遍历的同时就能操作了:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">reverseList</span><span class="params">(self, head)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> head <span class="keyword">or</span> <span class="keyword">not</span> head.next:</span><br><span class="line"> <span class="keyword">return</span> head</span><br><span class="line"> prev_node = <span class="keyword">None</span></span><br><span class="line"> next_node = head.next</span><br><span class="line"> <span class="keyword">while</span> head:</span><br><span class="line"> next_node = head.next</span><br><span class="line"> head.next = prev_node</span><br><span class="line"> prev_node = head</span><br><span class="line"> head = next_node</span><br><span class="line"> <span class="keyword">return</span> prev_node</span><br></pre></td></tr></table></figure>
<p>然后是递归的做法,主要思路是一直进到最深一层–也就是链表的最后一个–的时候开始返回,同时修改那一层的两个 node。一开始踩了一个坑是返回了每一个node结果最后回到第一层的时候得到的是链表的末端其实只需要修改链表并不需要返回 node所以一开始到达链表末端的时候直接返回那一个node就可以了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">reverseList</span><span class="params">(self, head)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> head:</span><br><span class="line"> <span class="keyword">return</span> head</span><br><span class="line"> <span class="keyword">if</span> head.next:</span><br><span class="line"> ss = Solution()</span><br><span class="line"> last = ss.reverseList(head.next)</span><br><span class="line"> head.next.next = head</span><br><span class="line"> head.next = <span class="keyword">None</span></span><br><span class="line"> <span class="keyword">return</span> last</span><br><span class="line"> <span class="keyword">return</span> head</span><br></pre></td></tr></table></figure>
<p>一开始是用list来打草稿不过想明白递归之后就大同小异了</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">a</span><span class="params">(l:list)</span>-&gt;list:</span></span><br><span class="line"> k=[l[<span class="number">0</span>]]</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> l[<span class="number">1</span>:]:</span><br><span class="line"> b=a(l[<span class="number">1</span>:])</span><br><span class="line"> b.extend(k)</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> [l[<span class="number">0</span>]]</span><br><span class="line"> <span class="keyword">return</span> b</span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/23/leetcode-169/" class="prev">PRVE</a><a href="/2020/03/17/leetcode-121/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,126 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-169 · MarkDown</title><meta name="description" content="leetcode-169 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-169</h1><div class="post-meta"><div class="post-time">2020年3月23日</div></div><div class="post-content"><h3 id="169-__u591A_u6570_u5143_u7D20"><a href="#169-__u591A_u6570_u5143_u7D20" class="headerlink" title="169. 多数元素"></a>169. 多数元素</h3><p><a href="https://leetcode-cn.com/problems/majority-element/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>一开始的思路是遍历一遍整个列表,用一个字典去记录每个元素出现的次数,当次数大于 $\cfrac{n}{2}$ 时就可以得出结果。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">majorityElement</span><span class="params">(self, nums)</span> -&gt; int:</span></span><br><span class="line"> d = &#123;&#125;</span><br><span class="line"> l = len(nums)</span><br><span class="line"> <span class="keyword">for</span> n <span class="keyword">in</span> nums:</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> n <span class="keyword">in</span> d:</span><br><span class="line"> d[n] = <span class="number">0</span></span><br><span class="line"> d[n] = d[n] + <span class="number">1</span></span><br><span class="line"> <span class="keyword">if</span> d[n] &gt; l/<span class="number">2</span>:</span><br><span class="line"> <span class="keyword">return</span> n</span><br><span class="line"><span class="comment"># 64 ms 15.1 MB</span></span><br></pre></td></tr></table></figure>
<p>Python 也有专门计数的库,写起来更简单一点:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">majorityElement</span><span class="params">(self, nums)</span>:</span></span><br><span class="line"> counts = collections.Counter(nums)</span><br><span class="line"> <span class="keyword">return</span> max(counts.keys(), key=counts.get)</span><br><span class="line"><span class="comment"># 44 ms 15.1 MB</span></span><br></pre></td></tr></table></figure>
<p>由于要找的数出现次数大于 $\cfrac{n}{2}$,脑子里掠过一下<a href="https://zh.wikipedia.org/wiki/%E8%92%99%E5%9C%B0%E5%8D%A1%E7%BE%85%E6%96%B9%E6%B3%95" target="_blank" rel="noopener">蒙特卡罗算法</a>,后来在官方解答中也看到类似的思路了:</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">majorityElement</span><span class="params">(self, nums)</span>:</span></span><br><span class="line"> majority_count = len(nums)//<span class="number">2</span></span><br><span class="line"> <span class="keyword">while</span> <span class="keyword">True</span>:</span><br><span class="line"> candidate = random.choice(nums)</span><br><span class="line"> <span class="keyword">if</span> sum(<span class="number">1</span> <span class="keyword">for</span> elem <span class="keyword">in</span> nums <span class="keyword">if</span> elem == candidate) &gt; majority_count:</span><br><span class="line"> <span class="keyword">return</span> candidate</span><br><span class="line"></span><br><span class="line"><span class="comment">#作者LeetCode-Solution</span></span><br><span class="line"><span class="comment">#链接https://leetcode-cn.com/problems/majority-element/solution/duo-shu-yuan-su-by-leetcode-solution/</span></span><br><span class="line"><span class="comment">#来源力扣LeetCode</span></span><br><span class="line"><span class="comment">#著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/23/leetcode-225/" class="prev">PRVE</a><a href="/2020/03/18/leetcode-206/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,123 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-409 · MarkDown</title><meta name="description" content="leetcode-409 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-409</h1><div class="post-meta"><div class="post-time">2020年3月25日</div></div><div class="post-content"><h3 id="409-__u6700_u957F_u56DE_u6587_u4E32"><a href="#409-__u6700_u957F_u56DE_u6587_u4E32" class="headerlink" title="409. 最长回文串"></a>409. 最长回文串</h3><p><a href="https://leetcode-cn.com/problems/longest-palindrome/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>一开始理解错题目了,以为是寻找字符串中的最长回文串,结果是构造。但是原理基本一样,由于回文中心对称,所以是由多个偶数个相同字母和至多一个奇数个相同字母组成。</p>
<p>这样只要数给出的字符串中有几个偶数个相同字母和几个奇数个相同字母就可以了。奇数个相同字母可以减少一个当偶数个用,最后再加回去一个。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">longestPalindrome</span><span class="params">(self, s: str)</span> -&gt; int:</span></span><br><span class="line"> d = &#123;&#125;</span><br><span class="line"> <span class="keyword">for</span> l <span class="keyword">in</span> s:</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> l <span class="keyword">in</span> d:</span><br><span class="line"> d[l] = <span class="number">0</span></span><br><span class="line"> d[l] += <span class="number">1</span></span><br><span class="line"></span><br><span class="line"> i = <span class="number">0</span></span><br><span class="line"> odd = <span class="keyword">False</span></span><br><span class="line"> <span class="keyword">for</span> k, v <span class="keyword">in</span> d.items():</span><br><span class="line"> <span class="keyword">if</span> v % <span class="number">2</span>:</span><br><span class="line"> i += (v<span class="number">-1</span>)</span><br><span class="line"> odd = <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> i += v</span><br><span class="line"> <span class="keyword">if</span> odd:</span><br><span class="line"> i += <span class="number">1</span></span><br><span class="line"> <span class="keyword">return</span> i</span><br><span class="line"></span><br><span class="line"><span class="comment">#40 ms 13.6 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/25/leetcode-836/" class="prev">PRVE</a><a href="/2020/03/25/leetcode-543/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-836 · MarkDown</title><meta name="description" content="leetcode-836 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-836</h1><div class="post-meta"><div class="post-time">2020年3月25日</div></div><div class="post-content"><h3 id="836-__u77E9_u5F62_u91CD_u53E0"><a href="#836-__u77E9_u5F62_u91CD_u53E0" class="headerlink" title="836. 矩形重叠"></a>836. 矩形重叠</h3><p><a href="https://leetcode-cn.com/problems/rectangle-overlap/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>看两个矩形有没有重叠,就看两个矩形在坐标轴上的投影有没有重叠。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">isRectangleOverlap</span><span class="params">(self, rec1, rec2)</span> -&gt; bool:</span></span><br><span class="line"> <span class="keyword">return</span> ((min(rec1[<span class="number">2</span>], rec2[<span class="number">2</span>]) &gt; max(rec1[<span class="number">0</span>], rec2[<span class="number">0</span>]))</span><br><span class="line"> <span class="keyword">and</span> (min(rec1[<span class="number">3</span>], rec2[<span class="number">3</span>]) &gt; max(rec1[<span class="number">1</span>], rec2[<span class="number">1</span>])))</span><br><span class="line"></span><br><span class="line">s = Solution()</span><br><span class="line">s.isRectangleOverlap(rec1 = [<span class="number">0</span>,<span class="number">0</span>,<span class="number">2</span>,<span class="number">2</span>], rec2 = [<span class="number">1</span>,<span class="number">1</span>,<span class="number">3</span>,<span class="number">3</span>])</span><br><span class="line"></span><br><span class="line"><span class="comment">#40 ms 13.7 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/26/leetcode-876/" class="prev">PRVE</a><a href="/2020/03/25/leetcode-409/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,124 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-876 · MarkDown</title><meta name="description" content="leetcode-876 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-876</h1><div class="post-meta"><div class="post-time">2020年3月26日</div></div><div class="post-content"><h3 id="876-__u94FE_u8868_u7684_u4E2D_u95F4_u7ED3_u70B9"><a href="#876-__u94FE_u8868_u7684_u4E2D_u95F4_u7ED3_u70B9" class="headerlink" title="876. 链表的中间结点"></a>876. 链表的中间结点</h3><p><a href="https://leetcode-cn.com/problems/middle-of-the-linked-list/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>思路是遍历一遍得到整个链表,讲每个 node 放进一个 list就可以通过下标得到中间的。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># Definition for singly-linked list.</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ListNode</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">__init__</span><span class="params">(self, x)</span>:</span></span><br><span class="line"> self.val = x</span><br><span class="line"> self.next = <span class="keyword">None</span></span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">middleNode</span><span class="params">(self, head: ListNode)</span> -&gt; ListNode:</span></span><br><span class="line"> l = []</span><br><span class="line"> n = head</span><br><span class="line"> <span class="keyword">while</span> n.next:</span><br><span class="line"> l.append(n)</span><br><span class="line"> n = n.next</span><br><span class="line"> l.append(n)</span><br><span class="line"> <span class="keyword">return</span> l[len(l)//<span class="number">2</span>]</span><br><span class="line"></span><br><span class="line"> <span class="comment">#44 ms 13.7 MB</span></span><br></pre></td></tr></table></figure>
<p>看官方解答,还有一个骚操作,通过两个速度不一样的指针,一个一次走一步,一个两次走一步,快的走到底时,慢的就在中间了。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">middleNode</span><span class="params">(self, head: ListNode)</span> -&gt; ListNode:</span></span><br><span class="line"> slow = fast = head</span><br><span class="line"> <span class="keyword">while</span> fast <span class="keyword">and</span> fast.next:</span><br><span class="line"> slow = slow.next</span><br><span class="line"> fast = fast.next.next</span><br><span class="line"> <span class="keyword">return</span> slow</span><br><span class="line"></span><br><span class="line">作者LeetCode-Solution</span><br><span class="line">链接https://leetcode-cn.com/problems/middle-of-the-linked-list/solution/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut/</span><br><span class="line">来源力扣LeetCode</span><br><span class="line">著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。</span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/29/leetcode-1013/" class="prev">PRVE</a><a href="/2020/03/25/leetcode-836/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-1013 · MarkDown</title><meta name="description" content="leetcode-1013 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-1013</h1><div class="post-meta"><div class="post-time">2020年3月29日</div></div><div class="post-content"><h3 id="1013-__u5C06_u6570_u7EC4_u5206_u6210_u548C_u76F8_u7B49_u7684_u4E09_u4E2A_u90E8_u5206"><a href="#1013-__u5C06_u6570_u7EC4_u5206_u6210_u548C_u76F8_u7B49_u7684_u4E09_u4E2A_u90E8_u5206" class="headerlink" title="1013. 将数组分成和相等的三个部分"></a>1013. 将数组分成和相等的三个部分</h3><p><a href="https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>因为是整数数组如果能均分成三份则数组和肯定是3的倍数。然后遍历数组逐端求和使得和为 sum(A)/3。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">canThreePartsEqualSum</span><span class="params">(self, A)</span> -&gt; bool:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> A:</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">False</span></span><br><span class="line"> sa = sum(A)</span><br><span class="line"> <span class="keyword">if</span> sa % <span class="number">3</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">False</span></span><br><span class="line"> s = sa // <span class="number">3</span></span><br><span class="line"> s1 = <span class="number">0</span></span><br><span class="line"> s2 = <span class="number">0</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> range(len(A)):</span><br><span class="line"> s1 += A[i]</span><br><span class="line"> <span class="keyword">if</span> s1 == s <span class="keyword">and</span> (i+<span class="number">1</span>) &lt; len(A):</span><br><span class="line"> <span class="keyword">for</span> j <span class="keyword">in</span> range(len(A[i+<span class="number">1</span>:])):</span><br><span class="line"> s2 += A[i+<span class="number">1</span>+j]</span><br><span class="line"> <span class="keyword">if</span> s2 == s <span class="keyword">and</span> j+<span class="number">1</span> &lt; len(A[i+<span class="number">1</span>:]) <span class="keyword">and</span> sum(A[i+j+<span class="number">2</span>:])== s:</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">False</span></span><br><span class="line"><span class="comment">#60 ms 18.7 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/29/leetcode-914/" class="prev">PRVE</a><a href="/2020/03/26/leetcode-876/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-914 · MarkDown</title><meta name="description" content="leetcode-914 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-914</h1><div class="post-meta"><div class="post-time">2020年3月29日</div></div><div class="post-content"><h3 id="914-__u5361_u724C_u5206_u7EC4"><a href="#914-__u5361_u724C_u5206_u7EC4" class="headerlink" title="914. 卡牌分组"></a>914. 卡牌分组</h3><p><a href="https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>将大牌堆分成多个牌数量相等的小牌堆就是求每张牌数量的公约数。先遍历一遍得到每张牌的数量然后找出比2大的公约数即可。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">hasGroupsSizeX</span><span class="params">(self, deck)</span> -&gt; bool:</span></span><br><span class="line"> dc = &#123;&#125;</span><br><span class="line"> max_d = <span class="number">0</span></span><br><span class="line"> <span class="keyword">for</span> d <span class="keyword">in</span> deck:</span><br><span class="line"> <span class="keyword">if</span> d <span class="keyword">not</span> <span class="keyword">in</span> dc:</span><br><span class="line"> dc[d] = <span class="number">0</span></span><br><span class="line"> dc[d] += <span class="number">1</span></span><br><span class="line"> <span class="keyword">if</span> max_d &lt; d:</span><br><span class="line"> max_d = d</span><br><span class="line"> <span class="keyword">if</span> max_d &lt; dc[d]:</span><br><span class="line"> max_d = dc[d]</span><br><span class="line"> has_x = <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">if</span> max_d == <span class="number">1</span>:</span><br><span class="line"> max_d = <span class="number">2</span></span><br><span class="line"></span><br><span class="line"> <span class="keyword">for</span> i <span class="keyword">in</span> range(<span class="number">2</span>, max_d + <span class="number">1</span>):</span><br><span class="line"> has_x = <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">for</span> k,v <span class="keyword">in</span> dc.items():</span><br><span class="line"> <span class="keyword">if</span> v % i:</span><br><span class="line"> has_x = <span class="keyword">False</span></span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">if</span> has_x <span class="keyword">and</span> i &gt;= <span class="number">2</span>:</span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">return</span> <span class="keyword">False</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#56 ms 13.8 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/03/30/leetcode-999/" class="prev">PRVE</a><a href="/2020/03/29/leetcode-1013/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,123 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-1071 · MarkDown</title><meta name="description" content="leetcode-1071 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-1071</h1><div class="post-meta"><div class="post-time">2020年3月30日</div></div><div class="post-content"><h3 id="1071-__u5B57_u7B26_u4E32_u7684_u6700_u5927_u516C_u56E0_u5B50"><a href="#1071-__u5B57_u7B26_u4E32_u7684_u6700_u5927_u516C_u56E0_u5B50" class="headerlink" title="1071. 字符串的最大公因子"></a>1071. 字符串的最大公因子</h3><p><a href="https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>如果存在这样字符串,那它最大的长度就是这两个字符串长度的最大公约数。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">gcdOfStrings</span><span class="params">(self, str1: str, str2: str)</span> -&gt; str:</span></span><br><span class="line"> <span class="keyword">if</span> str1[<span class="number">0</span>] != str2[<span class="number">0</span>]:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">''</span></span><br><span class="line"></span><br><span class="line"> a = len(str1)</span><br><span class="line"> b = len(str2)</span><br><span class="line"> print(a, b)</span><br><span class="line"> <span class="keyword">if</span> a &lt; b:</span><br><span class="line"> str1, str2 = str2, str1</span><br><span class="line"> a = len(str1)</span><br><span class="line"> b = len(str2)</span><br><span class="line"></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> a%b:</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> range(<span class="number">0</span>, a//b):</span><br><span class="line"> <span class="keyword">if</span> str1[x*b:(x+<span class="number">1</span>)*b] != str2:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">''</span></span><br><span class="line"> <span class="keyword">return</span> str2</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> range(b, <span class="number">0</span>, <span class="number">-1</span>):</span><br><span class="line"> print(x)</span><br><span class="line"> <span class="keyword">if</span> x==b <span class="keyword">or</span> b%x <span class="keyword">or</span> a%x:</span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> <span class="keyword">for</span> y <span class="keyword">in</span> range(<span class="number">0</span>, b//x):</span><br><span class="line"> <span class="keyword">if</span> str2[y*x:(y+<span class="number">1</span>)*x] != str2[b-x:b]:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">''</span></span><br><span class="line"> <span class="keyword">for</span> y <span class="keyword">in</span> range(<span class="number">0</span>, a//x):</span><br><span class="line"> <span class="keyword">if</span> str1[y*x:(y+<span class="number">1</span>)*x] != str2[<span class="number">0</span>:x]:</span><br><span class="line"> <span class="keyword">return</span> <span class="string">''</span></span><br><span class="line"> <span class="keyword">return</span> str2[<span class="number">0</span>:x]</span><br><span class="line"><span class="comment"># 44 ms 13.9 MB</span></span><br></pre></td></tr></table></figure>
<p>官方解答中还给了一种巧妙的解法,如果 str1 + str2 == str2 + str1 的话,<a href="https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/solution/zi-fu-chuan-de-zui-da-gong-yin-zi-by-leetcode-solu/" target="_blank" rel="noopener">可以证明</a>必定存在这样一个字符串,其长度为两个字符串长度的最大公约数。</p>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/01/leetcode-1160/" class="prev">PRVE</a><a href="/2020/03/30/leetcode-999/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-1103 · MarkDown</title><meta name="description" content="leetcode-1103 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-1103</h1><div class="post-meta"><div class="post-time">2020年4月1日</div></div><div class="post-content"><h3 id="1103-__u5206_u7CD6_u679C_II"><a href="#1103-__u5206_u7CD6_u679C_II" class="headerlink" title="1103. 分糖果 II"></a>1103. 分糖果 II</h3><p><a href="https://leetcode-cn.com/problems/distribute-candies-to-people/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>小学奥数题。主要思路就是等差数列求和 <mjx-container class="MathJax" jax="SVG"><svg style="vertical-align: -1.625ex" xmlns="http://www.w3.org/2000/svg" width="20.379ex" height="4.656ex" role="img" focusable="false" viewbox="0 -1340 9007.3 2058.1" xmlns:xlink="http://www.w3.org/1999/xlink"><g stroke="currentColor" fill="currentColor" stroke-width="0" transform="matrix(1 0 0 -1 0 0)"><g data-mml-node="math"><g data-mml-node="mfrac"><g data-mml-node="mrow" transform="translate(220, 590) scale(0.707)"><g data-mml-node="mpadded"><g data-mml-node="mrow"/></g><g data-mml-node="mstyle" transform="scale(1.414)"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mo"><use xlink:href="#MJX-TEX-N-28"/></g><g data-mml-node="mo" transform="translate(389, 0)"><text data-variant="normal" transform="matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif"></text><text data-variant="normal" transform="translate(900, 0) matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif"></text></g><g data-mml-node="mo" transform="translate(2466.8, 0)"><use xlink:href="#MJX-TEX-N-2B"/></g><g data-mml-node="mo" transform="translate(3522.6, 0)"><text data-variant="normal" transform="matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif"></text><text data-variant="normal" transform="translate(900, 0) matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif"></text></g><g data-mml-node="mo" transform="translate(5322.6, 0)"><use xlink:href="#MJX-TEX-N-29"/></g><g data-mml-node="mo" transform="translate(5711.6, 0)"><use xlink:href="#MJX-TEX-N-D7"/></g><g data-mml-node="mo" transform="translate(6767.3, 0)"><text data-variant="normal" transform="matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif"></text><text data-variant="normal" transform="translate(900, 0) matrix(1 0 0 -1 0 0)" font-size="884px" font-family="serif"></text></g></g></g></g><g data-mml-node="mrow" transform="translate(4253.7, -506) scale(0.707)"><g data-mml-node="mpadded"><g data-mml-node="mrow"/></g><g data-mml-node="mstyle" transform="scale(1.414)"><g data-mml-node="TeXAtom" data-mjx-texclass="ORD"><g data-mml-node="mn"><use xlink:href="#MJX-TEX-N-32"/></g></g></g></g><rect width="8767.3" height="60" x="120" y="220"/></g></g></g></svg></mjx-container> 。可以用公式把每一个位置获得的总糖果数表示出来。我的方法稍微蠢了点,算了每一轮的总糖果数,其实可以直接求总共发了多少次糖果,除以每轮的人数就可以得出发了多少轮。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">distributeCandies</span><span class="params">(self, candies: int, num_people: int)</span>:</span></span><br><span class="line"> total = <span class="number">0</span></span><br><span class="line"> i = <span class="number">0</span></span><br><span class="line"> <span class="comment"># import ipdb; ipdb.set_trace()</span></span><br><span class="line"> <span class="keyword">while</span> total &lt;= candies:</span><br><span class="line"> t = (num_people*i)*num_people + int((<span class="number">1</span>+num_people)*num_people/<span class="number">2</span>)</span><br><span class="line"> <span class="keyword">if</span> total + t &lt;= candies:</span><br><span class="line"> total += t</span><br><span class="line"> i += <span class="number">1</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> remaining = candies - total</span><br><span class="line"> print(total, remaining, i)</span><br><span class="line"> l = []</span><br><span class="line"> <span class="keyword">for</span> n <span class="keyword">in</span> range(<span class="number">1</span>, num_people+<span class="number">1</span>):</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> total:</span><br><span class="line"> current_candy = n</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> current_candy = n+i*num_people</span><br><span class="line"></span><br><span class="line"> n_count = int((<span class="number">0</span>+(i<span class="number">-1</span>))*(i)/<span class="number">2</span>)</span><br><span class="line"> print(current_candy, n_count)</span><br><span class="line"> <span class="keyword">if</span> remaining &gt;= current_candy:</span><br><span class="line"> l.append(n_count*num_people + n*i + current_candy)</span><br><span class="line"> remaining -= current_candy</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> l.append(n_count*num_people + n*i + remaining)</span><br><span class="line"> remaining = <span class="number">0</span></span><br><span class="line"> <span class="keyword">return</span> l</span><br><span class="line"><span class="comment"># 28 ms 13.7 MB,</span></span><br></pre></td></tr></table></figure>
<svg style="display: none" id="MJX-SVG-global-cache"><defs><path id="MJX-TEX-N-28" d="M94 250Q94 319 104 381T127 488T164 576T202 643T244 695T277 729T302 750H315H319Q333 750 333 741Q333 738 316 720T275 667T226 581T184 443T167 250T184 58T225 -81T274 -167T316 -220T333 -241Q333 -250 318 -250H315H302L274 -226Q180 -141 137 -14T94 250Z"/><path id="MJX-TEX-N-2B" d="M56 237T56 250T70 270H369V420L370 570Q380 583 389 583Q402 583 409 568V270H707Q722 262 722 250T707 230H409V-68Q401 -82 391 -82H389H387Q375 -82 369 -68V230H70Q56 237 56 250Z"/><path id="MJX-TEX-N-29" d="M60 749L64 750Q69 750 74 750H86L114 726Q208 641 251 514T294 250Q294 182 284 119T261 12T224 -76T186 -143T145 -194T113 -227T90 -246Q87 -249 86 -250H74Q66 -250 63 -250T58 -247T55 -238Q56 -237 66 -225Q221 -64 221 250T66 725Q56 737 55 738Q55 746 60 749Z"/><path id="MJX-TEX-N-D7" d="M630 29Q630 9 609 9Q604 9 587 25T493 118L389 222L284 117Q178 13 175 11Q171 9 168 9Q160 9 154 15T147 29Q147 36 161 51T255 146L359 250L255 354Q174 435 161 449T147 471Q147 480 153 485T168 490Q173 490 175 489Q178 487 284 383L389 278L493 382Q570 459 587 475T609 491Q630 491 630 471Q630 464 620 453T522 355L418 250L522 145Q606 61 618 48T630 29Z"/><path id="MJX-TEX-N-32" d="M109 429Q82 429 66 447T50 491Q50 562 103 614T235 666Q326 666 387 610T449 465Q449 422 429 383T381 315T301 241Q265 210 201 149L142 93L218 92Q375 92 385 97Q392 99 409 186V189H449V186Q448 183 436 95T421 3V0H50V19V31Q50 38 56 46T86 81Q115 113 136 137Q145 147 170 174T204 211T233 244T261 278T284 308T305 340T320 369T333 401T340 431T343 464Q343 527 309 573T212 619Q179 619 154 602T119 569T109 550Q109 549 114 549Q132 549 151 535T170 489Q170 464 154 447T109 429Z"/></defs></svg></div></article></div></section><footer><div class="paginator"><a href="/2020/04/01/leetcode-compress-string-lcci/" class="prev">PRVE</a><a href="/2020/04/01/leetcode-1160/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-1160 · MarkDown</title><meta name="description" content="leetcode-1160 - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-1160</h1><div class="post-meta"><div class="post-time">2020年4月1日</div></div><div class="post-content"><h3 id="1160-__u62FC_u5199_u5355_u8BCD"><a href="#1160-__u62FC_u5199_u5355_u8BCD" class="headerlink" title="1160. 拼写单词"></a>1160. 拼写单词</h3><p><a href="https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>利用列表 remove 方法,检查 chars 中是否有足够的字母拼写 word</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">countCharacters</span><span class="params">(self, words, chars: str)</span> -&gt; int:</span></span><br><span class="line"></span><br><span class="line"> words_ = <span class="string">''</span></span><br><span class="line"> <span class="keyword">for</span> w <span class="keyword">in</span> words:</span><br><span class="line"> lchars = list(chars)</span><br><span class="line"> <span class="keyword">try</span>:</span><br><span class="line"> <span class="keyword">for</span> l <span class="keyword">in</span> w:</span><br><span class="line"> lchars.remove(l)</span><br><span class="line"> <span class="keyword">except</span>:</span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> words_ += w</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> len(words_)</span><br><span class="line"><span class="comment"># 152 ms 14.1 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/01/leetcode-1103/" class="prev">PRVE</a><a href="/2020/03/30/leetcode-1071/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-compress-string-lcci · MarkDown</title><meta name="description" content="leetcode-compress-string-lcci - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-compress-string-lcci</h1><div class="post-meta"><div class="post-time">2020年4月1日</div></div><div class="post-content"><h3 id="u9762_u8BD5_u9898_01-06-__u5B57_u7B26_u4E32_u538B_u7F29"><a href="#u9762_u8BD5_u9898_01-06-__u5B57_u7B26_u4E32_u538B_u7F29" class="headerlink" title="面试题 01.06. 字符串压缩"></a>面试题 01.06. 字符串压缩</h3><p><a href="https://leetcode-cn.com/problems/compress-string-lcci/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>遍历一遍字符串,遇到跟上一个字符不同的字符时记录上一个字符的重复长度。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">compressString</span><span class="params">(self, S: str)</span> -&gt; str:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> S:</span><br><span class="line"> <span class="keyword">return</span> S</span><br><span class="line"> c = <span class="string">''</span></span><br><span class="line"> prev = S[<span class="number">0</span>]</span><br><span class="line"> p_len = <span class="number">1</span></span><br><span class="line"> <span class="keyword">for</span> w <span class="keyword">in</span> S[<span class="number">1</span>:]:</span><br><span class="line"> <span class="keyword">if</span> w != prev:</span><br><span class="line"> c += <span class="string">'%s%s'</span> % (prev, p_len)</span><br><span class="line"> prev = w</span><br><span class="line"> p_len = <span class="number">1</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> p_len += <span class="number">1</span></span><br><span class="line"> c += <span class="string">'%s%s'</span> % (prev, p_len)</span><br><span class="line"> <span class="keyword">if</span> len(S) &gt; len(c):</span><br><span class="line"> <span class="keyword">return</span> c</span><br><span class="line"> <span class="keyword">return</span> S</span><br><span class="line"><span class="comment"># 52 ms 13.8 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/09/leetcode-the-masseuse-lcci/" class="prev">PRVE</a><a href="/2020/04/01/leetcode-1103/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,128 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-01-matrix · MarkDown</title><meta name="description" content="leetcode-01-matrix - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-01-matrix</h1><div class="post-meta"><div class="post-time">2020年4月16日</div></div><div class="post-content"><h3 id="542-_01__u77E9_u9635"><a href="#542-_01__u77E9_u9635" class="headerlink" title="542. 01 矩阵"></a>542. 01 矩阵</h3><p><a href="https://leetcode-cn.com/problems/01-matrix/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>想了两种思路</p>
<ol>
<li><p>0 位置的上下左右是 1 上下左右中有跟 1 相邻的就是 2以此类推从 0 的坐标开始往上下左右四个方向扩散。如果我们把同意个距离的看作是一层,可以用一个队列依次存放每一层的坐标,直至每个坐标都被计算过。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">updateMatrix</span><span class="params">(self, matrix: List[List[int]])</span> -&gt; List[List[int]]:</span></span><br><span class="line"> m, n = len(matrix), len(matrix[<span class="number">0</span>])</span><br><span class="line"> dist = [[<span class="number">0</span>] * n <span class="keyword">for</span> _ <span class="keyword">in</span> range(m)]</span><br><span class="line"> zeroes_pos = [(i, j) <span class="keyword">for</span> i <span class="keyword">in</span> range(m) <span class="keyword">for</span> j <span class="keyword">in</span> range(n) <span class="keyword">if</span> matrix[i][j] == <span class="number">0</span>]</span><br><span class="line"> <span class="comment"># 将所有的 0 添加进初始队列中</span></span><br><span class="line"> q = collections.deque(zeroes_pos)</span><br><span class="line"> seen = set(zeroes_pos)</span><br><span class="line"></span><br><span class="line"> <span class="comment"># 广度优先搜索</span></span><br><span class="line"> <span class="keyword">while</span> q:</span><br><span class="line"> i, j = q.popleft()</span><br><span class="line"> <span class="keyword">for</span> ni, nj <span class="keyword">in</span> [(i - <span class="number">1</span>, j), (i + <span class="number">1</span>, j), (i, j - <span class="number">1</span>), (i, j + <span class="number">1</span>)]:</span><br><span class="line"> <span class="keyword">if</span> <span class="number">0</span> &lt;= ni &lt; m <span class="keyword">and</span> <span class="number">0</span> &lt;= nj &lt; n <span class="keyword">and</span> (ni, nj) <span class="keyword">not</span> <span class="keyword">in</span> seen:</span><br><span class="line"> dist[ni][nj] = dist[i][j] + <span class="number">1</span></span><br><span class="line"> q.append((ni, nj))</span><br><span class="line"> seen.add((ni, nj))</span><br><span class="line"></span><br><span class="line"> <span class="keyword">return</span> dist</span><br></pre></td></tr></table></figure>
</li>
<li><p>从左上角开往右下角遍历矩阵,当前坐标的距离由左和上两个位置的值确定。遍历一遍后,再反过来从右下角开始往左上角遍历,当前坐标的距离根据右和下两个位置的值确定,比较这两次得出的值中较小的一个即为该点的距离。</p>
</li>
</ol>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/16/leetcode-merge-intervals/" class="prev">PRVE</a><a href="/2020/04/14/leetcode-add-two-numbers-ii/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-merge-intervals · MarkDown</title><meta name="description" content="leetcode-merge-intervals - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-merge-intervals</h1><div class="post-meta"><div class="post-time">2020年4月16日</div></div><div class="post-content"><h3 id="56-__u5408_u5E76_u533A_u95F4"><a href="#56-__u5408_u5E76_u533A_u95F4" class="headerlink" title="56. 合并区间"></a>56. 合并区间</h3><p><a href="https://leetcode-cn.com/problems/merge-intervals/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>首先将区间按起点由小到大排序,这样相邻的两个就能通过终点判断是否重合。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">merge</span><span class="params">(self, intervals)</span>:</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> intervals:</span><br><span class="line"> <span class="keyword">return</span> []</span><br><span class="line"> intervals.sort()</span><br><span class="line"> merged = []</span><br><span class="line"> l = len(intervals)</span><br><span class="line"> m = intervals[<span class="number">0</span>]</span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> range(l<span class="number">-1</span>):</span><br><span class="line"> j = intervals[x+<span class="number">1</span>]</span><br><span class="line"> <span class="keyword">if</span> m[<span class="number">1</span>] &gt;= j[<span class="number">0</span>]:</span><br><span class="line"> <span class="keyword">if</span> m[<span class="number">1</span>] &lt;= j[<span class="number">1</span>]:</span><br><span class="line"> m = [m[<span class="number">0</span>], j[<span class="number">1</span>]]</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> merged.append(m)</span><br><span class="line"> m = j</span><br><span class="line"> <span class="keyword">if</span> m:</span><br><span class="line"> merged.append(m)</span><br><span class="line"> <span class="keyword">return</span> merged</span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/16/leetcode-string-to-integer-atoi/" class="prev">PRVE</a><a href="/2020/04/16/leetcode-01-matrix/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-string-to-integer-atoi · MarkDown</title><meta name="description" content="leetcode-string-to-integer-atoi - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-string-to-integer-atoi</h1><div class="post-meta"><div class="post-time">2020年4月16日</div></div><div class="post-content"><h3 id="8-__u5B57_u7B26_u4E32_u8F6C_u6362_u6574_u6570__28atoi_29"><a href="#8-__u5B57_u7B26_u4E32_u8F6C_u6362_u6574_u6570__28atoi_29" class="headerlink" title="8. 字符串转换整数 (atoi)"></a>8. 字符串转换整数 (atoi)</h3><p><a href="https://leetcode-cn.com/problems/string-to-integer-atoi/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>没什么好说的,注意各种情况,识别到数字之后就一直要是数字。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">myAtoi</span><span class="params">(self, str: str)</span> -&gt; int:</span></span><br><span class="line"> p = <span class="string">''</span></span><br><span class="line"> str = str.lstrip()</span><br><span class="line"> n = <span class="string">''</span></span><br><span class="line"> min_int = <span class="number">-2</span>**<span class="number">31</span></span><br><span class="line"> max_int = <span class="number">2</span>**<span class="number">31</span><span class="number">-1</span></span><br><span class="line"> isnumeric = <span class="keyword">False</span></span><br><span class="line"> <span class="keyword">for</span> x <span class="keyword">in</span> str:</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> isnumeric <span class="keyword">and</span> x == <span class="string">'-'</span>:</span><br><span class="line"> p = <span class="string">'-'</span></span><br><span class="line"> isnumeric = <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> isnumeric <span class="keyword">and</span> x == <span class="string">'+'</span>:</span><br><span class="line"> isnumeric = <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">continue</span></span><br><span class="line"> <span class="keyword">if</span> x.isnumeric():</span><br><span class="line"> n += x</span><br><span class="line"> isnumeric = <span class="keyword">True</span></span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">break</span></span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> n:</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line"> <span class="keyword">if</span> int(n) &gt; max_int:</span><br><span class="line"> <span class="keyword">if</span> p:</span><br><span class="line"> <span class="keyword">return</span> min_int</span><br><span class="line"> <span class="keyword">else</span>:</span><br><span class="line"> <span class="keyword">return</span> max_int</span><br><span class="line"> p += n</span><br><span class="line"> <span class="keyword">return</span> int(p)</span><br><span class="line"><span class="comment"># 32 ms 13.6 MB</span></span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/21/leetcode-number-of-islands/" class="prev">PRVE</a><a href="/2020/04/16/leetcode-merge-intervals/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,122 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> leetcode-number-of-islands · MarkDown</title><meta name="description" content="leetcode-number-of-islands - Ching"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><div class="post"><article class="post-block"><h1 class="post-title">leetcode-number-of-islands</h1><div class="post-meta"><div class="post-time">2020年4月21日</div></div><div class="post-content"><h3 id="200-__u5C9B_u5C7F_u6570_u91CF"><a href="#200-__u5C9B_u5C7F_u6570_u91CF" class="headerlink" title="200. 岛屿数量"></a>200. 岛屿数量</h3><p><a href="https://leetcode-cn.com/problems/number-of-islands/" target="_blank" rel="noopener">题目</a></p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><a id="more"></a>
<p>这种矩阵题现在第一反应就是用<a href="(https://zh.wikipedia.org/zh-cn/%E5%B9%BF%E5%BA%A6%E4%BC%98%E5%85%88%E6%90%9C%E7%B4%A2)">广度优先搜索</a>类似之前算和0之间的距离那题。遍历矩阵遇到 1 就将 1 改成 0然后广度优先搜索找出 1 相邻的所有 1这就是一个岛屿以此类推。</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> collections</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Solution</span>:</span></span><br><span class="line"> <span class="function"><span class="keyword">def</span> <span class="title">numIslands</span><span class="params">(self, grid)</span> -&gt; int:</span></span><br><span class="line"> rows = len(grid)</span><br><span class="line"> <span class="keyword">if</span> <span class="keyword">not</span> rows:</span><br><span class="line"> <span class="keyword">return</span> <span class="number">0</span></span><br><span class="line"> cols = len(grid[<span class="number">0</span>])</span><br><span class="line"> islands = <span class="number">0</span></span><br><span class="line"> <span class="keyword">for</span> r <span class="keyword">in</span> range(rows):</span><br><span class="line"> <span class="keyword">for</span> l <span class="keyword">in</span> range(cols):</span><br><span class="line"> <span class="keyword">if</span> grid[r][l] == <span class="string">'1'</span>:</span><br><span class="line"> islands += <span class="number">1</span></span><br><span class="line"> grid[r][l] = <span class="string">'0'</span></span><br><span class="line"> neighbors = collections.deque([(r, l)])</span><br><span class="line"> <span class="keyword">while</span> neighbors:</span><br><span class="line"> x, y = neighbors.popleft()</span><br><span class="line"> <span class="keyword">for</span> x_, y_ <span class="keyword">in</span> [[x<span class="number">-1</span>, y], [x+<span class="number">1</span>, y], [x, y<span class="number">-1</span>], [x, y+<span class="number">1</span>]]:</span><br><span class="line"> <span class="keyword">if</span> <span class="number">0</span>&lt;=x_&lt;rows <span class="keyword">and</span> <span class="number">0</span>&lt;=y_&lt;cols <span class="keyword">and</span> grid[x_][y_] == <span class="string">'1'</span>:</span><br><span class="line"> neighbors.append([x_, y_])</span><br><span class="line"> grid[x_][y_] = <span class="string">'0'</span></span><br><span class="line"> <span class="keyword">return</span> islands</span><br></pre></td></tr></table></figure>
</div></article></div></section><footer><div class="paginator"><a href="/2020/04/16/leetcode-string-to-integer-atoi/" class="next">NEXT</a></div><div class="copyright"><p>© 2016 - 2020 <a href="http://blog.tunpok.com">Ching</a>, unless otherwise noted.</p></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

1
public/CNAME Normal file
View File

@ -0,0 +1 @@
blog.tunpok.com

View File

@ -0,0 +1,158 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/16/Flask-Day-2/" class="post-title-link">Flask Day 2</a></h2><div class="post-meta"><div class="post-time">2016年2月16日</div></div><div class="post-content"><p>To handle web forms we use <a href="http://packages.python.org/Flask-WTF" target="_blank" rel="noopener">Flask-WTF </a>. So we need to write a config file (file <code>config.py</code>):</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">WTF_CSRF_ENABLED = <span class="keyword">True</span></span><br><span class="line">SECRET_KEY = <span class="string">'you-will-never-guess'</span></span><br></pre></td></tr></table></figure>
<p>And then you need to use this config (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line">app.config.from_object(<span class="string">'config'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Lets build a simple form (file <code>app/forms.app</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask.ext.wtf <span class="keyword">import</span> Form</span><br><span class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, BooleanField</span><br><span class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> DataRequired</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginForm</span><span class="params">(Form)</span>:</span></span><br><span class="line"> openid = StringField(<span class="string">'openid'</span>, validators=[DataRequired()])</span><br><span class="line"> remember_me = BooleanField(<span class="string">'remember_me'</span>, default=<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
<p>The <code>DataRequired()</code> is a validator that checks the field is empty or not.</p>
<p>After that, we need a HTML page to show the form (file <code>app/templates/login.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- extend from base layout --&gt;</span></span><br><span class="line">&#123;% extends "base.html" %&#125;</span><br><span class="line"></span><br><span class="line">&#123;% block content %&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Sign In<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">""</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">name</span>=<span class="string">"login"</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.hidden_tag() &#125;&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line"> Please enter your OpenID:<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.openid(size=80) &#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; form.remember_me &#125;&#125; Remember Me<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Sign In"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line">&#123;% endblock %&#125;</span><br></pre></td></tr></table></figure>
<p>The final step is to code a view function that renders the template and receiving data from form (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"><span class="keyword">from</span> .forms <span class="keyword">import</span> LoginForm</span><br><span class="line"></span><br><span class="line"><span class="comment"># index view function suppressed for brevity</span></span><br><span class="line"></span><br><span class="line">app.route(<span class="string">'/login'</span>, methods=[<span class="string">'GET'</span>, <span class="string">'POST'</span>])</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">()</span>:</span></span><br><span class="line"> form = LoginForm()</span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> flash(<span class="string">'Login requested for OpenID="%s", remember_me=%s'</span> %</span><br><span class="line"> (form.openid.data, str(form.remember_me.data)))</span><br><span class="line"> <span class="keyword">return</span> redirect(<span class="string">'/index'</span>)</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'login.html'</span>, </span><br><span class="line"> title=<span class="string">'Sign In'</span>,</span><br><span class="line"> form=form)</span><br></pre></td></tr></table></figure>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/15/Flask-Day-1/" class="post-title-link">Flask Day 1</a></h2><div class="post-meta"><div class="post-time">2016年2月15日</div></div><div class="post-content"><h3 id="u201CHello_World_u201D_in_Flask"><a href="#u201CHello_World_u201D_in_Flask" class="headerlink" title="“Hello World” in Flask"></a>“Hello World” in Flask</h3><p>Create a folder named <code>microblog</code> (or whatever you want). Then cd into that folder and run following prompt in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m venv flask</span><br></pre></td></tr></table></figure>
<p>Now youll have a folder named <code>flask</code> inside <code>microblog</code>, containing a private version of Python interpreter.</p>
<p>And you should install <strong>flask</strong> and extensions by the commands below:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ flask/bin/pip install flask</span><br><span class="line">$ flask/bin/pip install flask-login</span><br><span class="line">$ flask/bin/pip install flask-openid</span><br><span class="line">$ flask/bin/pip install flask-mail</span><br><span class="line">$ flask/bin/pip install flask-sqlalchemy</span><br><span class="line">$ flask/bin/pip install sqlalchemy-migrate</span><br><span class="line">$ flask/bin/pip install flask-whooshalchemy</span><br><span class="line">$ flask/bin/pip install flask-wtf</span><br><span class="line">$ flask/bin/pip install flask-babel</span><br><span class="line">$ flask/bin/pip install guess_language</span><br><span class="line">$ flask/bin/pip install flipflop</span><br><span class="line">$ flask/bin/pip install coverage</span><br></pre></td></tr></table></figure>
<p>After that, lets create the basic structure for our application: <code>app</code> <code>app/static</code> <code>app/templates</code> <code>tmp</code>.</p>
<ol>
<li><code>app</code> — where the application package is</li>
<li><code>static</code> — stores static files like images, javascripts, and css.</li>
<li><code>templates</code> — where templates will go.</li>
</ol>
<p>Then you can start with <code>__init__.py</code> which should put into app folder (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>The views are the handlers that response to requests from web browsers or other clients. Each view function is mapped to one or more request URLs.</p>
<p>Lets see what a views function looks like (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Finally we should create a script to starts up the web server with our application(file <code>run.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!flask/bin/python</span></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line">app.run(debug=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
<p>To indicating that is an executable file you need to run this in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod a+x run.py</span><br></pre></td></tr></table></figure>
<p>Now the file structure should look like:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">microblog\</span><br><span class="line"> flask\</span><br><span class="line"> &lt;virtual environment files&gt;</span><br><span class="line"> app\</span><br><span class="line"> static\</span><br><span class="line"> templates\</span><br><span class="line"> __init__.py</span><br><span class="line"> views.py</span><br><span class="line"> tmp\</span><br><span class="line"> run.py</span><br></pre></td></tr></table></figure>
<p>Then start to write the template (file <code>app/templates/index.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#123;&#123; title &#125;&#125; - microblog<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; user.nickname &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Now lets write the view function that uses this template (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/')</span></span><br><span class="line"><span class="meta">@app.route('/index')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> user = &#123;<span class="string">'nickname'</span>: <span class="string">'ching'</span>&#125; <span class="comment"># fake user</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>,</span><br><span class="line"> title=<span class="string">'Home'</span>,</span><br><span class="line"> user=user)</span><br></pre></td></tr></table></figure>
<p><code>render_template</code> function is what we import from Flask framework to render the template. It uses <a href="http://jinja.pocoo.org/" target="_blank" rel="noopener">Jinja2</a> templating engine.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/12/first-post/" class="post-title-link">First Post</a></h2><div class="post-meta"><div class="post-time">2016年2月12日</div></div><div class="post-content"><p>This is the very first post I wrote,</p>
<p>with <a href="https://www.typora.io/" target="_blank" rel="noopener">Typora</a> &amp; <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>.</p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,132 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/04/25/Django-Manager-Method/" class="post-title-link">Django Manager Method</a></h2><div class="post-meta"><div class="post-time">2016年4月25日</div></div><div class="post-content"><h4 id="Django_Manager"><a href="#Django_Manager" class="headerlink" title="Django Manager"></a>Django Manager</h4><p>Django 里会为每一个 model 生成一个 Manager默认名字为 objects一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理QuerySet 方法是对数据库操作的封装。</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.db <span class="keyword">import</span> models</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> ...</span><br><span class="line"> people = models.Manager()</span><br></pre></td></tr></table></figure>
<p>上面这个 model<code>Person.objects</code>会产生一个<code>AttributeError</code>,但是<code>Person.people</code>就可以正常操作。因为默认的 manager 已经变成 peopleobjects 这个 manager 没有重新声明,不起作用。</p>
<h4 id="u81EA_u5B9A_u4E49_Manager"><a href="#u81EA_u5B9A_u4E49_Manager" class="headerlink" title="自定义 Manager"></a>自定义 Manager</h4><p>通常需要自定义 manager 的情况有两点:</p>
<ol>
<li>需要修改/扩展 Django 的 manager 方法</li>
<li>需要修改返回的 QuerySet</li>
</ol>
<h4 id="u9ED8_u8BA4_Manager"><a href="#u9ED8_u8BA4_Manager" class="headerlink" title="默认 Manager"></a>默认 Manager</h4><p>如果使用自定义的 manager 需要注意的是Django 将 model 中定义的第一个 manager 认为是默认 manager而且 Django 框架中会用到默认 manager。</p>
<p>笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager并放在第一个。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> title = models.CharField(max_length=<span class="number">100</span>)</span><br><span class="line"> author = models.CharField(max_length=<span class="number">50</span>)</span><br><span class="line"> </span><br><span class="line"> objects = models.Manager() <span class="comment"># default manager</span></span><br><span class="line"> custom_objects = CustomBOokManager() <span class="comment"># custom manager</span></span><br></pre></td></tr></table></figure>
<p><a href="http://blog.csdn.net/sicofield/article/details/49283751" target="_blank" rel="noopener">source</a></p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,168 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/10/TastyPie-Note-1/" class="post-title-link">TastyPie Note 1</a></h2><div class="post-meta"><div class="post-time">2016年5月10日</div></div><div class="post-content"><h3 id="Flow_Through_The_Request/Response_Cycle"><a href="#Flow_Through_The_Request/Response_Cycle" class="headerlink" title="Flow Through The Request/Response Cycle"></a>Flow Through The Request/Response Cycle</h3><p>Tastypie can be thought of as a set of class-based view that provide the API functionality. All routing/middleware/response-handling aspectss are the same as a typical Django app. Where the differs is in the view itself.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Walking through what a GET request to a list endpoint looks like:</p>
<ul>
<li><p>The <code>Resource.urls</code> are checked by Djangos url resolvers.</p>
</li>
<li><p>On a match for the list view, <code>Resource.wrap_view(&#39;dispatch_list&#39;)</code> is called. <code>wrap_view</code> provides basic error handling &amp; allows for returning serilized errors.</p>
</li>
<li><p>Because dispatch_list was passed to <code>wrap_view</code>, <code>Resource.dispatch_list</code> is called next. This is a thin wrapper around <code>Resource.dispatch</code>.</p>
</li>
<li><p><code>dispatch</code> does a bunch of havy lifting. It ensures:</p>
<ul>
<li>the requested HTTP method is in <code>allowed_methos</code> (<code>method_check</code>).</li>
<li>the class has a method that can handle the request(<code>get_list</code>)</li>
<li>the user is authenticated(<code>is_authenticated</code>)</li>
<li>the user has no exceeded their throttle(<code>throttle_check</code>).</li>
</ul>
<p>At this point, <code>dispatch</code> actually calls the requested method (<code>get_list</code>).</p>
</li>
<li><p><code>get_list</code> does the actual work of API. It does:</p>
<ul>
<li>A fetch of the available objects via <code>Resource.obj_get_list</code>. In the case of <code>ModelResource</code>, this builds the ORM filters to apply (<code>ModelResource.build_filters</code>). It then gets the <code>QuerySet</code> via <code>ModelResource.get_object_list</code> (which performs <code>Resource.authorized_read_list</code> to possibly limit the set the user can work with) and applies the built filters to it.</li>
<li>It then sorts the objects based on user input (<code>ModelResource.apply_sorting</code>).</li>
<li>Then it paginates the results using the supplied <code>Paginator</code> &amp; pulls out the data to be serialized.</li>
<li>The objects in the page have <code>full_dehydrate</code> applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.</li>
<li>Finally, it calls <code>Resource.create_response</code>.</li>
</ul>
</li>
<li><p><code>create_response</code> is a shortcut method that:</p>
<ul>
<li>Determines the desired response format (<code>Resource.determine_format</code>).</li>
<li>Serializes the data given to it in the proper format.</li>
<li>Returns a Django <code>HttpResponse</code> (200 OK) with the serialized data.</li>
</ul>
</li>
<li><p>We bubble back up the call stack to <code>dispatch</code>. The last thing <code>dispatch</code> does is potentially store that a request occured for future throttling (<code>Resource.log_throttled_access</code>) then either returns the <code>HttpResponse</code> or wraps whatever data came back in a response (so Django doesnt freak out).</p>
</li>
</ul>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/04/Tastypie/" class="post-title-link">Tastypie</a></h2><div class="post-meta"><div class="post-time">2016年5月4日</div></div><div class="post-content"><h4 id="Resources_in_Tastypie"><a href="#Resources_in_Tastypie" class="headerlink" title="Resources in Tastypie"></a>Resources in Tastypie</h4><p>Resources are the heart of Tastypie. By defining a resource we can actually convert a model into an API stream. The data is automatically converted into API response.</p>
<p>Understanding the process of creating a resource.</p>
<ol>
<li>Import ModelResource from Tastypie.</li>
<li>Import models from services app</li>
<li>Create custom resource by inheriting ModelResource and link app model in inner Meta class of resource. </li>
</ol>
<p>Add API URL in the urls.py of app.</p>
<h4 id="Dehydrating_the_JSON_data"><a href="#Dehydrating_the_JSON_data" class="headerlink" title="Dehydrating the JSON data"></a>Dehydrating the JSON data</h4><p><img src="https://impythonist.files.wordpress.com/2016/04/tastypie_ill.png?w=800" alt="flow"></p>
<p>Dehydration in Tastypie means making alterations before sending data to the client. Suppose we need to send capitalized product names instead of small letters. Now we see two kinds of dehydrate methods.</p>
<h5 id="Dehydrate_field_method"><a href="#Dehydrate_field_method" class="headerlink" title="Dehydrate_field method"></a>Dehydrate_field method</h5><p>This <code>dehydrate_field</code> is uesd to modify field on the response JSON. </p>
<h5 id="Dehydrate_method"><a href="#Dehydrate_method" class="headerlink" title="Dehydrate method"></a>Dehydrate method</h5><p>Dehydrate method is useful for aadding additional fields to bundle (response data). </p>
<p>Similarly using <code>hydrate</code> method we can alter the bundle data which is generated from request at the time of PUT or POST methods.</p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,222 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/10/TastyPie-Note-1/" class="post-title-link">TastyPie Note 1</a></h2><div class="post-meta"><div class="post-time">2016年5月10日</div></div><div class="post-content"><h3 id="Flow_Through_The_Request/Response_Cycle"><a href="#Flow_Through_The_Request/Response_Cycle" class="headerlink" title="Flow Through The Request/Response Cycle"></a>Flow Through The Request/Response Cycle</h3><p>Tastypie can be thought of as a set of class-based view that provide the API functionality. All routing/middleware/response-handling aspectss are the same as a typical Django app. Where the differs is in the view itself.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Walking through what a GET request to a list endpoint looks like:</p>
<ul>
<li><p>The <code>Resource.urls</code> are checked by Djangos url resolvers.</p>
</li>
<li><p>On a match for the list view, <code>Resource.wrap_view(&#39;dispatch_list&#39;)</code> is called. <code>wrap_view</code> provides basic error handling &amp; allows for returning serilized errors.</p>
</li>
<li><p>Because dispatch_list was passed to <code>wrap_view</code>, <code>Resource.dispatch_list</code> is called next. This is a thin wrapper around <code>Resource.dispatch</code>.</p>
</li>
<li><p><code>dispatch</code> does a bunch of havy lifting. It ensures:</p>
<ul>
<li>the requested HTTP method is in <code>allowed_methos</code> (<code>method_check</code>).</li>
<li>the class has a method that can handle the request(<code>get_list</code>)</li>
<li>the user is authenticated(<code>is_authenticated</code>)</li>
<li>the user has no exceeded their throttle(<code>throttle_check</code>).</li>
</ul>
<p>At this point, <code>dispatch</code> actually calls the requested method (<code>get_list</code>).</p>
</li>
<li><p><code>get_list</code> does the actual work of API. It does:</p>
<ul>
<li>A fetch of the available objects via <code>Resource.obj_get_list</code>. In the case of <code>ModelResource</code>, this builds the ORM filters to apply (<code>ModelResource.build_filters</code>). It then gets the <code>QuerySet</code> via <code>ModelResource.get_object_list</code> (which performs <code>Resource.authorized_read_list</code> to possibly limit the set the user can work with) and applies the built filters to it.</li>
<li>It then sorts the objects based on user input (<code>ModelResource.apply_sorting</code>).</li>
<li>Then it paginates the results using the supplied <code>Paginator</code> &amp; pulls out the data to be serialized.</li>
<li>The objects in the page have <code>full_dehydrate</code> applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.</li>
<li>Finally, it calls <code>Resource.create_response</code>.</li>
</ul>
</li>
<li><p><code>create_response</code> is a shortcut method that:</p>
<ul>
<li>Determines the desired response format (<code>Resource.determine_format</code>).</li>
<li>Serializes the data given to it in the proper format.</li>
<li>Returns a Django <code>HttpResponse</code> (200 OK) with the serialized data.</li>
</ul>
</li>
<li><p>We bubble back up the call stack to <code>dispatch</code>. The last thing <code>dispatch</code> does is potentially store that a request occured for future throttling (<code>Resource.log_throttled_access</code>) then either returns the <code>HttpResponse</code> or wraps whatever data came back in a response (so Django doesnt freak out).</p>
</li>
</ul>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/04/Tastypie/" class="post-title-link">Tastypie</a></h2><div class="post-meta"><div class="post-time">2016年5月4日</div></div><div class="post-content"><h4 id="Resources_in_Tastypie"><a href="#Resources_in_Tastypie" class="headerlink" title="Resources in Tastypie"></a>Resources in Tastypie</h4><p>Resources are the heart of Tastypie. By defining a resource we can actually convert a model into an API stream. The data is automatically converted into API response.</p>
<p>Understanding the process of creating a resource.</p>
<ol>
<li>Import ModelResource from Tastypie.</li>
<li>Import models from services app</li>
<li>Create custom resource by inheriting ModelResource and link app model in inner Meta class of resource. </li>
</ol>
<p>Add API URL in the urls.py of app.</p>
<h4 id="Dehydrating_the_JSON_data"><a href="#Dehydrating_the_JSON_data" class="headerlink" title="Dehydrating the JSON data"></a>Dehydrating the JSON data</h4><p><img src="https://impythonist.files.wordpress.com/2016/04/tastypie_ill.png?w=800" alt="flow"></p>
<p>Dehydration in Tastypie means making alterations before sending data to the client. Suppose we need to send capitalized product names instead of small letters. Now we see two kinds of dehydrate methods.</p>
<h5 id="Dehydrate_field_method"><a href="#Dehydrate_field_method" class="headerlink" title="Dehydrate_field method"></a>Dehydrate_field method</h5><p>This <code>dehydrate_field</code> is uesd to modify field on the response JSON. </p>
<h5 id="Dehydrate_method"><a href="#Dehydrate_method" class="headerlink" title="Dehydrate method"></a>Dehydrate method</h5><p>Dehydrate method is useful for aadding additional fields to bundle (response data). </p>
<p>Similarly using <code>hydrate</code> method we can alter the bundle data which is generated from request at the time of PUT or POST methods.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/04/25/Django-Manager-Method/" class="post-title-link">Django Manager Method</a></h2><div class="post-meta"><div class="post-time">2016年4月25日</div></div><div class="post-content"><h4 id="Django_Manager"><a href="#Django_Manager" class="headerlink" title="Django Manager"></a>Django Manager</h4><p>Django 里会为每一个 model 生成一个 Manager默认名字为 objects一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理QuerySet 方法是对数据库操作的封装。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.db <span class="keyword">import</span> models</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> ...</span><br><span class="line"> people = models.Manager()</span><br></pre></td></tr></table></figure>
<p>上面这个 model<code>Person.objects</code>会产生一个<code>AttributeError</code>,但是<code>Person.people</code>就可以正常操作。因为默认的 manager 已经变成 peopleobjects 这个 manager 没有重新声明,不起作用。</p>
<h4 id="u81EA_u5B9A_u4E49_Manager"><a href="#u81EA_u5B9A_u4E49_Manager" class="headerlink" title="自定义 Manager"></a>自定义 Manager</h4><p>通常需要自定义 manager 的情况有两点:</p>
<ol>
<li>需要修改/扩展 Django 的 manager 方法</li>
<li>需要修改返回的 QuerySet</li>
</ol>
<h4 id="u9ED8_u8BA4_Manager"><a href="#u9ED8_u8BA4_Manager" class="headerlink" title="默认 Manager"></a>默认 Manager</h4><p>如果使用自定义的 manager 需要注意的是Django 将 model 中定义的第一个 manager 认为是默认 manager而且 Django 框架中会用到默认 manager。</p>
<p>笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager并放在第一个。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> title = models.CharField(max_length=<span class="number">100</span>)</span><br><span class="line"> author = models.CharField(max_length=<span class="number">50</span>)</span><br><span class="line"> </span><br><span class="line"> objects = models.Manager() <span class="comment"># default manager</span></span><br><span class="line"> custom_objects = CustomBOokManager() <span class="comment"># custom manager</span></span><br></pre></td></tr></table></figure>
<p><a href="http://blog.csdn.net/sicofield/article/details/49283751" target="_blank" rel="noopener">source</a></p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/16/Flask-Day-2/" class="post-title-link">Flask Day 2</a></h2><div class="post-meta"><div class="post-time">2016年2月16日</div></div><div class="post-content"><p>To handle web forms we use <a href="http://packages.python.org/Flask-WTF" target="_blank" rel="noopener">Flask-WTF </a>. So we need to write a config file (file <code>config.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">WTF_CSRF_ENABLED = <span class="keyword">True</span></span><br><span class="line">SECRET_KEY = <span class="string">'you-will-never-guess'</span></span><br></pre></td></tr></table></figure>
<p>And then you need to use this config (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line">app.config.from_object(<span class="string">'config'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Lets build a simple form (file <code>app/forms.app</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask.ext.wtf <span class="keyword">import</span> Form</span><br><span class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, BooleanField</span><br><span class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> DataRequired</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginForm</span><span class="params">(Form)</span>:</span></span><br><span class="line"> openid = StringField(<span class="string">'openid'</span>, validators=[DataRequired()])</span><br><span class="line"> remember_me = BooleanField(<span class="string">'remember_me'</span>, default=<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
<p>The <code>DataRequired()</code> is a validator that checks the field is empty or not.</p>
<p>After that, we need a HTML page to show the form (file <code>app/templates/login.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- extend from base layout --&gt;</span></span><br><span class="line">&#123;% extends "base.html" %&#125;</span><br><span class="line"></span><br><span class="line">&#123;% block content %&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Sign In<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">""</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">name</span>=<span class="string">"login"</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.hidden_tag() &#125;&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line"> Please enter your OpenID:<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.openid(size=80) &#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; form.remember_me &#125;&#125; Remember Me<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Sign In"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line">&#123;% endblock %&#125;</span><br></pre></td></tr></table></figure>
<p>The final step is to code a view function that renders the template and receiving data from form (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"><span class="keyword">from</span> .forms <span class="keyword">import</span> LoginForm</span><br><span class="line"></span><br><span class="line"><span class="comment"># index view function suppressed for brevity</span></span><br><span class="line"></span><br><span class="line">app.route(<span class="string">'/login'</span>, methods=[<span class="string">'GET'</span>, <span class="string">'POST'</span>])</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">()</span>:</span></span><br><span class="line"> form = LoginForm()</span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> flash(<span class="string">'Login requested for OpenID="%s", remember_me=%s'</span> %</span><br><span class="line"> (form.openid.data, str(form.remember_me.data)))</span><br><span class="line"> <span class="keyword">return</span> redirect(<span class="string">'/index'</span>)</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'login.html'</span>, </span><br><span class="line"> title=<span class="string">'Sign In'</span>,</span><br><span class="line"> form=form)</span><br></pre></td></tr></table></figure>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/15/Flask-Day-1/" class="post-title-link">Flask Day 1</a></h2><div class="post-meta"><div class="post-time">2016年2月15日</div></div><div class="post-content"><h3 id="u201CHello_World_u201D_in_Flask"><a href="#u201CHello_World_u201D_in_Flask" class="headerlink" title="“Hello World” in Flask"></a>“Hello World” in Flask</h3><p>Create a folder named <code>microblog</code> (or whatever you want). Then cd into that folder and run following prompt in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m venv flask</span><br></pre></td></tr></table></figure>
<p>Now youll have a folder named <code>flask</code> inside <code>microblog</code>, containing a private version of Python interpreter.</p>
<p>And you should install <strong>flask</strong> and extensions by the commands below:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ flask/bin/pip install flask</span><br><span class="line">$ flask/bin/pip install flask-login</span><br><span class="line">$ flask/bin/pip install flask-openid</span><br><span class="line">$ flask/bin/pip install flask-mail</span><br><span class="line">$ flask/bin/pip install flask-sqlalchemy</span><br><span class="line">$ flask/bin/pip install sqlalchemy-migrate</span><br><span class="line">$ flask/bin/pip install flask-whooshalchemy</span><br><span class="line">$ flask/bin/pip install flask-wtf</span><br><span class="line">$ flask/bin/pip install flask-babel</span><br><span class="line">$ flask/bin/pip install guess_language</span><br><span class="line">$ flask/bin/pip install flipflop</span><br><span class="line">$ flask/bin/pip install coverage</span><br></pre></td></tr></table></figure>
<p>After that, lets create the basic structure for our application: <code>app</code> <code>app/static</code> <code>app/templates</code> <code>tmp</code>.</p>
<ol>
<li><code>app</code> — where the application package is</li>
<li><code>static</code> — stores static files like images, javascripts, and css.</li>
<li><code>templates</code> — where templates will go.</li>
</ol>
<p>Then you can start with <code>__init__.py</code> which should put into app folder (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>The views are the handlers that response to requests from web browsers or other clients. Each view function is mapped to one or more request URLs.</p>
<p>Lets see what a views function looks like (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Finally we should create a script to starts up the web server with our application(file <code>run.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!flask/bin/python</span></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line">app.run(debug=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
<p>To indicating that is an executable file you need to run this in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod a+x run.py</span><br></pre></td></tr></table></figure>
<p>Now the file structure should look like:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">microblog\</span><br><span class="line"> flask\</span><br><span class="line"> &lt;virtual environment files&gt;</span><br><span class="line"> app\</span><br><span class="line"> static\</span><br><span class="line"> templates\</span><br><span class="line"> __init__.py</span><br><span class="line"> views.py</span><br><span class="line"> tmp\</span><br><span class="line"> run.py</span><br></pre></td></tr></table></figure>
<p>Then start to write the template (file <code>app/templates/index.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#123;&#123; title &#125;&#125; - microblog<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; user.nickname &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Now lets write the view function that uses this template (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/')</span></span><br><span class="line"><span class="meta">@app.route('/index')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> user = &#123;<span class="string">'nickname'</span>: <span class="string">'ching'</span>&#125; <span class="comment"># fake user</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>,</span><br><span class="line"> title=<span class="string">'Home'</span>,</span><br><span class="line"> user=user)</span><br></pre></td></tr></table></figure>
<p><code>render_template</code> function is what we import from Flask framework to render the template. It uses <a href="http://jinja.pocoo.org/" target="_blank" rel="noopener">Jinja2</a> templating engine.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/12/first-post/" class="post-title-link">First Post</a></h2><div class="post-meta"><div class="post-time">2016年2月12日</div></div><div class="post-content"><p>This is the very first post I wrote,</p>
<p>with <a href="https://www.typora.io/" target="_blank" rel="noopener">Typora</a> &amp; <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>.</p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,130 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2018/05/31/bash-function-and-awk/" class="post-title-link">bash function and awk</a></h2><div class="post-meta"><div class="post-time">2018年5月31日</div></div><div class="post-content"><p>Ill come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So lets see what these functions can do to help me improve my productivity.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><h4 id="bash_functions"><a href="#bash_functions" class="headerlink" title="bash functions"></a>bash functions</h4><p>Bash functions are scripts like <code>alias</code>, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as <code>&amp;&amp;</code> I guess:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> run &#123;</span><br><span class="line"> <span class="built_in">source</span> ~/Develop/django/bin/activate</span><br><span class="line"> ./manage.py runserver</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>I activate django virtural enviroment first and then run django serve.</p>
<p>Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> ba &#123; hgba | grep <span class="string">"<span class="variable">$1</span>"</span> &#125;</span><br><span class="line"><span class="comment"># hgba is an alias for hg branches</span></span><br></pre></td></tr></table></figure>
<p>I can just type <code>ba release</code> then get current release branch info.</p>
<hr>
<p>Append:</p>
<p>I made some updates to the <code>ba</code> function and make it auto copying the branch result to my clipboard:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">function</span> ba &#123; var=<span class="string">"<span class="variable">$(hgba | grep $1)</span>"</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> | awk -F <span class="string">':'</span> END&#123;<span class="built_in">print</span>&#125; | awk -F <span class="string">':'</span> <span class="string">'&#123;print $NF&#125;'</span> | tr -d <span class="string">'\n'</span> | pbcopy &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> &#125;</span><br></pre></td></tr></table></figure>
<h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><p><code>awk</code> is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python. </p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,130 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2018/05/31/bash-function-and-awk/" class="post-title-link">bash function and awk</a></h2><div class="post-meta"><div class="post-time">2018年5月31日</div></div><div class="post-content"><p>Ill come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So lets see what these functions can do to help me improve my productivity.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><h4 id="bash_functions"><a href="#bash_functions" class="headerlink" title="bash functions"></a>bash functions</h4><p>Bash functions are scripts like <code>alias</code>, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as <code>&amp;&amp;</code> I guess:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> run &#123;</span><br><span class="line"> <span class="built_in">source</span> ~/Develop/django/bin/activate</span><br><span class="line"> ./manage.py runserver</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>I activate django virtural enviroment first and then run django serve.</p>
<p>Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> ba &#123; hgba | grep <span class="string">"<span class="variable">$1</span>"</span> &#125;</span><br><span class="line"><span class="comment"># hgba is an alias for hg branches</span></span><br></pre></td></tr></table></figure>
<p>I can just type <code>ba release</code> then get current release branch info.</p>
<hr>
<p>Append:</p>
<p>I made some updates to the <code>ba</code> function and make it auto copying the branch result to my clipboard:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">function</span> ba &#123; var=<span class="string">"<span class="variable">$(hgba | grep $1)</span>"</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> | awk -F <span class="string">':'</span> END&#123;<span class="built_in">print</span>&#125; | awk -F <span class="string">':'</span> <span class="string">'&#123;print $NF&#125;'</span> | tr -d <span class="string">'\n'</span> | pbcopy &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> &#125;</span><br></pre></td></tr></table></figure>
<h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><p><code>awk</code> is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python. </p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,146 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/03/12/Postgresql-Partitioning/" class="post-title-link">Postgresql Partitioning</a></h2><div class="post-meta"><div class="post-time">2019年3月12日</div></div><div class="post-content"><p><code>Partitioning</code> refers to splitting what is logically one large table inot smaller physical pieces.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Currently, PostgreSQL supports partitioning via table <a href="https://www.postgresql.org/docs/9.6/ddl-inherit.html" target="_blank" rel="noopener">inheritance</a>. Each partition must be created as a child table of a single parent table. <strong>The parent table itself is normally empty</strong>; It exists just to represent the entire data set.</p>
<p>There are two forms of partitioning can be implemented in PostgreSQL:</p>
<ul>
<li><p>Range Partitioning</p>
<p> The table is partitioning into “range” defined by a key column or a set of columns, with no overlap between the ranges of values assigned to different partitions. eg. partition by date ranges or by identifiers.</p>
</li>
<li><p>List Partitioning</p>
<p> The table is partitioned by explicitly listing which key values appear in each partition.</p>
</li>
</ul>
<h3 id="Implementing_Partitioning"><a href="#Implementing_Partitioning" class="headerlink" title="Implementing Partitioning"></a>Implementing Partitioning</h3><ol>
<li><p>Create the “master” / “parent” table, from which all the partitions will inherit.</p>
<p> This table will not contain any data. Do not define any check on this table, unless you intend them to be applied equally to all partitions. There is no point in defining any indexes or unique constraints on it either.</p>
</li>
<li><p>Create “child” tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master. </p>
</li>
<li><p>Add table constraints to the partition tables to define the allowed key values in each partitions. </p>
<p> Ensure that the constraints guarantee that there is no overlap between the key values premitted in different partitions. And there is no difference in syntax between range and list partitioning. </p>
</li>
<li><p>Create indexes on column(s) for each partitions.</p>
</li>
<li><p>Optionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.</p>
</li>
<li><p>Ensure hte <a href="https://www.postgresql.org/docs/9.6/runtime-config-query.html#GUC-CONSTRAINT-EXCLUSION" target="_blank" rel="noopener">constraint_exclusion</a> configuration parameter is not disabled in <code>postgresql.conf</code>. If it is, queries will not be optimized as desired.</p>
</li>
</ol>
<h3 id="Trigger"><a href="#Trigger" class="headerlink" title="Trigger"></a>Trigger</h3><p>As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.</p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,121 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/11/14/AWS-KMS/" class="post-title-link">AWS KMS</a></h2><div class="post-meta"><div class="post-time">2019年11月14日</div></div><div class="post-content"><p>We used to keep private credentials on production servers without any protection or encryption. Well, luckily we dont have any leak but this practice is not recommended for both security and easy of use reasons.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Since AWS finally provides <a href="https://www.amazonaws.cn/kms/" target="_blank" rel="noopener">KMS(Key Management Service)</a> in our local region, we try to encrypt every private credentials by KMS and store them on S3.</p>
<p><em>TBD</em></p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,149 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/11/14/AWS-KMS/" class="post-title-link">AWS KMS</a></h2><div class="post-meta"><div class="post-time">2019年11月14日</div></div><div class="post-content"><p>We used to keep private credentials on production servers without any protection or encryption. Well, luckily we dont have any leak but this practice is not recommended for both security and easy of use reasons.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Since AWS finally provides <a href="https://www.amazonaws.cn/kms/" target="_blank" rel="noopener">KMS(Key Management Service)</a> in our local region, we try to encrypt every private credentials by KMS and store them on S3.</p>
<p><em>TBD</em></p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/03/12/Postgresql-Partitioning/" class="post-title-link">Postgresql Partitioning</a></h2><div class="post-meta"><div class="post-time">2019年3月12日</div></div><div class="post-content"><p><code>Partitioning</code> refers to splitting what is logically one large table inot smaller physical pieces.</p>
<p>Currently, PostgreSQL supports partitioning via table <a href="https://www.postgresql.org/docs/9.6/ddl-inherit.html" target="_blank" rel="noopener">inheritance</a>. Each partition must be created as a child table of a single parent table. <strong>The parent table itself is normally empty</strong>; It exists just to represent the entire data set.</p>
<p>There are two forms of partitioning can be implemented in PostgreSQL:</p>
<ul>
<li><p>Range Partitioning</p>
<p> The table is partitioning into “range” defined by a key column or a set of columns, with no overlap between the ranges of values assigned to different partitions. eg. partition by date ranges or by identifiers.</p>
</li>
<li><p>List Partitioning</p>
<p> The table is partitioned by explicitly listing which key values appear in each partition.</p>
</li>
</ul>
<h3 id="Implementing_Partitioning"><a href="#Implementing_Partitioning" class="headerlink" title="Implementing Partitioning"></a>Implementing Partitioning</h3><ol>
<li><p>Create the “master” / “parent” table, from which all the partitions will inherit.</p>
<p> This table will not contain any data. Do not define any check on this table, unless you intend them to be applied equally to all partitions. There is no point in defining any indexes or unique constraints on it either.</p>
</li>
<li><p>Create “child” tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master. </p>
</li>
<li><p>Add table constraints to the partition tables to define the allowed key values in each partitions. </p>
<p> Ensure that the constraints guarantee that there is no overlap between the key values premitted in different partitions. And there is no difference in syntax between range and list partitioning. </p>
</li>
<li><p>Create indexes on column(s) for each partitions.</p>
</li>
<li><p>Optionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.</p>
</li>
<li><p>Ensure hte <a href="https://www.postgresql.org/docs/9.6/runtime-config-query.html#GUC-CONSTRAINT-EXCLUSION" target="_blank" rel="noopener">constraint_exclusion</a> configuration parameter is not disabled in <code>postgresql.conf</code>. If it is, queries will not be optimized as desired.</p>
</li>
</ol>
<h3 id="Trigger"><a href="#Trigger" class="headerlink" title="Trigger"></a>Trigger</h3><p>As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.</p>
</div></article></li></ul></section><footer><div class="paginator"></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,118 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style>mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/18/leetcode-206/" class="post-title-link">leetcode-206</a></h2><div class="post-meta"><div class="post-time">2020年3月18日</div></div><div class="post-content"><h3 id="206-__u53CD_u8F6C_u94FE_u8868"><a href="#206-__u53CD_u8F6C_u94FE_u8868" class="headerlink" title="206. 反转链表"></a>206. 反转链表</h3><p><a href="https://leetcode-cn.com/problems/reverse-linked-list/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/18/leetcode-206/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/17/leetcode-121/" class="post-title-link">leetcode-121</a></h2><div class="post-meta"><div class="post-time">2020年3月17日</div></div><div class="post-content"><h3 id="121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A"><a href="#121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h3><p><a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/17/leetcode-121/" class="read-more">...more</a></div></article></li></ul></section><footer><div class="paginator"><a href="/archives/2020/03/" class="prev">PRVE</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script></body></html>

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,118 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style>mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/04/01/leetcode-1160/" class="post-title-link">leetcode-1160</a></h2><div class="post-meta"><div class="post-time">2020年4月1日</div></div><div class="post-content"><h3 id="1160-__u62FC_u5199_u5355_u8BCD"><a href="#1160-__u62FC_u5199_u5355_u8BCD" class="headerlink" title="1160. 拼写单词"></a>1160. 拼写单词</h3><p><a href="https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/" target="_blank" rel="noopener">题目</a></p><a href="/2020/04/01/leetcode-1160/" class="read-more">...more</a></div></article></li></ul></section><footer><div class="paginator"><a href="/archives/2020/04/" class="prev">PRVE</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,118 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style>mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/23/leetcode-169/" class="post-title-link">leetcode-169</a></h2><div class="post-meta"><div class="post-time">2020年3月23日</div></div><div class="post-content"><h3 id="169-__u591A_u6570_u5143_u7D20"><a href="#169-__u591A_u6570_u5143_u7D20" class="headerlink" title="169. 多数元素"></a>169. 多数元素</h3><p><a href="https://leetcode-cn.com/problems/majority-element/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/23/leetcode-169/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/18/leetcode-206/" class="post-title-link">leetcode-206</a></h2><div class="post-meta"><div class="post-time">2020年3月18日</div></div><div class="post-content"><h3 id="206-__u53CD_u8F6C_u94FE_u8868"><a href="#206-__u53CD_u8F6C_u94FE_u8868" class="headerlink" title="206. 反转链表"></a>206. 反转链表</h3><p><a href="https://leetcode-cn.com/problems/reverse-linked-list/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/18/leetcode-206/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/17/leetcode-121/" class="post-title-link">leetcode-121</a></h2><div class="post-meta"><div class="post-time">2020年3月17日</div></div><div class="post-content"><h3 id="121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A"><a href="#121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h3><p><a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/17/leetcode-121/" class="read-more">...more</a></div></article></li></ul></section><footer><div class="paginator"><a href="/archives/2020/page/2/" class="prev">PRVE</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script></body></html>

118
public/archives/index.html Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,236 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/23/leetcode-169/" class="post-title-link">leetcode-169</a></h2><div class="post-meta"><div class="post-time">2020年3月23日</div></div><div class="post-content"><h3 id="169-__u591A_u6570_u5143_u7D20"><a href="#169-__u591A_u6570_u5143_u7D20" class="headerlink" title="169. 多数元素"></a>169. 多数元素</h3><p><a href="https://leetcode-cn.com/problems/majority-element/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/23/leetcode-169/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/18/leetcode-206/" class="post-title-link">leetcode-206</a></h2><div class="post-meta"><div class="post-time">2020年3月18日</div></div><div class="post-content"><h3 id="206-__u53CD_u8F6C_u94FE_u8868"><a href="#206-__u53CD_u8F6C_u94FE_u8868" class="headerlink" title="206. 反转链表"></a>206. 反转链表</h3><p><a href="https://leetcode-cn.com/problems/reverse-linked-list/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/18/leetcode-206/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/17/leetcode-121/" class="post-title-link">leetcode-121</a></h2><div class="post-meta"><div class="post-time">2020年3月17日</div></div><div class="post-content"><h3 id="121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A"><a href="#121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h3><p><a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/17/leetcode-121/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/11/14/AWS-KMS/" class="post-title-link">AWS KMS</a></h2><div class="post-meta"><div class="post-time">2019年11月14日</div></div><div class="post-content"><p>We used to keep private credentials on production servers without any protection or encryption. Well, luckily we dont have any leak but this practice is not recommended for both security and easy of use reasons.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Since AWS finally provides <a href="https://www.amazonaws.cn/kms/" target="_blank" rel="noopener">KMS(Key Management Service)</a> in our local region, we try to encrypt every private credentials by KMS and store them on S3.</p>
<p><em>TBD</em></p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/03/12/Postgresql-Partitioning/" class="post-title-link">Postgresql Partitioning</a></h2><div class="post-meta"><div class="post-time">2019年3月12日</div></div><div class="post-content"><p><code>Partitioning</code> refers to splitting what is logically one large table inot smaller physical pieces.</p>
<p>Currently, PostgreSQL supports partitioning via table <a href="https://www.postgresql.org/docs/9.6/ddl-inherit.html" target="_blank" rel="noopener">inheritance</a>. Each partition must be created as a child table of a single parent table. <strong>The parent table itself is normally empty</strong>; It exists just to represent the entire data set.</p>
<p>There are two forms of partitioning can be implemented in PostgreSQL:</p>
<ul>
<li><p>Range Partitioning</p>
<p> The table is partitioning into “range” defined by a key column or a set of columns, with no overlap between the ranges of values assigned to different partitions. eg. partition by date ranges or by identifiers.</p>
</li>
<li><p>List Partitioning</p>
<p> The table is partitioned by explicitly listing which key values appear in each partition.</p>
</li>
</ul>
<h3 id="Implementing_Partitioning"><a href="#Implementing_Partitioning" class="headerlink" title="Implementing Partitioning"></a>Implementing Partitioning</h3><ol>
<li><p>Create the “master” / “parent” table, from which all the partitions will inherit.</p>
<p> This table will not contain any data. Do not define any check on this table, unless you intend them to be applied equally to all partitions. There is no point in defining any indexes or unique constraints on it either.</p>
</li>
<li><p>Create “child” tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master. </p>
</li>
<li><p>Add table constraints to the partition tables to define the allowed key values in each partitions. </p>
<p> Ensure that the constraints guarantee that there is no overlap between the key values premitted in different partitions. And there is no difference in syntax between range and list partitioning. </p>
</li>
<li><p>Create indexes on column(s) for each partitions.</p>
</li>
<li><p>Optionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.</p>
</li>
<li><p>Ensure hte <a href="https://www.postgresql.org/docs/9.6/runtime-config-query.html#GUC-CONSTRAINT-EXCLUSION" target="_blank" rel="noopener">constraint_exclusion</a> configuration parameter is not disabled in <code>postgresql.conf</code>. If it is, queries will not be optimized as desired.</p>
</li>
</ol>
<h3 id="Trigger"><a href="#Trigger" class="headerlink" title="Trigger"></a>Trigger</h3><p>As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2018/05/31/bash-function-and-awk/" class="post-title-link">bash function and awk</a></h2><div class="post-meta"><div class="post-time">2018年5月31日</div></div><div class="post-content"><p>Ill come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So lets see what these functions can do to help me improve my productivity.</p>
<h4 id="bash_functions"><a href="#bash_functions" class="headerlink" title="bash functions"></a>bash functions</h4><p>Bash functions are scripts like <code>alias</code>, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as <code>&amp;&amp;</code> I guess:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> run &#123;</span><br><span class="line"> <span class="built_in">source</span> ~/Develop/django/bin/activate</span><br><span class="line"> ./manage.py runserver</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>I activate django virtural enviroment first and then run django serve.</p>
<p>Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> ba &#123; hgba | grep <span class="string">"<span class="variable">$1</span>"</span> &#125;</span><br><span class="line"><span class="comment"># hgba is an alias for hg branches</span></span><br></pre></td></tr></table></figure>
<p>I can just type <code>ba release</code> then get current release branch info.</p>
<hr>
<p>Append:</p>
<p>I made some updates to the <code>ba</code> function and make it auto copying the branch result to my clipboard:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">function</span> ba &#123; var=<span class="string">"<span class="variable">$(hgba | grep $1)</span>"</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> | awk -F <span class="string">':'</span> END&#123;<span class="built_in">print</span>&#125; | awk -F <span class="string">':'</span> <span class="string">'&#123;print $NF&#125;'</span> | tr -d <span class="string">'\n'</span> | pbcopy &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> &#125;</span><br></pre></td></tr></table></figure>
<h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><p><code>awk</code> is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python. </p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/10/TastyPie-Note-1/" class="post-title-link">TastyPie Note 1</a></h2><div class="post-meta"><div class="post-time">2016年5月10日</div></div><div class="post-content"><h3 id="Flow_Through_The_Request/Response_Cycle"><a href="#Flow_Through_The_Request/Response_Cycle" class="headerlink" title="Flow Through The Request/Response Cycle"></a>Flow Through The Request/Response Cycle</h3><p>Tastypie can be thought of as a set of class-based view that provide the API functionality. All routing/middleware/response-handling aspectss are the same as a typical Django app. Where the differs is in the view itself.</p>
<p>Walking through what a GET request to a list endpoint looks like:</p>
<ul>
<li><p>The <code>Resource.urls</code> are checked by Djangos url resolvers.</p>
</li>
<li><p>On a match for the list view, <code>Resource.wrap_view(&#39;dispatch_list&#39;)</code> is called. <code>wrap_view</code> provides basic error handling &amp; allows for returning serilized errors.</p>
</li>
<li><p>Because dispatch_list was passed to <code>wrap_view</code>, <code>Resource.dispatch_list</code> is called next. This is a thin wrapper around <code>Resource.dispatch</code>.</p>
</li>
<li><p><code>dispatch</code> does a bunch of havy lifting. It ensures:</p>
<ul>
<li>the requested HTTP method is in <code>allowed_methos</code> (<code>method_check</code>).</li>
<li>the class has a method that can handle the request(<code>get_list</code>)</li>
<li>the user is authenticated(<code>is_authenticated</code>)</li>
<li>the user has no exceeded their throttle(<code>throttle_check</code>).</li>
</ul>
<p>At this point, <code>dispatch</code> actually calls the requested method (<code>get_list</code>).</p>
</li>
<li><p><code>get_list</code> does the actual work of API. It does:</p>
<ul>
<li>A fetch of the available objects via <code>Resource.obj_get_list</code>. In the case of <code>ModelResource</code>, this builds the ORM filters to apply (<code>ModelResource.build_filters</code>). It then gets the <code>QuerySet</code> via <code>ModelResource.get_object_list</code> (which performs <code>Resource.authorized_read_list</code> to possibly limit the set the user can work with) and applies the built filters to it.</li>
<li>It then sorts the objects based on user input (<code>ModelResource.apply_sorting</code>).</li>
<li>Then it paginates the results using the supplied <code>Paginator</code> &amp; pulls out the data to be serialized.</li>
<li>The objects in the page have <code>full_dehydrate</code> applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.</li>
<li>Finally, it calls <code>Resource.create_response</code>.</li>
</ul>
</li>
<li><p><code>create_response</code> is a shortcut method that:</p>
<ul>
<li>Determines the desired response format (<code>Resource.determine_format</code>).</li>
<li>Serializes the data given to it in the proper format.</li>
<li>Returns a Django <code>HttpResponse</code> (200 OK) with the serialized data.</li>
</ul>
</li>
<li><p>We bubble back up the call stack to <code>dispatch</code>. The last thing <code>dispatch</code> does is potentially store that a request occured for future throttling (<code>Resource.log_throttled_access</code>) then either returns the <code>HttpResponse</code> or wraps whatever data came back in a response (so Django doesnt freak out).</p>
</li>
</ul>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/04/Tastypie/" class="post-title-link">Tastypie</a></h2><div class="post-meta"><div class="post-time">2016年5月4日</div></div><div class="post-content"><h4 id="Resources_in_Tastypie"><a href="#Resources_in_Tastypie" class="headerlink" title="Resources in Tastypie"></a>Resources in Tastypie</h4><p>Resources are the heart of Tastypie. By defining a resource we can actually convert a model into an API stream. The data is automatically converted into API response.</p>
<p>Understanding the process of creating a resource.</p>
<ol>
<li>Import ModelResource from Tastypie.</li>
<li>Import models from services app</li>
<li>Create custom resource by inheriting ModelResource and link app model in inner Meta class of resource. </li>
</ol>
<p>Add API URL in the urls.py of app.</p>
<h4 id="Dehydrating_the_JSON_data"><a href="#Dehydrating_the_JSON_data" class="headerlink" title="Dehydrating the JSON data"></a>Dehydrating the JSON data</h4><p><img src="https://impythonist.files.wordpress.com/2016/04/tastypie_ill.png?w=800" alt="flow"></p>
<p>Dehydration in Tastypie means making alterations before sending data to the client. Suppose we need to send capitalized product names instead of small letters. Now we see two kinds of dehydrate methods.</p>
<h5 id="Dehydrate_field_method"><a href="#Dehydrate_field_method" class="headerlink" title="Dehydrate_field method"></a>Dehydrate_field method</h5><p>This <code>dehydrate_field</code> is uesd to modify field on the response JSON. </p>
<h5 id="Dehydrate_method"><a href="#Dehydrate_method" class="headerlink" title="Dehydrate method"></a>Dehydrate method</h5><p>Dehydrate method is useful for aadding additional fields to bundle (response data). </p>
<p>Similarly using <code>hydrate</code> method we can alter the bundle data which is generated from request at the time of PUT or POST methods.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/04/25/Django-Manager-Method/" class="post-title-link">Django Manager Method</a></h2><div class="post-meta"><div class="post-time">2016年4月25日</div></div><div class="post-content"><h4 id="Django_Manager"><a href="#Django_Manager" class="headerlink" title="Django Manager"></a>Django Manager</h4><p>Django 里会为每一个 model 生成一个 Manager默认名字为 objects一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理QuerySet 方法是对数据库操作的封装。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.db <span class="keyword">import</span> models</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> ...</span><br><span class="line"> people = models.Manager()</span><br></pre></td></tr></table></figure>
<p>上面这个 model<code>Person.objects</code>会产生一个<code>AttributeError</code>,但是<code>Person.people</code>就可以正常操作。因为默认的 manager 已经变成 peopleobjects 这个 manager 没有重新声明,不起作用。</p>
<h4 id="u81EA_u5B9A_u4E49_Manager"><a href="#u81EA_u5B9A_u4E49_Manager" class="headerlink" title="自定义 Manager"></a>自定义 Manager</h4><p>通常需要自定义 manager 的情况有两点:</p>
<ol>
<li>需要修改/扩展 Django 的 manager 方法</li>
<li>需要修改返回的 QuerySet</li>
</ol>
<h4 id="u9ED8_u8BA4_Manager"><a href="#u9ED8_u8BA4_Manager" class="headerlink" title="默认 Manager"></a>默认 Manager</h4><p>如果使用自定义的 manager 需要注意的是Django 将 model 中定义的第一个 manager 认为是默认 manager而且 Django 框架中会用到默认 manager。</p>
<p>笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager并放在第一个。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> title = models.CharField(max_length=<span class="number">100</span>)</span><br><span class="line"> author = models.CharField(max_length=<span class="number">50</span>)</span><br><span class="line"> </span><br><span class="line"> objects = models.Manager() <span class="comment"># default manager</span></span><br><span class="line"> custom_objects = CustomBOokManager() <span class="comment"># custom manager</span></span><br></pre></td></tr></table></figure>
<p><a href="http://blog.csdn.net/sicofield/article/details/49283751" target="_blank" rel="noopener">source</a></p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/16/Flask-Day-2/" class="post-title-link">Flask Day 2</a></h2><div class="post-meta"><div class="post-time">2016年2月16日</div></div><div class="post-content"><p>To handle web forms we use <a href="http://packages.python.org/Flask-WTF" target="_blank" rel="noopener">Flask-WTF </a>. So we need to write a config file (file <code>config.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">WTF_CSRF_ENABLED = <span class="keyword">True</span></span><br><span class="line">SECRET_KEY = <span class="string">'you-will-never-guess'</span></span><br></pre></td></tr></table></figure>
<p>And then you need to use this config (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line">app.config.from_object(<span class="string">'config'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Lets build a simple form (file <code>app/forms.app</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask.ext.wtf <span class="keyword">import</span> Form</span><br><span class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, BooleanField</span><br><span class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> DataRequired</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginForm</span><span class="params">(Form)</span>:</span></span><br><span class="line"> openid = StringField(<span class="string">'openid'</span>, validators=[DataRequired()])</span><br><span class="line"> remember_me = BooleanField(<span class="string">'remember_me'</span>, default=<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
<p>The <code>DataRequired()</code> is a validator that checks the field is empty or not.</p>
<p>After that, we need a HTML page to show the form (file <code>app/templates/login.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- extend from base layout --&gt;</span></span><br><span class="line">&#123;% extends "base.html" %&#125;</span><br><span class="line"></span><br><span class="line">&#123;% block content %&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Sign In<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">""</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">name</span>=<span class="string">"login"</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.hidden_tag() &#125;&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line"> Please enter your OpenID:<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.openid(size=80) &#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; form.remember_me &#125;&#125; Remember Me<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Sign In"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line">&#123;% endblock %&#125;</span><br></pre></td></tr></table></figure>
<p>The final step is to code a view function that renders the template and receiving data from form (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"><span class="keyword">from</span> .forms <span class="keyword">import</span> LoginForm</span><br><span class="line"></span><br><span class="line"><span class="comment"># index view function suppressed for brevity</span></span><br><span class="line"></span><br><span class="line">app.route(<span class="string">'/login'</span>, methods=[<span class="string">'GET'</span>, <span class="string">'POST'</span>])</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">()</span>:</span></span><br><span class="line"> form = LoginForm()</span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> flash(<span class="string">'Login requested for OpenID="%s", remember_me=%s'</span> %</span><br><span class="line"> (form.openid.data, str(form.remember_me.data)))</span><br><span class="line"> <span class="keyword">return</span> redirect(<span class="string">'/index'</span>)</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'login.html'</span>, </span><br><span class="line"> title=<span class="string">'Sign In'</span>,</span><br><span class="line"> form=form)</span><br></pre></td></tr></table></figure>
</div></article></li></ul></section><footer><div class="paginator"><a href="/archives/page/2/" class="prev">PRVE</a><a href="/archives/page/4/" class="next">NEXT</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

View File

@ -0,0 +1,147 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/15/Flask-Day-1/" class="post-title-link">Flask Day 1</a></h2><div class="post-meta"><div class="post-time">2016年2月15日</div></div><div class="post-content"><h3 id="u201CHello_World_u201D_in_Flask"><a href="#u201CHello_World_u201D_in_Flask" class="headerlink" title="“Hello World” in Flask"></a>“Hello World” in Flask</h3><p>Create a folder named <code>microblog</code> (or whatever you want). Then cd into that folder and run following prompt in terminal:</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m venv flask</span><br></pre></td></tr></table></figure>
<p>Now youll have a folder named <code>flask</code> inside <code>microblog</code>, containing a private version of Python interpreter.</p>
<p>And you should install <strong>flask</strong> and extensions by the commands below:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ flask/bin/pip install flask</span><br><span class="line">$ flask/bin/pip install flask-login</span><br><span class="line">$ flask/bin/pip install flask-openid</span><br><span class="line">$ flask/bin/pip install flask-mail</span><br><span class="line">$ flask/bin/pip install flask-sqlalchemy</span><br><span class="line">$ flask/bin/pip install sqlalchemy-migrate</span><br><span class="line">$ flask/bin/pip install flask-whooshalchemy</span><br><span class="line">$ flask/bin/pip install flask-wtf</span><br><span class="line">$ flask/bin/pip install flask-babel</span><br><span class="line">$ flask/bin/pip install guess_language</span><br><span class="line">$ flask/bin/pip install flipflop</span><br><span class="line">$ flask/bin/pip install coverage</span><br></pre></td></tr></table></figure>
<p>After that, lets create the basic structure for our application: <code>app</code> <code>app/static</code> <code>app/templates</code> <code>tmp</code>.</p>
<ol>
<li><code>app</code> — where the application package is</li>
<li><code>static</code> — stores static files like images, javascripts, and css.</li>
<li><code>templates</code> — where templates will go.</li>
</ol>
<p>Then you can start with <code>__init__.py</code> which should put into app folder (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>The views are the handlers that response to requests from web browsers or other clients. Each view function is mapped to one or more request URLs.</p>
<p>Lets see what a views function looks like (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Finally we should create a script to starts up the web server with our application(file <code>run.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!flask/bin/python</span></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line">app.run(debug=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
<p>To indicating that is an executable file you need to run this in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod a+x run.py</span><br></pre></td></tr></table></figure>
<p>Now the file structure should look like:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">microblog\</span><br><span class="line"> flask\</span><br><span class="line"> &lt;virtual environment files&gt;</span><br><span class="line"> app\</span><br><span class="line"> static\</span><br><span class="line"> templates\</span><br><span class="line"> __init__.py</span><br><span class="line"> views.py</span><br><span class="line"> tmp\</span><br><span class="line"> run.py</span><br></pre></td></tr></table></figure>
<p>Then start to write the template (file <code>app/templates/index.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#123;&#123; title &#125;&#125; - microblog<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; user.nickname &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Now lets write the view function that uses this template (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/')</span></span><br><span class="line"><span class="meta">@app.route('/index')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> user = &#123;<span class="string">'nickname'</span>: <span class="string">'ching'</span>&#125; <span class="comment"># fake user</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>,</span><br><span class="line"> title=<span class="string">'Home'</span>,</span><br><span class="line"> user=user)</span><br></pre></td></tr></table></figure>
<p><code>render_template</code> function is what we import from Flask framework to render the template. It uses <a href="http://jinja.pocoo.org/" target="_blank" rel="noopener">Jinja2</a> templating engine.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/12/first-post/" class="post-title-link">First Post</a></h2><div class="post-meta"><div class="post-time">2016年2月12日</div></div><div class="post-content"><p>This is the very first post I wrote,</p>
<p>with <a href="https://www.typora.io/" target="_blank" rel="noopener">Typora</a> &amp; <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>.</p>
</div></article></li></ul></section><footer><div class="paginator"><a href="/archives/page/3/" class="prev">PRVE</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

521
public/atom.xml Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,118 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style>mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/23/leetcode-169/" class="post-title-link">leetcode-169</a></h2><div class="post-meta"><div class="post-time">2020年3月23日</div></div><div class="post-content"><h3 id="169-__u591A_u6570_u5143_u7D20"><a href="#169-__u591A_u6570_u5143_u7D20" class="headerlink" title="169. 多数元素"></a>169. 多数元素</h3><p><a href="https://leetcode-cn.com/problems/majority-element/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/23/leetcode-169/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/18/leetcode-206/" class="post-title-link">leetcode-206</a></h2><div class="post-meta"><div class="post-time">2020年3月18日</div></div><div class="post-content"><h3 id="206-__u53CD_u8F6C_u94FE_u8868"><a href="#206-__u53CD_u8F6C_u94FE_u8868" class="headerlink" title="206. 反转链表"></a>206. 反转链表</h3><p><a href="https://leetcode-cn.com/problems/reverse-linked-list/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/18/leetcode-206/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/17/leetcode-121/" class="post-title-link">leetcode-121</a></h2><div class="post-meta"><div class="post-time">2020年3月17日</div></div><div class="post-content"><h3 id="121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A"><a href="#121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h3><p><a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/17/leetcode-121/" class="read-more">...more</a></div></article></li></ul></section><footer><div class="paginator"><a href="/categories/leetcode/page/2/" class="prev">PRVE</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script></body></html>

1
public/css/apollo.css Normal file

File diff suppressed because one or more lines are too long

BIN
public/favicon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 616 B

118
public/index.html Normal file

File diff suppressed because one or more lines are too long

BIN
public/logo.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.9 KiB

118
public/page/2/index.html Normal file

File diff suppressed because one or more lines are too long

236
public/page/3/index.html Normal file
View File

@ -0,0 +1,236 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link active">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/23/leetcode-169/" class="post-title-link">leetcode-169</a></h2><div class="post-meta"><div class="post-time">2020年3月23日</div></div><div class="post-content"><h3 id="169-__u591A_u6570_u5143_u7D20"><a href="#169-__u591A_u6570_u5143_u7D20" class="headerlink" title="169. 多数元素"></a>169. 多数元素</h3><p><a href="https://leetcode-cn.com/problems/majority-element/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/23/leetcode-169/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/18/leetcode-206/" class="post-title-link">leetcode-206</a></h2><div class="post-meta"><div class="post-time">2020年3月18日</div></div><div class="post-content"><h3 id="206-__u53CD_u8F6C_u94FE_u8868"><a href="#206-__u53CD_u8F6C_u94FE_u8868" class="headerlink" title="206. 反转链表"></a>206. 反转链表</h3><p><a href="https://leetcode-cn.com/problems/reverse-linked-list/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/18/leetcode-206/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2020/03/17/leetcode-121/" class="post-title-link">leetcode-121</a></h2><div class="post-meta"><div class="post-time">2020年3月17日</div></div><div class="post-content"><h3 id="121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A"><a href="#121-__u4E70_u5356_u80A1_u7968_u7684_u6700_u4F73_u65F6_u673A" class="headerlink" title="121. 买卖股票的最佳时机"></a>121. 买卖股票的最佳时机</h3><p><a href="https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/" target="_blank" rel="noopener">题目</a></p><a href="/2020/03/17/leetcode-121/" class="read-more">...more</a></div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/11/14/AWS-KMS/" class="post-title-link">AWS KMS</a></h2><div class="post-meta"><div class="post-time">2019年11月14日</div></div><div class="post-content"><p>We used to keep private credentials on production servers without any protection or encryption. Well, luckily we dont have any leak but this practice is not recommended for both security and easy of use reasons.</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><p>Since AWS finally provides <a href="https://www.amazonaws.cn/kms/" target="_blank" rel="noopener">KMS(Key Management Service)</a> in our local region, we try to encrypt every private credentials by KMS and store them on S3.</p>
<p><em>TBD</em></p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2019/03/12/Postgresql-Partitioning/" class="post-title-link">Postgresql Partitioning</a></h2><div class="post-meta"><div class="post-time">2019年3月12日</div></div><div class="post-content"><p><code>Partitioning</code> refers to splitting what is logically one large table inot smaller physical pieces.</p>
<p>Currently, PostgreSQL supports partitioning via table <a href="https://www.postgresql.org/docs/9.6/ddl-inherit.html" target="_blank" rel="noopener">inheritance</a>. Each partition must be created as a child table of a single parent table. <strong>The parent table itself is normally empty</strong>; It exists just to represent the entire data set.</p>
<p>There are two forms of partitioning can be implemented in PostgreSQL:</p>
<ul>
<li><p>Range Partitioning</p>
<p> The table is partitioning into “range” defined by a key column or a set of columns, with no overlap between the ranges of values assigned to different partitions. eg. partition by date ranges or by identifiers.</p>
</li>
<li><p>List Partitioning</p>
<p> The table is partitioned by explicitly listing which key values appear in each partition.</p>
</li>
</ul>
<h3 id="Implementing_Partitioning"><a href="#Implementing_Partitioning" class="headerlink" title="Implementing Partitioning"></a>Implementing Partitioning</h3><ol>
<li><p>Create the “master” / “parent” table, from which all the partitions will inherit.</p>
<p> This table will not contain any data. Do not define any check on this table, unless you intend them to be applied equally to all partitions. There is no point in defining any indexes or unique constraints on it either.</p>
</li>
<li><p>Create “child” tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master. </p>
</li>
<li><p>Add table constraints to the partition tables to define the allowed key values in each partitions. </p>
<p> Ensure that the constraints guarantee that there is no overlap between the key values premitted in different partitions. And there is no difference in syntax between range and list partitioning. </p>
</li>
<li><p>Create indexes on column(s) for each partitions.</p>
</li>
<li><p>Optionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.</p>
</li>
<li><p>Ensure hte <a href="https://www.postgresql.org/docs/9.6/runtime-config-query.html#GUC-CONSTRAINT-EXCLUSION" target="_blank" rel="noopener">constraint_exclusion</a> configuration parameter is not disabled in <code>postgresql.conf</code>. If it is, queries will not be optimized as desired.</p>
</li>
</ol>
<h3 id="Trigger"><a href="#Trigger" class="headerlink" title="Trigger"></a>Trigger</h3><p>As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2018/05/31/bash-function-and-awk/" class="post-title-link">bash function and awk</a></h2><div class="post-meta"><div class="post-time">2018年5月31日</div></div><div class="post-content"><p>Ill come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So lets see what these functions can do to help me improve my productivity.</p>
<h4 id="bash_functions"><a href="#bash_functions" class="headerlink" title="bash functions"></a>bash functions</h4><p>Bash functions are scripts like <code>alias</code>, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as <code>&amp;&amp;</code> I guess:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> run &#123;</span><br><span class="line"> <span class="built_in">source</span> ~/Develop/django/bin/activate</span><br><span class="line"> ./manage.py runserver</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>I activate django virtural enviroment first and then run django serve.</p>
<p>Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> ba &#123; hgba | grep <span class="string">"<span class="variable">$1</span>"</span> &#125;</span><br><span class="line"><span class="comment"># hgba is an alias for hg branches</span></span><br></pre></td></tr></table></figure>
<p>I can just type <code>ba release</code> then get current release branch info.</p>
<hr>
<p>Append:</p>
<p>I made some updates to the <code>ba</code> function and make it auto copying the branch result to my clipboard:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line"><span class="keyword">function</span> ba &#123; var=<span class="string">"<span class="variable">$(hgba | grep $1)</span>"</span> &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> | awk -F <span class="string">':'</span> END&#123;<span class="built_in">print</span>&#125; | awk -F <span class="string">':'</span> <span class="string">'&#123;print $NF&#125;'</span> | tr -d <span class="string">'\n'</span> | pbcopy &amp;&amp; <span class="built_in">echo</span> <span class="variable">$var</span> &#125;</span><br></pre></td></tr></table></figure>
<h4 id="awk"><a href="#awk" class="headerlink" title="awk"></a>awk</h4><p><code>awk</code> is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python. </p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/10/TastyPie-Note-1/" class="post-title-link">TastyPie Note 1</a></h2><div class="post-meta"><div class="post-time">2016年5月10日</div></div><div class="post-content"><h3 id="Flow_Through_The_Request/Response_Cycle"><a href="#Flow_Through_The_Request/Response_Cycle" class="headerlink" title="Flow Through The Request/Response Cycle"></a>Flow Through The Request/Response Cycle</h3><p>Tastypie can be thought of as a set of class-based view that provide the API functionality. All routing/middleware/response-handling aspectss are the same as a typical Django app. Where the differs is in the view itself.</p>
<p>Walking through what a GET request to a list endpoint looks like:</p>
<ul>
<li><p>The <code>Resource.urls</code> are checked by Djangos url resolvers.</p>
</li>
<li><p>On a match for the list view, <code>Resource.wrap_view(&#39;dispatch_list&#39;)</code> is called. <code>wrap_view</code> provides basic error handling &amp; allows for returning serilized errors.</p>
</li>
<li><p>Because dispatch_list was passed to <code>wrap_view</code>, <code>Resource.dispatch_list</code> is called next. This is a thin wrapper around <code>Resource.dispatch</code>.</p>
</li>
<li><p><code>dispatch</code> does a bunch of havy lifting. It ensures:</p>
<ul>
<li>the requested HTTP method is in <code>allowed_methos</code> (<code>method_check</code>).</li>
<li>the class has a method that can handle the request(<code>get_list</code>)</li>
<li>the user is authenticated(<code>is_authenticated</code>)</li>
<li>the user has no exceeded their throttle(<code>throttle_check</code>).</li>
</ul>
<p>At this point, <code>dispatch</code> actually calls the requested method (<code>get_list</code>).</p>
</li>
<li><p><code>get_list</code> does the actual work of API. It does:</p>
<ul>
<li>A fetch of the available objects via <code>Resource.obj_get_list</code>. In the case of <code>ModelResource</code>, this builds the ORM filters to apply (<code>ModelResource.build_filters</code>). It then gets the <code>QuerySet</code> via <code>ModelResource.get_object_list</code> (which performs <code>Resource.authorized_read_list</code> to possibly limit the set the user can work with) and applies the built filters to it.</li>
<li>It then sorts the objects based on user input (<code>ModelResource.apply_sorting</code>).</li>
<li>Then it paginates the results using the supplied <code>Paginator</code> &amp; pulls out the data to be serialized.</li>
<li>The objects in the page have <code>full_dehydrate</code> applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.</li>
<li>Finally, it calls <code>Resource.create_response</code>.</li>
</ul>
</li>
<li><p><code>create_response</code> is a shortcut method that:</p>
<ul>
<li>Determines the desired response format (<code>Resource.determine_format</code>).</li>
<li>Serializes the data given to it in the proper format.</li>
<li>Returns a Django <code>HttpResponse</code> (200 OK) with the serialized data.</li>
</ul>
</li>
<li><p>We bubble back up the call stack to <code>dispatch</code>. The last thing <code>dispatch</code> does is potentially store that a request occured for future throttling (<code>Resource.log_throttled_access</code>) then either returns the <code>HttpResponse</code> or wraps whatever data came back in a response (so Django doesnt freak out).</p>
</li>
</ul>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/05/04/Tastypie/" class="post-title-link">Tastypie</a></h2><div class="post-meta"><div class="post-time">2016年5月4日</div></div><div class="post-content"><h4 id="Resources_in_Tastypie"><a href="#Resources_in_Tastypie" class="headerlink" title="Resources in Tastypie"></a>Resources in Tastypie</h4><p>Resources are the heart of Tastypie. By defining a resource we can actually convert a model into an API stream. The data is automatically converted into API response.</p>
<p>Understanding the process of creating a resource.</p>
<ol>
<li>Import ModelResource from Tastypie.</li>
<li>Import models from services app</li>
<li>Create custom resource by inheriting ModelResource and link app model in inner Meta class of resource. </li>
</ol>
<p>Add API URL in the urls.py of app.</p>
<h4 id="Dehydrating_the_JSON_data"><a href="#Dehydrating_the_JSON_data" class="headerlink" title="Dehydrating the JSON data"></a>Dehydrating the JSON data</h4><p><img src="https://impythonist.files.wordpress.com/2016/04/tastypie_ill.png?w=800" alt="flow"></p>
<p>Dehydration in Tastypie means making alterations before sending data to the client. Suppose we need to send capitalized product names instead of small letters. Now we see two kinds of dehydrate methods.</p>
<h5 id="Dehydrate_field_method"><a href="#Dehydrate_field_method" class="headerlink" title="Dehydrate_field method"></a>Dehydrate_field method</h5><p>This <code>dehydrate_field</code> is uesd to modify field on the response JSON. </p>
<h5 id="Dehydrate_method"><a href="#Dehydrate_method" class="headerlink" title="Dehydrate method"></a>Dehydrate method</h5><p>Dehydrate method is useful for aadding additional fields to bundle (response data). </p>
<p>Similarly using <code>hydrate</code> method we can alter the bundle data which is generated from request at the time of PUT or POST methods.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/04/25/Django-Manager-Method/" class="post-title-link">Django Manager Method</a></h2><div class="post-meta"><div class="post-time">2016年4月25日</div></div><div class="post-content"><h4 id="Django_Manager"><a href="#Django_Manager" class="headerlink" title="Django Manager"></a>Django Manager</h4><p>Django 里会为每一个 model 生成一个 Manager默认名字为 objects一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理QuerySet 方法是对数据库操作的封装。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> django.db <span class="keyword">import</span> models</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Person</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> ...</span><br><span class="line"> people = models.Manager()</span><br></pre></td></tr></table></figure>
<p>上面这个 model<code>Person.objects</code>会产生一个<code>AttributeError</code>,但是<code>Person.people</code>就可以正常操作。因为默认的 manager 已经变成 peopleobjects 这个 manager 没有重新声明,不起作用。</p>
<h4 id="u81EA_u5B9A_u4E49_Manager"><a href="#u81EA_u5B9A_u4E49_Manager" class="headerlink" title="自定义 Manager"></a>自定义 Manager</h4><p>通常需要自定义 manager 的情况有两点:</p>
<ol>
<li>需要修改/扩展 Django 的 manager 方法</li>
<li>需要修改返回的 QuerySet</li>
</ol>
<h4 id="u9ED8_u8BA4_Manager"><a href="#u9ED8_u8BA4_Manager" class="headerlink" title="默认 Manager"></a>默认 Manager</h4><p>如果使用自定义的 manager 需要注意的是Django 将 model 中定义的第一个 manager 认为是默认 manager而且 Django 框架中会用到默认 manager。</p>
<p>笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager并放在第一个。</p>
<p>eg.</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Book</span><span class="params">(models.Model)</span>:</span></span><br><span class="line"> title = models.CharField(max_length=<span class="number">100</span>)</span><br><span class="line"> author = models.CharField(max_length=<span class="number">50</span>)</span><br><span class="line"> </span><br><span class="line"> objects = models.Manager() <span class="comment"># default manager</span></span><br><span class="line"> custom_objects = CustomBOokManager() <span class="comment"># custom manager</span></span><br></pre></td></tr></table></figure>
<p><a href="http://blog.csdn.net/sicofield/article/details/49283751" target="_blank" rel="noopener">source</a></p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/16/Flask-Day-2/" class="post-title-link">Flask Day 2</a></h2><div class="post-meta"><div class="post-time">2016年2月16日</div></div><div class="post-content"><p>To handle web forms we use <a href="http://packages.python.org/Flask-WTF" target="_blank" rel="noopener">Flask-WTF </a>. So we need to write a config file (file <code>config.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">WTF_CSRF_ENABLED = <span class="keyword">True</span></span><br><span class="line">SECRET_KEY = <span class="string">'you-will-never-guess'</span></span><br></pre></td></tr></table></figure>
<p>And then you need to use this config (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line">app.config.from_object(<span class="string">'config'</span>)</span><br><span class="line"></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Lets build a simple form (file <code>app/forms.app</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask.ext.wtf <span class="keyword">import</span> Form</span><br><span class="line"><span class="keyword">from</span> wtforms <span class="keyword">import</span> StringField, BooleanField</span><br><span class="line"><span class="keyword">from</span> wtforms.validators <span class="keyword">import</span> DataRequired</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">LoginForm</span><span class="params">(Form)</span>:</span></span><br><span class="line"> openid = StringField(<span class="string">'openid'</span>, validators=[DataRequired()])</span><br><span class="line"> remember_me = BooleanField(<span class="string">'remember_me'</span>, default=<span class="keyword">False</span>)</span><br></pre></td></tr></table></figure>
<p>The <code>DataRequired()</code> is a validator that checks the field is empty or not.</p>
<p>After that, we need a HTML page to show the form (file <code>app/templates/login.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">&lt;!-- extend from base layout --&gt;</span></span><br><span class="line">&#123;% extends "base.html" %&#125;</span><br><span class="line"></span><br><span class="line">&#123;% block content %&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Sign In<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">form</span> <span class="attr">action</span>=<span class="string">""</span> <span class="attr">method</span>=<span class="string">"post"</span> <span class="attr">name</span>=<span class="string">"login"</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.hidden_tag() &#125;&#125;</span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span></span><br><span class="line"> Please enter your OpenID:<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> &#123;&#123; form.openid(size=80) &#125;&#125;<span class="tag">&lt;<span class="name">br</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span>&#123;&#123; form.remember_me &#125;&#125; Remember Me<span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">p</span>&gt;</span><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"submit"</span> <span class="attr">value</span>=<span class="string">"Sign In"</span>&gt;</span><span class="tag">&lt;/<span class="name">p</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">form</span>&gt;</span></span><br><span class="line">&#123;% endblock %&#125;</span><br></pre></td></tr></table></figure>
<p>The final step is to code a view function that renders the template and receiving data from form (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template, flash, redirect</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"><span class="keyword">from</span> .forms <span class="keyword">import</span> LoginForm</span><br><span class="line"></span><br><span class="line"><span class="comment"># index view function suppressed for brevity</span></span><br><span class="line"></span><br><span class="line">app.route(<span class="string">'/login'</span>, methods=[<span class="string">'GET'</span>, <span class="string">'POST'</span>])</span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">login</span><span class="params">()</span>:</span></span><br><span class="line"> form = LoginForm()</span><br><span class="line"> <span class="keyword">if</span> form.validate_on_submit():</span><br><span class="line"> flash(<span class="string">'Login requested for OpenID="%s", remember_me=%s'</span> %</span><br><span class="line"> (form.openid.data, str(form.remember_me.data)))</span><br><span class="line"> <span class="keyword">return</span> redirect(<span class="string">'/index'</span>)</span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'login.html'</span>, </span><br><span class="line"> title=<span class="string">'Sign In'</span>,</span><br><span class="line"> form=form)</span><br></pre></td></tr></table></figure>
</div></article></li></ul></section><footer><div class="paginator"><a href="/page/2/" class="prev">PRVE</a><a href="/page/4/" class="next">NEXT</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

147
public/page/4/index.html Normal file
View File

@ -0,0 +1,147 @@
<!DOCTYPE html><html lang="zh-CN"><head><meta charset="utf-8"><meta name="X-UA-Compatible" content="IE=edge"><title> MarkDown</title><meta name="description" content="A Blog Powered By Hexo"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="short icon" href="/favicon.png"><link rel="stylesheet" href="/css/apollo.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:400,600" type="text/css"><style><!-- hexo-inject:begin --><!-- hexo-inject:end -->mjx-container[jax="SVG"] {
direction: ltr;
}
mjx-container[jax="SVG"] > svg {
overflow: visible;
}
mjx-container[jax="SVG"] > svg a {
fill: blue;
stroke: blue;
}
mjx-container[jax="SVG"][display="true"] {
display: block;
text-align: center;
margin: 1em 0;
}
mjx-container[jax="SVG"][justify="left"] {
text-align: left;
}
mjx-container[jax="SVG"][justify="right"] {
text-align: right;
}
g[data-mml-node="merror"] > g {
fill: red;
stroke: red;
}
g[data-mml-node="merror"] > rect[data-background] {
fill: yellow;
stroke: none;
}
g[data-mml-node="mtable"] > line[data-line] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > rect[data-frame] {
stroke-width: 70px;
fill: none;
}
g[data-mml-node="mtable"] > .mjx-dashed {
stroke-dasharray: 140;
}
g[data-mml-node="mtable"] > .mjx-dotted {
stroke-linecap: round;
stroke-dasharray: 0,140;
}
g[data-mml-node="mtable"] > svg {
overflow: visible;
}
[jax="SVG"] mjx-tool {
display: inline-block;
position: relative;
width: 0;
height: 0;
}
[jax="SVG"] mjx-tool > mjx-tip {
position: absolute;
top: 0;
left: 0;
}
mjx-tool > mjx-tip {
display: inline-block;
padding: .2em;
border: 1px solid #888;
font-size: 70%;
background-color: #F8F8F8;
color: black;
box-shadow: 2px 2px 5px #AAAAAA;
}
g[data-mml-node="maction"][data-toggle] {
cursor: pointer;
}
mjx-status {
display: block;
position: fixed;
left: 1em;
bottom: 1em;
min-width: 25%;
padding: .2em .4em;
border: 1px solid #888;
font-size: 90%;
background-color: #F8F8F8;
color: black;
}
foreignObject[data-mjx-xml] {
font-family: initial;
line-height: normal;
overflow: visible;
}
.MathJax path {
stroke-width: 3;
}
mjx-container {
overflow: auto hidden;
}
mjx-container + br {
display: none;
}
</style><!-- hexo-inject:begin --><!-- hexo-inject:end --></head><body><header><a href="/" class="logo-link"><img src="/logo.png"></a><ul class="nav nav-list"><li class="nav-list-item"><a href="/" target="_self" class="nav-list-link active">ALL</a></li><li class="nav-list-item"><a href="/categories/leetcode/" target="_self" class="nav-list-link">LEETCODE</a></li><li class="nav-list-item"><a href="https://bearmiebear.blogspot.com" target="_blank" class="nav-list-link">BEAR</a></li><li class="nav-list-item"><a href="/atom.xml" target="_self" class="nav-list-link">RSS</a></li></ul></header><section class="container"><ul class="home post-list"><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/15/Flask-Day-1/" class="post-title-link">Flask Day 1</a></h2><div class="post-meta"><div class="post-time">2016年2月15日</div></div><div class="post-content"><h3 id="u201CHello_World_u201D_in_Flask"><a href="#u201CHello_World_u201D_in_Flask" class="headerlink" title="“Hello World” in Flask"></a>“Hello World” in Flask</h3><p>Create a folder named <code>microblog</code> (or whatever you want). Then cd into that folder and run following prompt in terminal:</p>
<!-- hexo-inject:begin --><!-- hexo-inject:end --><figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ python3 -m venv flask</span><br></pre></td></tr></table></figure>
<p>Now youll have a folder named <code>flask</code> inside <code>microblog</code>, containing a private version of Python interpreter.</p>
<p>And you should install <strong>flask</strong> and extensions by the commands below:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">$ flask/bin/pip install flask</span><br><span class="line">$ flask/bin/pip install flask-login</span><br><span class="line">$ flask/bin/pip install flask-openid</span><br><span class="line">$ flask/bin/pip install flask-mail</span><br><span class="line">$ flask/bin/pip install flask-sqlalchemy</span><br><span class="line">$ flask/bin/pip install sqlalchemy-migrate</span><br><span class="line">$ flask/bin/pip install flask-whooshalchemy</span><br><span class="line">$ flask/bin/pip install flask-wtf</span><br><span class="line">$ flask/bin/pip install flask-babel</span><br><span class="line">$ flask/bin/pip install guess_language</span><br><span class="line">$ flask/bin/pip install flipflop</span><br><span class="line">$ flask/bin/pip install coverage</span><br></pre></td></tr></table></figure>
<p>After that, lets create the basic structure for our application: <code>app</code> <code>app/static</code> <code>app/templates</code> <code>tmp</code>.</p>
<ol>
<li><code>app</code> — where the application package is</li>
<li><code>static</code> — stores static files like images, javascripts, and css.</li>
<li><code>templates</code> — where templates will go.</li>
</ol>
<p>Then you can start with <code>__init__.py</code> which should put into app folder (file <code>app/__init__.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>The views are the handlers that response to requests from web browsers or other clients. Each view function is mapped to one or more request URLs.</p>
<p>Lets see what a views function looks like (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> Flask</span><br><span class="line"></span><br><span class="line">app = Flask(__name__)</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> views</span><br></pre></td></tr></table></figure>
<p>Finally we should create a script to starts up the web server with our application(file <code>run.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#!flask/bin/python</span></span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line">app.run(debug=<span class="keyword">True</span>)</span><br></pre></td></tr></table></figure>
<p>To indicating that is an executable file you need to run this in terminal:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">$ chmod a+x run.py</span><br></pre></td></tr></table></figure>
<p>Now the file structure should look like:</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">microblog\</span><br><span class="line"> flask\</span><br><span class="line"> &lt;virtual environment files&gt;</span><br><span class="line"> app\</span><br><span class="line"> static\</span><br><span class="line"> templates\</span><br><span class="line"> __init__.py</span><br><span class="line"> views.py</span><br><span class="line"> tmp\</span><br><span class="line"> run.py</span><br></pre></td></tr></table></figure>
<p>Then start to write the template (file <code>app/templates/index.html</code>):</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">html</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">title</span>&gt;</span>&#123;&#123; title &#125;&#125; - microblog<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"> <span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;<span class="name">h1</span>&gt;</span>Hello, &#123;&#123; user.nickname &#125;&#125;!<span class="tag">&lt;/<span class="name">h1</span>&gt;</span></span><br><span class="line"> <span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"> </span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Now lets write the view function that uses this template (file <code>app/views.py</code>):</p>
<figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">from</span> flask <span class="keyword">import</span> render_template</span><br><span class="line"><span class="keyword">from</span> app <span class="keyword">import</span> app</span><br><span class="line"></span><br><span class="line"><span class="meta">@app.route('/')</span></span><br><span class="line"><span class="meta">@app.route('/index')</span></span><br><span class="line"><span class="function"><span class="keyword">def</span> <span class="title">index</span><span class="params">()</span>:</span></span><br><span class="line"> user = &#123;<span class="string">'nickname'</span>: <span class="string">'ching'</span>&#125; <span class="comment"># fake user</span></span><br><span class="line"> <span class="keyword">return</span> render_template(<span class="string">'index.html'</span>,</span><br><span class="line"> title=<span class="string">'Home'</span>,</span><br><span class="line"> user=user)</span><br></pre></td></tr></table></figure>
<p><code>render_template</code> function is what we import from Flask framework to render the template. It uses <a href="http://jinja.pocoo.org/" target="_blank" rel="noopener">Jinja2</a> templating engine.</p>
</div></article></li><li class="post-list-item"><article class="post-block"><h2 class="post-title"><a href="/2016/02/12/first-post/" class="post-title-link">First Post</a></h2><div class="post-meta"><div class="post-time">2016年2月12日</div></div><div class="post-content"><p>This is the very first post I wrote,</p>
<p>with <a href="https://www.typora.io/" target="_blank" rel="noopener">Typora</a> &amp; <a href="https://hexo.io/" target="_blank" rel="noopener">Hexo</a>.</p>
</div></article></li></ul></section><footer><div class="paginator"><a href="/page/3/" class="prev">PRVE</a></div></footer><script src="https://cdn.bootcss.com/mathjax/2.5.3/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script><!-- hexo-inject:begin --><!-- hexo-inject:end --></body></html>

10
public/scss/apollo.scss Normal file
View File

@ -0,0 +1,10 @@
@charset "utf-8";
@import "_partial/normalize";
@import "_partial/base";
@import "_partial/header";
@import "_partial/home-post-list";
@import "_partial/post";
@import "_partial/footer";
@import "_partial/mq";
@import "_partial/copyright";

228
public/sitemap.xml Normal file
View File

@ -0,0 +1,228 @@
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>http://blog.tunpok.com/2020/04/21/leetcode-number-of-islands/</loc>
<lastmod>2020-04-21T04:55:46.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/16/leetcode-string-to-integer-atoi/</loc>
<lastmod>2020-04-16T11:56:40.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/16/leetcode-merge-intervals/</loc>
<lastmod>2020-04-16T11:22:48.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/16/leetcode-01-matrix/</loc>
<lastmod>2020-04-16T04:26:56.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/25/leetcode-543/</loc>
<lastmod>2020-04-14T15:29:53.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/09/leetcode-he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/</loc>
<lastmod>2020-04-14T15:29:40.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/14/leetcode-design-twitter/</loc>
<lastmod>2020-04-14T15:29:34.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/14/leetcode-add-two-numbers-ii/</loc>
<lastmod>2020-04-14T15:28:35.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/01/leetcode-1103/</loc>
<lastmod>2020-04-08T17:17:27.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/09/leetcode-the-masseuse-lcci/</loc>
<lastmod>2020-04-08T17:14:04.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/01/leetcode-compress-string-lcci/</loc>
<lastmod>2020-04-01T07:57:26.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/04/01/leetcode-1160/</loc>
<lastmod>2020-04-01T07:57:06.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/30/leetcode-1071/</loc>
<lastmod>2020-03-30T14:04:17.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/30/leetcode-999/</loc>
<lastmod>2020-03-30T13:03:41.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/29/leetcode-914/</loc>
<lastmod>2020-03-29T14:52:07.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/29/leetcode-1013/</loc>
<lastmod>2020-03-29T13:15:11.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/26/leetcode-876/</loc>
<lastmod>2020-03-26T13:19:10.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/25/leetcode-836/</loc>
<lastmod>2020-03-25T14:41:44.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/25/leetcode-409/</loc>
<lastmod>2020-03-25T13:55:54.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/23/leetcode-225/</loc>
<lastmod>2020-03-23T15:35:24.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/23/leetcode-169/</loc>
<lastmod>2020-03-23T15:13:49.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/18/leetcode-206/</loc>
<lastmod>2020-03-18T15:44:12.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2020/03/17/leetcode-121/</loc>
<lastmod>2020-03-17T10:54:03.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2019/11/14/AWS-KMS/</loc>
<lastmod>2019-11-14T08:03:24.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2018/05/31/bash-function-and-awk/</loc>
<lastmod>2019-11-14T08:01:50.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2019/03/12/Postgresql-Partitioning/</loc>
<lastmod>2019-11-14T08:01:50.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2016/04/25/Django-Manager-Method/</loc>
<lastmod>2018-05-15T14:58:35.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2016/02/15/Flask-Day-1/</loc>
<lastmod>2018-05-15T14:58:35.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2016/02/16/Flask-Day-2/</loc>
<lastmod>2018-05-15T14:58:35.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2016/05/04/Tastypie/</loc>
<lastmod>2018-05-15T14:58:35.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2016/02/12/first-post/</loc>
<lastmod>2018-05-15T14:58:35.000Z</lastmod>
</url>
<url>
<loc>http://blog.tunpok.com/2016/05/10/TastyPie-Note-1/</loc>
<lastmod>2018-05-15T14:58:35.000Z</lastmod>
</url>
</urlset>

4
scaffolds/draft.md Normal file
View File

@ -0,0 +1,4 @@
---
title: {{ title }}
tags:
---

4
scaffolds/page.md Normal file
View File

@ -0,0 +1,4 @@
---
title: {{ title }}
date: {{ date }}
---

6
scaffolds/post.md Normal file
View File

@ -0,0 +1,6 @@
---
title: {{ title }}
date: {{ date }}
tags:
categories:
---

View File

@ -0,0 +1,10 @@
{
"name" : "hexo_blog_tunpok",
"fileExtension" : ".md",
"autoUploadInsertedImageKey" : "looching-https:\/\/looching.imgur.com\/",
"autoUploadInsertedImage" : false,
"folderType" : 11,
"mediaFloder" : "media",
"orderBy" : 0,
"newlinesToBR" : 0
}

1
source/CNAME Normal file
View File

@ -0,0 +1 @@
blog.tunpok.com

View File

@ -0,0 +1,52 @@
---
title: bash function and awk
date: 2018-05-31 00:00:48
tags:
---
I'll come across many hg branch-switching or log searching tasks during my work. Using bash functions and awk greatly reduce the time I spend on dealing with these tasks. So let's see what these functions can do to help me improve my productivity.
#### bash functions
Bash functions are scripts like `alias`, but can do much a lot than aliasThe first kind of usages of function is to run several scripts continuously, the same as `&&` I guess:
```bash
function run {
source ~/Develop/django/bin/activate
./manage.py runserver
}
```
I activate django virtural enviroment first and then run django serve.
Functions, like in any other languages, can take parameters and returns a result. So I can use this ability to do a liitle more complex work:
```bash
function ba { hgba | grep "$1" }
# hgba is an alias for hg branches
```
I can just type `ba release` then get current release branch info.
-----
Append:
I made some updates to the `ba` function and make it auto copying the branch result to my clipboard:
```bash
function ba { var="$(hgba | grep $1)" && echo $var | awk -F ':' END{print} | awk -F ':' '{print $NF}' | tr -d '\n' | pbcopy && echo $var }
```
#### awk
`awk` is a command I just know recently. I need to process a bunch of logs and analyze them. What I used to do is download the logs, grep things I want into a txt file and then process the txt file with python.

View File

@ -0,0 +1,14 @@
---
title: AWS KMS
date: 2019-11-14 15:19:05
tags:
---
We used to keep private credentials on production servers without any protection or encryption. Well, luckily we don't have any leak but this practice is not recommended for both security and easy of use reasons.
Since AWS finally provides [KMS(Key Management Service)][1] in our local region, we try to encrypt every private credentials by KMS and store them on S3.
*TBD*
[1]: https://www.amazonaws.cn/kms/

View File

@ -0,0 +1,59 @@
---
title: leetcode-121
date: 2020-03-17 18:06:45
tags:
categories: leetcode
---
### 121. 买卖股票的最佳时机
[题目](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)
<!--more-->
原始答案:
```python
class Solution:
def maxProfit(self, prices) -> int:
profit = 0
if not prices:
return profit
min_buyin = prices[0]
max_sellout = prices[0]
l = len(prices)
i = 0
for buyin in prices:
if i == l:
return profit
if min_buyin <= buyin:
max_sellout = max(prices[i:])
p = max_sellout - min_buyin
if p > profit:
profit = p
if buyin < min_buyin:
min_buyin = buyin
i += 1
return profit
#1880 ms 14.4 MB
```
主要思路是找到波谷如果当前价格比前一天要低则还是在去往波谷的路上当价格比前一天高或相同时则到达了一个波谷计算波谷和之后的波峰的差就是这一段的利润。将从头至尾过一次就能找到所有波谷和其后波峰的差返回最大的即可。但是这个明显地在重复max时间复杂度是O(n^2),看起来就很傻逼。
仔细想想其实并不需要直接找出波谷后的波峰只要在for loop时保持波谷为最低的那个就能算出每一个后续与波谷的差找最大差即可。改了下代码变成这样
```python
class Solution:
def maxProfit(self, prices) -> int:
profit = 0
if not prices:
return profit
min_buyin = prices[0]
for i in range(1, len(prices)):
min_buyin = min(min_buyin, prices[i-1])
profit = max(prices[i] - min_buyin, profit)
return profit
#44 ms 14.4 MB
```

View File

@ -0,0 +1,88 @@
---
title: leetcode-206
date: 2020-03-18 23:33:03
tags:
categories: leetcode
---
### 206. 反转链表
[题目](https://leetcode-cn.com/problems/reverse-linked-list/)
<!--more-->
最简单的思路是遍历链表一个列表去做存储,通过倒序读取列表的同时改写链表。
```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def reverseList(self, head):
if not head or not head.next:
return head
nl = []
while head.next:
nl.append(head)
head = head.next
nl.append(head)
l = len(nl)
for x in range(l):
if x == 0:
nl[x].next = None
continue
nl[x].next = nl[x-1]
if x == (l - 1):
return nl[x]
```
仔细想想自己又傻逼了,何必要遍历两次呢,在第一遍遍历的同时就能操作了:
```python
class Solution:
def reverseList(self, head):
if not head or not head.next:
return head
prev_node = None
next_node = head.next
while head:
next_node = head.next
head.next = prev_node
prev_node = head
head = next_node
return prev_node
```
然后是递归的做法,主要思路是一直进到最深一层--也就是链表的最后一个--的时候开始返回,同时修改那一层的两个 node。一开始踩了一个坑是返回了每一个node结果最后回到第一层的时候得到的是链表的末端其实只需要修改链表并不需要返回 node所以一开始到达链表末端的时候直接返回那一个node就可以了。
```python
class Solution:
def reverseList(self, head):
if not head:
return head
if head.next:
ss = Solution()
last = ss.reverseList(head.next)
head.next.next = head
head.next = None
return last
return head
```
一开始是用list来打草稿不过想明白递归之后就大同小异了
```python
def a(l:list)->list:
k=[l[0]]
if l[1:]:
b=a(l[1:])
b.extend(k)
else:
return [l[0]]
return b
```

View File

@ -0,0 +1,62 @@
---
title: leetcode-169
date: 2020-03-23 23:12:57
tags:
categories: leetcode
---
### 169. 多数元素
[题目](https://leetcode-cn.com/problems/majority-element/)
<!--more-->
一开始的思路是遍历一遍整个列表,用一个字典去记录每个元素出现的次数,当次数大于 $\cfrac{n}{2}$ 时就可以得出结果。
```python
class Solution:
def majorityElement(self, nums) -> int:
d = {}
l = len(nums)
for n in nums:
if not n in d:
d[n] = 0
d[n] = d[n] + 1
if d[n] > l/2:
return n
# 64 ms 15.1 MB
```
Python 也有专门计数的库,写起来更简单一点:
```python
class Solution:
def majorityElement(self, nums):
counts = collections.Counter(nums)
return max(counts.keys(), key=counts.get)
# 44 ms 15.1 MB
```
由于要找的数出现次数大于 $\cfrac{n}{2}$,脑子里掠过一下[蒙特卡罗算法](https://zh.wikipedia.org/wiki/%E8%92%99%E5%9C%B0%E5%8D%A1%E7%BE%85%E6%96%B9%E6%B3%95),后来在官方解答中也看到类似的思路了:
```python
class Solution:
def majorityElement(self, nums):
majority_count = len(nums)//2
while True:
candidate = random.choice(nums)
if sum(1 for elem in nums if elem == candidate) > majority_count:
return candidate
#作者LeetCode-Solution
#链接https://leetcode-cn.com/problems/majority-element/solution/duo-shu-yuan-su-by-leetcode-solution/
#来源力扣LeetCode
#著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
```

View File

@ -0,0 +1,69 @@
---
title: leetcode-225
date: 2020-03-23 23:35:05
tags:
categories: leetcode
---
### 225. 用队列实现栈
[题目](https://leetcode-cn.com/problems/implement-stack-using-queues/)
<!--more-->
注意栈是 FILO(First In Last Out)Python 的 list 是 FIFO(First In First Out)。
```python
class MyStack:
def __init__(self):
"""
Initialize your data structure here.
"""
self.data = []
def push(self, x: int) -> None:
"""
Push element x onto stack.
"""
self.data.append(x)
return
def pop(self) -> int:
"""
Removes the element on top of the stack and returns that element.
"""
return self.data.pop(-1)
def top(self) -> int:
"""
Get the top element.
"""
return self.data[-1]
def empty(self) -> bool:
"""
Returns whether the stack is empty.
"""
return not bool(self.data)
# Your MyStack object will be instantiated and called as such:
# obj = MyStack()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.top()
# param_4 = obj.empty()
#24 ms 13.5 MB
```

View File

@ -0,0 +1,49 @@
---
title: leetcode-409
date: 2020-03-25 21:55:38
tags:
categories: leetcode
---
### 409. 最长回文串
[题目](https://leetcode-cn.com/problems/longest-palindrome/)
<!--more-->
一开始理解错题目了,以为是寻找字符串中的最长回文串,结果是构造。但是原理基本一样,由于回文中心对称,所以是由多个偶数个相同字母和至多一个奇数个相同字母组成。
这样只要数给出的字符串中有几个偶数个相同字母和几个奇数个相同字母就可以了。奇数个相同字母可以减少一个当偶数个用,最后再加回去一个。
```python
class Solution:
def longestPalindrome(self, s: str) -> int:
d = {}
for l in s:
if not l in d:
d[l] = 0
d[l] += 1
i = 0
odd = False
for k, v in d.items():
if v % 2:
i += (v-1)
odd = True
else:
i += v
if odd:
i += 1
return i
#40 ms 13.6 MB
```

View File

@ -0,0 +1,112 @@
---
title: leetcode-543
date: 2020-03-25 19:13:52
tags:
categories: leetcode
---
### 543. 二叉树的直径
[题目](https://leetcode-cn.com/problems/diameter-of-binary-tree/)
<!--more-->
这题做出来了但是没有通过运行时间的测试,主要还是没想明白二叉树的直径到底是什么东西,用了个蠢办法。
```python
# Definition for a binary tree node.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
class Solution:
def diameterOfBinaryTree(self, root: TreeNode) -> int:
if not root:
return 0
last_node = -1
routes = []
start = root
node_stack = [root]
while (start.left or start.right or node_stack):
if start != node_stack[-1]:
node_stack.append(start)
if last_node == start.right:
node_stack = node_stack[:-1]
if not node_stack:
break
last_node = start
start = node_stack[-1]
continue
if start.left and last_node != start.left:
start = start.left
last_node = start
continue
if start.right:
start = start.right
last_node = start
continue
routes.append(node_stack)
node_stack = node_stack[:-1]
if not node_stack:
break
last_node = start
start = node_stack[-1]
max_l = 0
for route in routes:
for route_ in routes:
intersection = 0
if route != route_:
intersection = len(set(route).intersection(set(route_)))
if intersection:
intersection -= 1
max_l = max(max_l, len(set(route)| set(route_)) - intersection)
return max_l - 1
```
L43 之前做的是以深度优先的方式遍历一遍树,得出每个点的路径。后面的是将所有路径组合在一起得出任意两个点间的路径,算出最大长度。
其实以某个点为根节点的树的直径,就是某个节点的**左子树的深度和右子树的深度的和**,用递归来处理这个会比较容易理解
```python
class Solution(object):
def diameterOfBinaryTree(self, root):
self.ans = 1
def depth(node):
# 访问到空节点了返回0
if not node: return 0
# 左儿子为根的子树的深度
L = depth(node.left)
# 右儿子为根的子树的深度
R = depth(node.right)
# 计算d_node即L+R+1 并更新ans
self.ans = max(self.ans, L+R+1)
# 返回该节点为根的子树的深度
return max(L, R) + 1
depth(root)
return self.ans - 1
作者LeetCode-Solution
链接https://leetcode-cn.com/problems/diameter-of-binary-tree/solution/er-cha-shu-de-zhi-jing-by-leetcode-solution/
来源力扣LeetCode
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
```

View File

@ -0,0 +1,33 @@
---
title: leetcode-836
date: 2020-03-25 22:41:25
tags:
categories: leetcode
---
### 836. 矩形重叠
[题目](https://leetcode-cn.com/problems/rectangle-overlap/)
<!--more-->
看两个矩形有没有重叠,就看两个矩形在坐标轴上的投影有没有重叠。
```python
class Solution:
def isRectangleOverlap(self, rec1, rec2) -> bool:
return ((min(rec1[2], rec2[2]) > max(rec1[0], rec2[0]))
and (min(rec1[3], rec2[3]) > max(rec1[1], rec2[1])))
s = Solution()
s.isRectangleOverlap(rec1 = [0,0,2,2], rec2 = [1,1,3,3])
#40 ms 13.7 MB
```

View File

@ -0,0 +1,67 @@
---
title: leetcode-876
date: 2020-03-26 21:18:41
tags:
categories: leetcode
---
### 876. 链表的中间结点
[题目](https://leetcode-cn.com/problems/middle-of-the-linked-list/)
<!--more-->
思路是遍历一遍得到整个链表,讲每个 node 放进一个 list就可以通过下标得到中间的。
```python
# Definition for singly-linked list.
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
l = []
n = head
while n.next:
l.append(n)
n = n.next
l.append(n)
return l[len(l)//2]
#44 ms 13.7 MB
```
看官方解答,还有一个骚操作,通过两个速度不一样的指针,一个一次走一步,一个两次走一步,快的走到底时,慢的就在中间了。
```python
class Solution:
def middleNode(self, head: ListNode) -> ListNode:
slow = fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
作者LeetCode-Solution
链接https://leetcode-cn.com/problems/middle-of-the-linked-list/solution/lian-biao-de-zhong-jian-jie-dian-by-leetcode-solut/
来源力扣LeetCode
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
```

View File

@ -0,0 +1,45 @@
---
title: leetcode-1013
date: 2020-03-29 21:09:22
tags:
categories: leetcode
---
### 1013. 将数组分成和相等的三个部分
[题目](https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum/)
<!--more-->
因为是整数数组如果能均分成三份则数组和肯定是3的倍数。然后遍历数组逐端求和使得和为 sum(A)/3。
```python
class Solution:
def canThreePartsEqualSum(self, A) -> bool:
if not A:
return False
sa = sum(A)
if sa % 3:
return False
s = sa // 3
s1 = 0
s2 = 0
for i in range(len(A)):
s1 += A[i]
if s1 == s and (i+1) < len(A):
for j in range(len(A[i+1:])):
s2 += A[i+1+j]
if s2 == s and j+1 < len(A[i+1:]) and sum(A[i+j+2:])== s:
return True
return False
#60 ms 18.7 MB
```

View File

@ -0,0 +1,51 @@
---
title: leetcode-914
date: 2020-03-29 22:41:09
tags:
categories: leetcode
---
### 914. 卡牌分组
[题目](https://leetcode-cn.com/problems/x-of-a-kind-in-a-deck-of-cards/)
<!--more-->
将大牌堆分成多个牌数量相等的小牌堆就是求每张牌数量的公约数。先遍历一遍得到每张牌的数量然后找出比2大的公约数即可。
```python
class Solution:
def hasGroupsSizeX(self, deck) -> bool:
dc = {}
max_d = 0
for d in deck:
if d not in dc:
dc[d] = 0
dc[d] += 1
if max_d < d:
max_d = d
if max_d < dc[d]:
max_d = dc[d]
has_x = True
if max_d == 1:
max_d = 2
for i in range(2, max_d + 1):
has_x = True
for k,v in dc.items():
if v % i:
has_x = False
break
if has_x and i >= 2:
return True
return False
#56 ms 13.8 MB
```

View File

@ -0,0 +1,59 @@
---
title: leetcode-1071
date: 2020-03-30 22:03:01
tags:
categories: leetcode
---
### 1071. 字符串的最大公因子
[题目](https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/)
<!--more-->
如果存在这样字符串,那它最大的长度就是这两个字符串长度的最大公约数。
```python
class Solution:
def gcdOfStrings(self, str1: str, str2: str) -> str:
if str1[0] != str2[0]:
return ''
a = len(str1)
b = len(str2)
print(a, b)
if a < b:
str1, str2 = str2, str1
a = len(str1)
b = len(str2)
if not a%b:
for x in range(0, a//b):
if str1[x*b:(x+1)*b] != str2:
return ''
return str2
else:
for x in range(b, 0, -1):
print(x)
if x==b or b%x or a%x:
continue
for y in range(0, b//x):
if str2[y*x:(y+1)*x] != str2[b-x:b]:
return ''
for y in range(0, a//x):
if str1[y*x:(y+1)*x] != str2[0:x]:
return ''
return str2[0:x]
# 44 ms 13.9 MB
```
官方解答中还给了一种巧妙的解法,如果 str1 + str2 == str2 + str1 的话,[可以证明](https://leetcode-cn.com/problems/greatest-common-divisor-of-strings/solution/zi-fu-chuan-de-zui-da-gong-yin-zi-by-leetcode-solu/)必定存在这样一个字符串,其长度为两个字符串长度的最大公约数。

View File

@ -0,0 +1,71 @@
---
title: leetcode-999
date: 2020-03-30 21:03:25
tags:
categories: leetcode
---
### 999. 可以被一步捕获的棋子数
[题目](https://leetcode-cn.com/problems/available-captures-for-rook/)
<!--more-->
遍历一遍找到车的坐标,然后按上下左右四个方向循环一下看碰到的第一个棋子是什么。
```python
class Solution:
def numRookCaptures(self, board) -> int:
i = j = 0
for row in board:
if 'R' in row:
break
i += 1
j = row.index('R')
count = 0
# right
for x in range(j + 1, 8):
if row[x] == 'p':
count += 1
break
if row[x] == 'B':
break
# left
for x in range(j, 0, -1):
if row[x] == 'p':
count += 1
break
if row[x] == 'B':
break
# up
for x in range(i, 0, -1):
if board[x][j] == 'p':
count += 1
break
if board[x][j] == 'B':
break
# down
for x in range(i+1, 8):
if board[x][j] == 'p':
count += 1
break
if board[x][j] == 'B':
break
return count
#36 ms 13.6 MB
```
问题不难,官方解答中给了一个方向数组的概念,上下左右是 (0, 1) (0, -1) (-1, 0) (1, 0),有点像向量的意思。走的路线等于方向数组乘以步数。

View File

@ -0,0 +1,55 @@
---
title: leetcode-1103
date: 2020-04-01 11:22:20
tags:
categories: leetcode
mathjax: true
---
### 1103. 分糖果 II
[题目](https://leetcode-cn.com/problems/distribute-candies-to-people/)
<!--more-->
小学奥数题。主要思路就是等差数列求和 $\cfrac{(首项 + 末项)×项数}{2}$ 。可以用公式把每一个位置获得的总糖果数表示出来。我的方法稍微蠢了点,算了每一轮的总糖果数,其实可以直接求总共发了多少次糖果,除以每轮的人数就可以得出发了多少轮。
```python
class Solution:
def distributeCandies(self, candies: int, num_people: int):
total = 0
i = 0
# import ipdb; ipdb.set_trace()
while total <= candies:
t = (num_people*i)*num_people + int((1+num_people)*num_people/2)
if total + t <= candies:
total += t
i += 1
else:
break
remaining = candies - total
print(total, remaining, i)
l = []
for n in range(1, num_people+1):
if not total:
current_candy = n
else:
current_candy = n+i*num_people
n_count = int((0+(i-1))*(i)/2)
print(current_candy, n_count)
if remaining >= current_candy:
l.append(n_count*num_people + n*i + current_candy)
remaining -= current_candy
else:
l.append(n_count*num_people + n*i + remaining)
remaining = 0
return l
# 28 ms 13.7 MB,
```

View File

@ -0,0 +1,38 @@
---
title: leetcode-1160
date: 2020-04-01 00:18:48
tags:
categories: leetcode
---
### 1160. 拼写单词
[题目](https://leetcode-cn.com/problems/find-words-that-can-be-formed-by-characters/)
<!--more-->
利用列表 remove 方法,检查 chars 中是否有足够的字母拼写 word
```python
class Solution:
def countCharacters(self, words, chars: str) -> int:
words_ = ''
for w in words:
lchars = list(chars)
try:
for l in w:
lchars.remove(l)
except:
continue
words_ += w
return len(words_)
# 152 ms 14.1 MB
```

View File

@ -0,0 +1,41 @@
---
title: leetcode-compress-string-lcci
date: 2020-04-01 15:51:22
tags:
categories: leetcode
---
### 面试题 01.06. 字符串压缩
[题目](https://leetcode-cn.com/problems/compress-string-lcci/)
<!--more-->
遍历一遍字符串,遇到跟上一个字符不同的字符时记录上一个字符的重复长度。
```python
class Solution:
def compressString(self, S: str) -> str:
if not S:
return S
c = ''
prev = S[0]
p_len = 1
for w in S[1:]:
if w != prev:
c += '%s%s' % (prev, p_len)
prev = w
p_len = 1
else:
p_len += 1
c += '%s%s' % (prev, p_len)
if len(S) > len(c):
return c
return S
# 52 ms 13.8 MB
```

View File

@ -0,0 +1,42 @@
---
title: leetcode-he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof
date: 2020-04-09 22:14:56
tags:
categories: leetcode
mathjax: true
---
### 面试题57 - II. 和为s的连续正数序列
[题目](https://leetcode-cn.com/problems/he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/)
<!--more-->
又是小学奥数。由等差数列求和公式$\cfrac{(首项 + 末项)×项数}{2}$ 可知,当首项为 1 的时候项数最多,又由于是连续正整数,$n^2 < (1+n)×n < (n+1)^2 $,那最大的 $n$ 就不大于 $\sqrt{2×target} + 1$。
由小到大遍历 $n$,可以求得首项。
```python
import math
class Solution:
def findContinuousSequence(self, target: int):
n = int(math.sqrt(2 * target) + 1)
if n < 2:
return []
sum_list = []
a = 0
for i in range(2, n+1):
a = ((2 * target) / i + 1 - i) / 2
if a and not a % 1:
a = int(a)
s_ = []
for j in range(0, i):
s_.append(a + j)
sum_list.append(s_)
return sorted(sum_list)
# 60 ms 13.7 MB
```

View File

@ -0,0 +1,47 @@
---
title: leetcode-the-masseuse-lcci
date: 2020-04-09 00:35:26
tags:
categories: leetcode
mathjax: true
---
### 面试题 17.16. 按摩师
[题目](https://leetcode-cn.com/problems/the-masseuse-lcci/)
<!--more-->
一开始以为是用递归,想了半天没想出来,偷看了一下答案。答案的思路跟递归类似,假设在当前 $i$ 时刻,$dp[i][0]$ 为当前预约不接的情况下最长预约时间,$dp[i][1]$ 则为接受当前预约的最长预约时间。
那很显然,由于不能接受相邻两个预约,$dp[i][1] = dp[i-1][0] + nums_i$
不接受当前预约的话,上一个预约接不接受都可以,$dp[i][0] = max(dp[i-1][0], dp[i-1][1])$
最后只要比较两种情况即可 $max(dp[i][0], dp[i][1])$
```python
class Solution:
def massage(self, nums) -> int:
if not nums:
return 0
n = len(nums)
not_choose = 0
choose = 0
for n in nums:
not_choose, choose = max(not_choose, choose), not_choose+n
return max(not_choose, choose)
# 52 ms 13.6 MB
```
这种问题原来有个名字叫[动态规划](https://zh.wikipedia.org/wiki/动态规划),上面推导的方程叫[状态转移方程](https://baike.baidu.com/item/状态转移方程),可以找找资料来看一下。

View File

@ -0,0 +1,115 @@
---
title: leetcode-add-two-numbers-ii
date: 2020-04-14 23:22:39
tags:
categories: leetcode
---
### 445. 两数相加 II
[题目](https://leetcode-cn.com/problems/add-two-numbers-ii/)
<!--more-->
看到顺序的链表就想到用倒序链表的方法做,折腾了半天
```python
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
def _reverse(l):
if l.next:
last = _reverse(l.next)
l.next.next = l
l.next = None
return last
return l
l1e = _reverse(l1)
l2e = _reverse(l2)
new_l = ListNode(0)
head = new_l
c = 0
import ipdb; ipdb.set_trace()
while l1e and l2e:
new_val = l1e.val + l2e.val
if c==1:
new_val += 1
c = 0
if new_val >= 10:
new_val -= 10
c = 1
new_l.val = new_val
next_n = None
if l1e.next and l2e.next or c:
next_n = ListNode(c)
new_l.next = next_n
new_l = next_n
l1e = l1e.next
l2e = l2e.next
if l2e:
l1e = l2e
if not l1e and c:
l1e = ListNode(0)
while l1e:
new_l.val = l1e.val
new_l.val += c
c = 0
if new_l.val >= 10:
c = 1
new_l.val -= 10
l1e = l1e.next
if l1e:
new_l.next = ListNode(0)
new_l = new_l.next
else:
new_l.next = ListNode(1)
return _reverse(head)
# 84 ms 13.9 MB
```
最后面各种进位的处理应该还可以更清晰优雅一些,但是懒得搞了,感觉很蠢。翻了答案看到了小 tips需要倒序处理的情况可以用栈。
```python
class Solution:
def addTwoNumbers(self, l1: ListNode, l2: ListNode) -> ListNode:
s1, s2 = [], []
while l1:
s1.append(l1.val)
l1 = l1.next
while l2:
s2.append(l2.val)
l2 = l2.next
ans = None
carry = 0
while s1 or s2 or carry != 0:
a = 0 if not s1 else s1.pop()
b = 0 if not s2 else s2.pop()
cur = a + b + carry
carry = cur // 10
cur %= 10
curnode = ListNode(cur)
curnode.next = ans
ans = curnode
return ans
作者LeetCode-Solution
链接https://leetcode-cn.com/problems/add-two-numbers-ii/solution/liang-shu-xiang-jia-ii-by-leetcode-solution/
来源力扣LeetCode
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
```
不过就执行效率来看差不多。

View File

@ -0,0 +1,83 @@
---
title: leetcode-design-twitter
date: 2020-04-14 16:11:41
tags:
categories: leetcode
---
### 355. 设计推特
[题目](https://leetcode-cn.com/problems/design-twitter/)
<!--more-->
做出来倒是很简单,由于没有并发和特别的条件,测试数据量也不大。一开始搞错了,以为传入的 `twitterId` 就是自增的 id结果其实是每条推的内容所以增加了一个计数器去标记 id。
主要的考点应该是 `多路归并` 这个东西。我用的是排序,在数据量大的时候应该会有些问题。
```python
class Twitter:
def __init__(self):
"""
Initialize your data structure here.
"""
self.tweets = {}
self.followers = {}
self._tid = 0
def postTweet(self, userId: int, tweetId: int) -> None:
"""
Compose a new tweet.
"""
if not self.tweets.get(userId):
self.tweets[userId] = []
self.tweets[userId].append((self._tid, tweetId))
self._tid += 1
def getNewsFeed(self, userId: int) :
"""
Retrieve the 10 most recent tweet ids in the user's news feed. Each item in the news feed must be posted by users who the user followed or by the user herself. Tweets must be ordered from most recent to least recent.
"""
foers = self.followers.get(userId, set())
foers = foers.union((userId,))
tweets = []
for fo in foers:
tweets.extend(self.tweets.get(fo, [])[-10:])
return [tw[1] for tw in sorted(tweets, reverse=True)[:10]]
def follow(self, followerId: int, followeeId: int) -> None:
"""
Follower follows a followee. If the operation is invalid, it should be a no-op.
"""
if not self.followers.get(followerId):
self.followers[followerId] = set()
self.followers[followerId].add(followeeId)
def unfollow(self, followerId: int, followeeId: int) -> None:
"""
Follower unfollows a followee. If the operation is invalid, it should be a no-op.
"""
if not self.followers.get(followerId):
self.followers[followerId] = set()
if followeeId in self.followers[followerId]:
self.followers[followerId].remove(followeeId)
#100 ms 19.2 MB
# Your Twitter object will be instantiated and called as such:
# obj = Twitter()
# obj.postTweet(userId,tweetId)
# param_2 = obj.getNewsFeed(userId)
# obj.follow(followerId,followeeId)
# obj.unfollow(followerId,followeeId)
```

Some files were not shown because too many files have changed in this diff Show More