{"meta":{"version":1,"warehouse":"2.2.0"},"models":{"Asset":[{"_id":"source/CNAME","path":"CNAME","modified":0,"renderable":0},{"_id":"themes/apollo/source/favicon.png","path":"favicon.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/logo.png","path":"logo.png","modified":0,"renderable":1},{"_id":"themes/apollo/source/scss/apollo.scss","path":"scss/apollo.scss","modified":0,"renderable":1},{"_id":"themes/apollo/source/css/apollo.css","path":"css/apollo.css","modified":0,"renderable":1}],"Cache":[{"_id":"source/CNAME","hash":"c53a99d8555292c2cc8dd02c564b9d5fdfed89cb","modified":1526396315000},{"_id":"themes/apollo/.DS_Store","hash":"695101ad11d1cb210f2667e7bdcdccd7e40391de","modified":1586364749000},{"_id":"themes/apollo/.gitignore","hash":"a006beea0877a0aa3610ee00e73f62cb1d45125b","modified":1526396315000},{"_id":"themes/apollo/README.md","hash":"a6930c691c69ed78584022fbcd0f4245587d09f5","modified":1526396315000},{"_id":"themes/apollo/LICENSE","hash":"6e31ac9076bfc8f09ae47977419eee4edfb63e5b","modified":1526396315000},{"_id":"themes/apollo/_config.yml","hash":"0564002d6a750856deec82fb89f6b2cb41d0a294","modified":1587446398000},{"_id":"themes/apollo/gulpfile.js","hash":"857a026b6643a2cd52c65d4ae0dc7fe9618206ee","modified":1526396315000},{"_id":"themes/apollo/package.json","hash":"9426138c09ebb95969021d951590c0c54b187a43","modified":1526396315000},{"_id":"source/.MWebMetaData/setting.json","hash":"63037ec4e43ae67cbe98b4f81451eef32dccf232","modified":1573529113000},{"_id":"source/_posts/2020-03-17-leetcode-121.md","hash":"62aa9ec2e2aa784728c189d4d7145ebab1380ae3","modified":1584442443000},{"_id":"source/_posts/2018-05-31-bash-function-and-awk.md","hash":"e9e89d931fd9652c4fc39ac49db953d15e55c19c","modified":1573718510000},{"_id":"source/_posts/2019-11-14-AWS-KMS.md","hash":"519de77de3ffdcc67169130e36f8c53e20f3b2b6","modified":1573718604000},{"_id":"source/_posts/2020-03-18-leetcode-206.md","hash":"c80133bf68fc993ddc24517da4bc226764ae2a40","modified":1584546252000},{"_id":"source/_posts/2020-03-23-leetcode-225.md","hash":"aff4492c0c07d0d31b0313b5ef9378279233141b","modified":1584977724000},{"_id":"source/_posts/2020-03-23-leetcode-169.md","hash":"f193c41627501ed7235459d5ad40fd3d6ebf0a71","modified":1584976429000},{"_id":"source/_posts/2020-03-25-leetcode-409.md","hash":"f0e210e4da442e66e4436f7055f673c4655907f0","modified":1585144554000},{"_id":"source/_posts/2020-03-25-leetcode-543.md","hash":"a6e406e0bca5aa834c99625f74ee38ae87324b7f","modified":1586878193000},{"_id":"source/_posts/2020-03-25-leetcode-836.md","hash":"cc9e68c7e2ea013086e1d6ae0c2b212c60cac573","modified":1585147304000},{"_id":"source/_posts/2020-03-29-leetcode-1013.md","hash":"4937a632273d945e4f258ef96d6ca8c2d8fe9f5b","modified":1585487711000},{"_id":"source/_posts/2020-03-26-leetcode-876.md","hash":"4905be27ad818ebc81d3938227d31d760c5631c4","modified":1585228750000},{"_id":"source/_posts/2020-03-29-leetcode-914.md","hash":"a0631cf342a08f5ff9ad4d0ca2bf8372dca60ecc","modified":1585493527000},{"_id":"source/_posts/2020-03-30-leetcode-1071.md","hash":"affa6c07b877b5fa46873eca5ca300db4f24efb6","modified":1585577057000},{"_id":"source/_posts/2020-03-30-leetcode-999.md","hash":"41bd6e2ce8483d5d158b88d28ee63ec1551ca10a","modified":1585573421000},{"_id":"source/_posts/2020-04-01-leetcode-1103.md","hash":"c76f58bceaa6aafb010e7163f3ef893573c2a45f","modified":1586366247000},{"_id":"source/_posts/2020-04-01-leetcode-1160.md","hash":"bb2daed56d6bfb2cd930afb2e3c9b0ebb6e597bd","modified":1585727826000},{"_id":"source/_posts/2020-04-01-leetcode-compress-string-lcci.md","hash":"254b0bffa89a3d5711f8b3f45f1d05f23f27fac9","modified":1585727846000},{"_id":"source/_posts/2020-04-09-leetcode-he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof.md","hash":"d1801e8c1bfff064a59cd43bc800226a193ef6b0","modified":1586878180000},{"_id":"source/_posts/2020-04-14-leetcode-add-two-numbers-ii.md","hash":"ebed1ea3f1229f6109e812cbb0f7e203f7312e0b","modified":1586878115000},{"_id":"source/_posts/2020-04-09-leetcode-the-masseuse-lcci.md","hash":"914948de11976b4d0db287c5beb1f4e40b71f7a7","modified":1586366044000},{"_id":"source/_posts/2020-04-16-leetcode-01-matrix.md","hash":"e29a270941ba47ad981089856d75640cc468f956","modified":1587011216000},{"_id":"source/_posts/2020-04-14-leetcode-design-twitter.md","hash":"f5e5079109ad996244e2cd9ed7a23976b6fd872c","modified":1586878174000},{"_id":"source/_posts/2020-04-16-leetcode-merge-intervals.md","hash":"4c8e56c27ec095f80ab803a819c83d60ffb6c697","modified":1587036168000},{"_id":"source/_posts/2020-04-21-leetcode-number-of-islands.md","hash":"766d66e49da9072f4e8afa0229b744e9407eed7a","modified":1587444946000},{"_id":"source/_posts/2020-04-16-leetcode-string-to-integer-atoi.md","hash":"903c766c73dd8ed9d1baf081b113113eef0740fa","modified":1587038200000},{"_id":"source/_posts/Postgresql Partitioning.md","hash":"63d88ca7c739c3153d63c7ca0f03c7fbc6708bb0","modified":1573718510000},{"_id":"source/_posts/Django-Manager-Method.md","hash":"5d83851ffbe6d2077a87ef66a7c11e1b13546c1f","modified":1526396315000},{"_id":"source/_posts/Flask-Day-1.md","hash":"19976eabe8d244b085108beb57116b81cabcc693","modified":1526396315000},{"_id":"source/_posts/Flask-Day-2.md","hash":"fea0129a559b2e76aa6927d5df528acd357fe3c9","modified":1526396315000},{"_id":"source/_posts/Tastypie.md","hash":"9b9d53979b98fcdca24c2ad8dc8a78c1dc77a665","modified":1526396315000},{"_id":"source/_posts/first-post.md","hash":"0cfe954d5b7b12d46dd20fe43e95835b89b30163","modified":1526396315000},{"_id":"themes/apollo/.git/HEAD","hash":"acbaef275e46a7f14c1ef456fff2c8bbe8c84724","modified":1526396315000},{"_id":"source/_posts/TastyPie-Note-1.md","hash":"843365f5f5b6142bba6ea900818d19848e3f070b","modified":1526396315000},{"_id":"themes/apollo/.git/config","hash":"e8486e77527181934f2dda23e50b879ab7641244","modified":1526396315000},{"_id":"themes/apollo/.git/description","hash":"9635f1b7e12c045212819dd934d809ef07efa2f4","modified":1526396315000},{"_id":"themes/apollo/.git/index","hash":"3aafcb5c0dca1df92022e4f3167231547c5e79e0","modified":1527846627000},{"_id":"themes/apollo/.git/packed-refs","hash":"0b13c31fe5e09d24b87c9bb547eaa0645fbfd2dc","modified":1526396315000},{"_id":"themes/apollo/doc/doc-en.md","hash":"1bccce1d01f085aedcb01317d2db23ca61351f13","modified":1526396315000},{"_id":"themes/apollo/languages/zh-cn.yml","hash":"9e4b03e14c094000257ea254fd660dde4c7af63c","modified":1526396315000},{"_id":"themes/apollo/doc/doc-zh.md","hash":"3aad2ed65922f6f5dd9731301195474d16a8a9be","modified":1526396315000},{"_id":"themes/apollo/languages/en.yml","hash":"65998758dd27a350b6d4f2dc803970a9c89978da","modified":1526396315000},{"_id":"themes/apollo/layout/index.jade","hash":"58c451042cad5beeb5a76852bba609c651ff3428","modified":1526396315000},{"_id":"themes/apollo/source/favicon.png","hash":"a72e4ae6672cdf2b1e3b018bbca3e3911771d873","modified":1526399879000},{"_id":"themes/apollo/source/logo.png","hash":"42de93a9e76ef2a1a3e42526169a9d1c844a3f5d","modified":1526396315000},{"_id":"themes/apollo/layout/post.jade","hash":"33ab46ab3736e5d51388939858647942ce375b9b","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/commit-msg.sample","hash":"ee1ed5aad98a435f2020b6de35c173b75d9affac","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/applypatch-msg.sample","hash":"86b9655a9ebbde13ac8dd5795eb4d5b539edab0f","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/post-update.sample","hash":"b614c2f63da7dca9f1db2e7ade61ef30448fc96c","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/pre-commit.sample","hash":"36aed8976dcc08b5076844f0ec645b18bc37758f","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/pre-applypatch.sample","hash":"42fa41564917b44183a50c4d94bb03e1768ddad8","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/pre-push.sample","hash":"b4ad74c989616b7395dc6c9fce9871bb1e86dfb5","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/prepare-commit-msg.sample","hash":"2b6275eda365cad50d167fe3a387c9bc9fedd54f","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/pre-rebase.sample","hash":"5885a56ab4fca8075a05a562d005e922cde9853b","modified":1526396315000},{"_id":"themes/apollo/.git/info/exclude","hash":"c879df015d97615050afa7b9641e3352a1e701ac","modified":1526396315000},{"_id":"themes/apollo/.git/hooks/update.sample","hash":"39355a075977d05708ef74e1b66d09a36e486df1","modified":1526396315000},{"_id":"themes/apollo/.git/logs/HEAD","hash":"ff18dd6e76ec3e861fb0bc3789225625abd19928","modified":1526396315000},{"_id":"themes/apollo/layout/mixins/paginator.jade","hash":"f4ee2fb61a32e199b48cf93771749edc8a007391","modified":1526396315000},{"_id":"themes/apollo/layout/mixins/post.jade","hash":"e3c78a617191a2feafd360a5ddbc5cceeae4cf21","modified":1526396315000},{"_id":"themes/apollo/layout/partial/head.jade","hash":"51b2ba6a1cebb275730eb7131eea211c91f0986a","modified":1526396315000},{"_id":"themes/apollo/layout/partial/copyright.jade","hash":"2e2ee43c7241279a32b8c1386a2dea1409ef0e1c","modified":1526396315000},{"_id":"themes/apollo/layout/partial/comment.jade","hash":"ff0a2c269c2434da2ac5529872f1d6184a71f96d","modified":1526396315000},{"_id":"themes/apollo/layout/partial/layout.jade","hash":"d596c281bbba02cf8837f25f8ac0ac06e3d10e72","modified":1526396315000},{"_id":"themes/apollo/layout/partial/nav.jade","hash":"c35d3061da4b053b73150d9741c542d660798270","modified":1526396315000},{"_id":"themes/apollo/source/scss/apollo.scss","hash":"2f0ecd6ea1fec0aad4097dcb509b5ed5d1badd5c","modified":1526396315000},{"_id":"themes/apollo/layout/partial/scripts.jade","hash":"4c83fec1e2fc5cffefafc2e31835e28122c0fdfd","modified":1526396315000},{"_id":"themes/apollo/source/css/apollo.css","hash":"3769b55e027697d6d163ddf82b664b16c767ba9c","modified":1526396315000},{"_id":"themes/apollo/.git/objects/pack/pack-35ea6b850205f59708ed992c93c7cea2251cc278.idx","hash":"6a8c6fe9f118fb534f32f6c5d4811ef895340eba","modified":1526396315000},{"_id":"themes/apollo/.git/refs/heads/master","hash":"d32e5811a7e998bca04209572d5265585c606fdb","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/base.scss","hash":"88b361e68475caddbab763feed5e1db788ac2cd7","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/copyright.scss","hash":"7fc843c37a4dbf9f6e70770398841a73465ec642","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/footer.scss","hash":"094aca6e52f11b139ac7980ca03fa7b9d8fc7b2f","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/header.scss","hash":"d24cc6520f3faa7bb80610b858a92639eadcc289","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/home-post-list.scss","hash":"92858015b8f3dcb4eb91b6dc41563b7aaa91b376","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/mq.scss","hash":"0b9c7097136ac8e4a07d9702fc4dbe0345ac7596","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/post.scss","hash":"3ba64c410edd07e7bf5e9900d9ad9d76f2ff5828","modified":1526396315000},{"_id":"themes/apollo/source/scss/_partial/normalize.scss","hash":"fd0b27bed6f103ea95b08f698ea663ff576dbcf1","modified":1526396315000},{"_id":"themes/apollo/.git/logs/refs/heads/master","hash":"ff18dd6e76ec3e861fb0bc3789225625abd19928","modified":1526396315000},{"_id":"themes/apollo/.git/refs/remotes/origin/HEAD","hash":"d9427cda09aba1cdde5c69c2b13c905bddb0bc51","modified":1526396315000},{"_id":"themes/apollo/.git/objects/pack/pack-35ea6b850205f59708ed992c93c7cea2251cc278.pack","hash":"b17d2960aef474cbff89c4c250ecaffff7c4790c","modified":1526396315000},{"_id":"themes/apollo/.git/logs/refs/remotes/origin/HEAD","hash":"ff18dd6e76ec3e861fb0bc3789225625abd19928","modified":1526396315000},{"_id":"public/atom.xml","hash":"a586321e90ce8456a406696b75b6c9bb17404570","modified":1587444952445},{"_id":"public/sitemap.xml","hash":"d316f7e351e88d31b23877fd010cb44b8087c840","modified":1587444952445},{"_id":"public/archives/index.html","hash":"fe7770520a4482f260261ffeff1bbee3299b8a23","modified":1587446402798},{"_id":"public/archives/2020/index.html","hash":"251bc2a11c2e05f962224521db20f4e220573608","modified":1587446402799},{"_id":"public/archives/2020/page/3/index.html","hash":"29030651310410cb57239babf6e003a2fdd6e980","modified":1587446402799},{"_id":"public/archives/page/2/index.html","hash":"9e6b4caccf0e9b14c1328a18fa946a8bfba6f2a3","modified":1587446402799},{"_id":"public/archives/2020/03/index.html","hash":"234393043bcd67e4e8efcc18f8b216277df715b0","modified":1587446402799},{"_id":"public/archives/2020/04/index.html","hash":"eb1fb49123a961d0927bc97b5074b0c9da56841b","modified":1587446402799},{"_id":"public/categories/leetcode/index.html","hash":"5fa307b686bfb8d48b609df293f9c817262352c1","modified":1587446402799},{"_id":"public/categories/leetcode/page/3/index.html","hash":"6ca07e52142b74cfbf2848d2c176b7e8fd0cad15","modified":1587446402799},{"_id":"public/index.html","hash":"5bf276e85ab585ee24e3cd01bf7b29508112d3b3","modified":1587446402800},{"_id":"public/page/2/index.html","hash":"4be6e473c0a3556143362acf12d9c6853ff1d1b7","modified":1587446402800},{"_id":"public/archives/2020/page/2/index.html","hash":"7959aa328aeb2c26a7b067bcab175c7447bc9986","modified":1587446402799},{"_id":"public/archives/2020/03/page/2/index.html","hash":"de542e40de5442276152044a8a79b57e334b6f3f","modified":1587446402799},{"_id":"public/categories/leetcode/page/2/index.html","hash":"7333305ab06f4d1b1a2b2a5402434bd74727c1f6","modified":1587446402799},{"_id":"public/2020/04/16/leetcode-string-to-integer-atoi/index.html","hash":"5aab7acb06a04909d65f2b37791fe020ac6b4b34","modified":1587446402800},{"_id":"public/2020/04/16/leetcode-01-matrix/index.html","hash":"a59789fc1b8e3e63ae66c5fe582eafd3d4bf2cf1","modified":1587446402800},{"_id":"public/2020/04/16/leetcode-merge-intervals/index.html","hash":"fe43c579b065f030836acf7adf640709bc382339","modified":1587446402800},{"_id":"public/2020/04/14/leetcode-design-twitter/index.html","hash":"4b22f200f0d627c187aa57b18192c1cf46bff91c","modified":1587446402800},{"_id":"public/2020/04/14/leetcode-add-two-numbers-ii/index.html","hash":"1cf8ed5b32d2522f46e1dbf33b351ccb2469de70","modified":1587446402800},{"_id":"public/2020/04/01/leetcode-1103/index.html","hash":"9df68a878761dd1c23aee8cff693137fba062b42","modified":1587446402800},{"_id":"public/2020/04/01/leetcode-compress-string-lcci/index.html","hash":"1692319e76fdba90f4a4835d07dd68956f2385d5","modified":1587446402800},{"_id":"public/2020/04/01/leetcode-1160/index.html","hash":"0f1c09bb7ac1e6a4ee43d6d729baa7e14fb75f8b","modified":1587446402800},{"_id":"public/2020/03/30/leetcode-999/index.html","hash":"bfb9f5ef24d32860c804e02028a3ec472a7f5c81","modified":1587446402800},{"_id":"public/2020/03/30/leetcode-1071/index.html","hash":"4dd54070b7ced7e45cee66a81a61d329600ffde5","modified":1587446402800},{"_id":"public/2020/03/26/leetcode-876/index.html","hash":"d08a6ebc25c304fdbb77cf5efbff3e3a9c66639e","modified":1587446402800},{"_id":"public/2020/03/29/leetcode-1013/index.html","hash":"68403102195d2c4b807802fc26419888c65b1cba","modified":1587446402800},{"_id":"public/2020/03/25/leetcode-543/index.html","hash":"b94a9b3f588cc5e11f6551737c753a1e2adf307e","modified":1587446402801},{"_id":"public/2020/03/29/leetcode-914/index.html","hash":"de4191f15d4a12e822c66b6d0196ee83482519a9","modified":1587446402800},{"_id":"public/2020/03/25/leetcode-836/index.html","hash":"66f352798839414ea29c6118f75d5af739c9d182","modified":1587446402800},{"_id":"public/2020/03/25/leetcode-409/index.html","hash":"41d70012826b76c402daba4f6875a9b52e55264e","modified":1587446402800},{"_id":"public/2020/03/17/leetcode-121/index.html","hash":"9c27e8c6c17fa24951b9857e5695c116df14b4d9","modified":1587446402801},{"_id":"public/2020/03/23/leetcode-169/index.html","hash":"ce2379b39051685a8e32a853d788517568dc4d5a","modified":1587446402801},{"_id":"public/2020/03/23/leetcode-225/index.html","hash":"73855dc29fed498d3c597a0e859001a611d3c47e","modified":1587446402801},{"_id":"public/2018/05/31/bash-function-and-awk/index.html","hash":"e8123f624cdce6ac8c87574232f17de4bd545fa3","modified":1587446402801},{"_id":"public/2019/03/12/Postgresql-Partitioning/index.html","hash":"a280187102a2eeeb1ef3a01a1aa51d4472b93622","modified":1587446402801},{"_id":"public/2019/11/14/AWS-KMS/index.html","hash":"ed7730b48b5c308a09e34064dada6c739b168649","modified":1587446402801},{"_id":"public/2016/05/04/Tastypie/index.html","hash":"b2279db518bc33249f19423ac18d92c4e516c9a6","modified":1587446402801},{"_id":"public/2016/04/25/Django-Manager-Method/index.html","hash":"5e97665a188021701f6904c80457a774ab7161dc","modified":1587446402801},{"_id":"public/2016/02/16/Flask-Day-2/index.html","hash":"b190b4056ab4d2b631a753ad4dec89e04e50e295","modified":1587446402801},{"_id":"public/2020/03/18/leetcode-206/index.html","hash":"867d48227994566c3bee90c1ea41bc05d8fd96f5","modified":1587446402801},{"_id":"public/2016/02/12/first-post/index.html","hash":"d9dfb8c5405c1df371cbadc058d7ea37735cf332","modified":1587446402801},{"_id":"public/2016/05/10/TastyPie-Note-1/index.html","hash":"ff6cb06b7bc1b9e4ea87caa9d4fde8e6264e699f","modified":1587446402801},{"_id":"public/archives/page/4/index.html","hash":"17c87e84bdd240a8c3872be7f954b117a6a0a64e","modified":1587446402801},{"_id":"public/archives/2016/04/index.html","hash":"ba5405758fae3205d45faebd9b6f6c29fabc6e0d","modified":1587446402801},{"_id":"public/2016/02/15/Flask-Day-1/index.html","hash":"70e1fe2be44fe1a1a2c47154a4ca14b77140ed5f","modified":1587446402801},{"_id":"public/archives/2016/05/index.html","hash":"9776f37ffc779f836f175a1a3cc7093d08db6d88","modified":1587446402801},{"_id":"public/archives/2018/index.html","hash":"f68186fdf1b5d2d4b17d60830ba45974a2a6edea","modified":1587446402801},{"_id":"public/archives/2018/05/index.html","hash":"f68186fdf1b5d2d4b17d60830ba45974a2a6edea","modified":1587446402802},{"_id":"public/archives/2019/index.html","hash":"b0ed0f0a97d72f877e32ca35f2e04446c31e87a5","modified":1587446402802},{"_id":"public/archives/2019/03/index.html","hash":"dea6c71318d3a5e846399723c08dd85e501cc853","modified":1587446402802},{"_id":"public/archives/2019/11/index.html","hash":"72c2812063a41e908ece68f6fb02693dabedb231","modified":1587446402802},{"_id":"public/page/4/index.html","hash":"3491fdf675901b32b4d7add96bf20afe0689f000","modified":1587446402802},{"_id":"public/2020/04/09/leetcode-the-masseuse-lcci/index.html","hash":"7aaf9401283dea109df78b0e9c15e4e1b019a97d","modified":1587446402802},{"_id":"public/2020/04/09/leetcode-he-wei-sde-lian-xu-zheng-shu-xu-lie-lcof/index.html","hash":"03d56ffca8243eb43b68ca87c589cff370c75644","modified":1587446402802},{"_id":"public/archives/page/3/index.html","hash":"aebf39704d5f24dbc364347ad432a7a7f11f6fc3","modified":1587446402802},{"_id":"public/archives/2016/index.html","hash":"7f01421dc66b6f3e2cdece71a3e5e8220a126d03","modified":1587446402802},{"_id":"public/archives/2016/02/index.html","hash":"cb7d38e6a2726a642c10ec7799e1c8c7fbaa21f8","modified":1587446402802},{"_id":"public/page/3/index.html","hash":"823df38f48cc7ece2d0a3feebad02e0987f54ab2","modified":1587446402802},{"_id":"public/archives/2020/04/page/2/index.html","hash":"e2cabbebde42d90c7a379c2407b494ebe9718a58","modified":1587446402799},{"_id":"public/2020/04/21/leetcode-number-of-islands/index.html","hash":"03ccb0bb82762283b2945bb6d4bce77fa0dcb3b7","modified":1587446402800},{"_id":"public/logo.png","hash":"42de93a9e76ef2a1a3e42526169a9d1c844a3f5d","modified":1587444954000},{"_id":"public/scss/apollo.scss","hash":"2f0ecd6ea1fec0aad4097dcb509b5ed5d1badd5c","modified":1587444954000},{"_id":"public/CNAME","hash":"c53a99d8555292c2cc8dd02c564b9d5fdfed89cb","modified":1587444954000},{"_id":"public/favicon.png","hash":"a72e4ae6672cdf2b1e3b018bbca3e3911771d873","modified":1587444954000},{"_id":"public/css/apollo.css","hash":"3769b55e027697d6d163ddf82b664b16c767ba9c","modified":1587444954045}],"Category":[{"name":"leetcode","_id":"ck99fqk4a0002subunok11hn6"}],"Data":[],"Page":[],"Post":[{"title":"leetcode-121","date":"2020-03-17T10:06:45.000Z","_content":"\n### 121. 买卖股票的最佳时机\n\n[题目](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)\n\n\n\n原始答案:\n\n```python\nclass Solution:\n def maxProfit(self, prices) -> int:\n profit = 0\n if not prices:\n return profit\n min_buyin = prices[0]\n max_sellout = prices[0]\n l = len(prices)\n i = 0\n for buyin in prices:\n if i == l:\n return profit\n if min_buyin <= buyin:\n max_sellout = max(prices[i:])\n p = max_sellout - min_buyin\n if p > profit:\n profit = p\n if buyin < min_buyin:\n min_buyin = buyin\n i += 1\n return profit\n#1880 ms\t14.4 MB\n```\n\n主要思路是找到波谷,如果当前价格比前一天要低,则还是在去往波谷的路上;当价格比前一天高或相同时,则到达了一个波谷,计算波谷和之后的波峰的差,就是这一段的利润。将从头至尾过一次,就能找到所有波谷和其后波峰的差,返回最大的即可。但是这个明显地在重复max,时间复杂度是O(n^2),看起来就很傻逼。\n\n仔细想想,其实并不需要直接找出波谷后的波峰,只要在for loop时保持波谷为最低的那个,就能算出每一个后续与波谷的差,找最大差即可。改了下代码变成这样\n\n```python\nclass Solution:\n def maxProfit(self, prices) -> int:\n profit = 0\n if not prices:\n return profit\n min_buyin = prices[0]\n for i in range(1, len(prices)):\n min_buyin = min(min_buyin, prices[i-1])\n profit = max(prices[i] - min_buyin, profit)\n return profit\n #44 ms\t14.4 MB\n```\n\n\n","source":"_posts/2020-03-17-leetcode-121.md","raw":"---\ntitle: leetcode-121\ndate: 2020-03-17 18:06:45\ntags:\ncategories: leetcode\n---\n\n### 121. 买卖股票的最佳时机\n\n[题目](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock/)\n\n\n\n原始答案:\n\n```python\nclass Solution:\n def maxProfit(self, prices) -> int:\n profit = 0\n if not prices:\n return profit\n min_buyin = prices[0]\n max_sellout = prices[0]\n l = len(prices)\n i = 0\n for buyin in prices:\n if i == l:\n return profit\n if min_buyin <= buyin:\n max_sellout = max(prices[i:])\n p = max_sellout - min_buyin\n if p > profit:\n profit = p\n if buyin < min_buyin:\n min_buyin = buyin\n i += 1\n return profit\n#1880 ms\t14.4 MB\n```\n\n主要思路是找到波谷,如果当前价格比前一天要低,则还是在去往波谷的路上;当价格比前一天高或相同时,则到达了一个波谷,计算波谷和之后的波峰的差,就是这一段的利润。将从头至尾过一次,就能找到所有波谷和其后波峰的差,返回最大的即可。但是这个明显地在重复max,时间复杂度是O(n^2),看起来就很傻逼。\n\n仔细想想,其实并不需要直接找出波谷后的波峰,只要在for loop时保持波谷为最低的那个,就能算出每一个后续与波谷的差,找最大差即可。改了下代码变成这样\n\n```python\nclass Solution:\n def maxProfit(self, prices) -> int:\n profit = 0\n if not prices:\n return profit\n min_buyin = prices[0]\n for i in range(1, len(prices)):\n min_buyin = min(min_buyin, prices[i-1])\n profit = max(prices[i] - min_buyin, profit)\n return profit\n #44 ms\t14.4 MB\n```\n\n\n","slug":"leetcode-121","published":1,"updated":"2020-03-17T10:54:03.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk450000subu0lleqph1","content":"
原始答案:
\n1 | class Solution: |
主要思路是找到波谷,如果当前价格比前一天要低,则还是在去往波谷的路上;当价格比前一天高或相同时,则到达了一个波谷,计算波谷和之后的波峰的差,就是这一段的利润。将从头至尾过一次,就能找到所有波谷和其后波峰的差,返回最大的即可。但是这个明显地在重复max,时间复杂度是O(n^2),看起来就很傻逼。
\n仔细想想,其实并不需要直接找出波谷后的波峰,只要在for loop时保持波谷为最低的那个,就能算出每一个后续与波谷的差,找最大差即可。改了下代码变成这样
\n1 | class Solution: |
原始答案:
\n1 | class Solution: |
主要思路是找到波谷,如果当前价格比前一天要低,则还是在去往波谷的路上;当价格比前一天高或相同时,则到达了一个波谷,计算波谷和之后的波峰的差,就是这一段的利润。将从头至尾过一次,就能找到所有波谷和其后波峰的差,返回最大的即可。但是这个明显地在重复max,时间复杂度是O(n^2),看起来就很傻逼。
\n仔细想想,其实并不需要直接找出波谷后的波峰,只要在for loop时保持波谷为最低的那个,就能算出每一个后续与波谷的差,找最大差即可。改了下代码变成这样
\n1 | class Solution: |
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.
\nBash 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:
1 | function run { |
I activate django virtural enviroment first and then run django serve.
\nFunctions, 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:
\n1 | function ba { hgba | grep \"$1\" } |
I can just type ba release then get current release branch info.
Append:
\nI made some updates to the ba function and make it auto copying the branch result to my clipboard:
1 |
|
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.
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.
\nBash 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:
1 | function run { |
I activate django virtural enviroment first and then run django serve.
\nFunctions, 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:
\n1 | function ba { hgba | grep \"$1\" } |
I can just type ba release then get current release branch info.
Append:
\nI made some updates to the ba function and make it auto copying the branch result to my clipboard:
1 |
|
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.
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.
\nSince AWS finally provides KMS(Key Management Service) in our local region, we try to encrypt every private credentials by KMS and store them on S3.
\nTBD
\n","site":{"data":{}},"excerpt":"","more":"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.
\nSince AWS finally provides KMS(Key Management Service) in our local region, we try to encrypt every private credentials by KMS and store them on S3.
\nTBD
\n"},{"title":"leetcode-206","date":"2020-03-18T15:33:03.000Z","_content":"\n### 206. 反转链表\n\n[题目](https://leetcode-cn.com/problems/reverse-linked-list/)\n\n\n\n最简单的思路是遍历链表一个列表去做存储,通过倒序读取列表的同时改写链表。\n\n```python\nclass ListNode:\n def __init__(self, x):\n self.val = x\n self.next = None\n\nclass Solution:\n def reverseList(self, head):\n if not head or not head.next:\n return head\n nl = []\n while head.next:\n nl.append(head)\n head = head.next\n nl.append(head)\n l = len(nl)\n for x in range(l):\n if x == 0:\n nl[x].next = None\n continue\n nl[x].next = nl[x-1]\n if x == (l - 1):\n return nl[x]\n```\n\n仔细想想自己又傻逼了,何必要遍历两次呢,在第一遍遍历的同时就能操作了:\n\n```python\nclass Solution:\n def reverseList(self, head):\n if not head or not head.next:\n return head\n prev_node = None\n next_node = head.next\n while head:\n next_node = head.next\n head.next = prev_node\n prev_node = head\n head = next_node\n return prev_node\n```\n\n然后是递归的做法,主要思路是一直进到最深一层--也就是链表的最后一个--的时候开始返回,同时修改那一层的两个 node。一开始踩了一个坑是返回了每一个node,结果最后回到第一层的时候得到的是链表的末端,其实只需要修改链表,并不需要返回 node,所以一开始到达链表末端的时候直接返回那一个node就可以了。\n\n```python\nclass Solution:\n def reverseList(self, head):\n if not head:\n return head\n if head.next:\n ss = Solution()\n last = ss.reverseList(head.next)\n head.next.next = head\n head.next = None\n return last\n return head\n```\n\n一开始是用list来打草稿,不过想明白递归之后就大同小异了:\n\n```python\ndef a(l:list)->list:\n k=[l[0]]\n\n if l[1:]:\n b=a(l[1:])\n b.extend(k)\n else:\n return [l[0]]\n return b\n```\n\n\n","source":"_posts/2020-03-18-leetcode-206.md","raw":"---\ntitle: leetcode-206\ndate: 2020-03-18 23:33:03\ntags:\ncategories: leetcode\n---\n\n### 206. 反转链表\n\n[题目](https://leetcode-cn.com/problems/reverse-linked-list/)\n\n\n\n最简单的思路是遍历链表一个列表去做存储,通过倒序读取列表的同时改写链表。\n\n```python\nclass ListNode:\n def __init__(self, x):\n self.val = x\n self.next = None\n\nclass Solution:\n def reverseList(self, head):\n if not head or not head.next:\n return head\n nl = []\n while head.next:\n nl.append(head)\n head = head.next\n nl.append(head)\n l = len(nl)\n for x in range(l):\n if x == 0:\n nl[x].next = None\n continue\n nl[x].next = nl[x-1]\n if x == (l - 1):\n return nl[x]\n```\n\n仔细想想自己又傻逼了,何必要遍历两次呢,在第一遍遍历的同时就能操作了:\n\n```python\nclass Solution:\n def reverseList(self, head):\n if not head or not head.next:\n return head\n prev_node = None\n next_node = head.next\n while head:\n next_node = head.next\n head.next = prev_node\n prev_node = head\n head = next_node\n return prev_node\n```\n\n然后是递归的做法,主要思路是一直进到最深一层--也就是链表的最后一个--的时候开始返回,同时修改那一层的两个 node。一开始踩了一个坑是返回了每一个node,结果最后回到第一层的时候得到的是链表的末端,其实只需要修改链表,并不需要返回 node,所以一开始到达链表末端的时候直接返回那一个node就可以了。\n\n```python\nclass Solution:\n def reverseList(self, head):\n if not head:\n return head\n if head.next:\n ss = Solution()\n last = ss.reverseList(head.next)\n head.next.next = head\n head.next = None\n return last\n return head\n```\n\n一开始是用list来打草稿,不过想明白递归之后就大同小异了:\n\n```python\ndef a(l:list)->list:\n k=[l[0]]\n\n if l[1:]:\n b=a(l[1:])\n b.extend(k)\n else:\n return [l[0]]\n return b\n```\n\n\n","slug":"leetcode-206","published":1,"updated":"2020-03-18T15:44:12.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk4c0004subueeqgf7uv","content":"最简单的思路是遍历链表一个列表去做存储,通过倒序读取列表的同时改写链表。
\n1 | class ListNode: |
仔细想想自己又傻逼了,何必要遍历两次呢,在第一遍遍历的同时就能操作了:
\n1 | class Solution: |
然后是递归的做法,主要思路是一直进到最深一层–也就是链表的最后一个–的时候开始返回,同时修改那一层的两个 node。一开始踩了一个坑是返回了每一个node,结果最后回到第一层的时候得到的是链表的末端,其实只需要修改链表,并不需要返回 node,所以一开始到达链表末端的时候直接返回那一个node就可以了。
\n1 | class Solution: |
一开始是用list来打草稿,不过想明白递归之后就大同小异了:
\n1 | def a(l:list)->list: |
最简单的思路是遍历链表一个列表去做存储,通过倒序读取列表的同时改写链表。
\n1 | class ListNode: |
仔细想想自己又傻逼了,何必要遍历两次呢,在第一遍遍历的同时就能操作了:
\n1 | class Solution: |
然后是递归的做法,主要思路是一直进到最深一层–也就是链表的最后一个–的时候开始返回,同时修改那一层的两个 node。一开始踩了一个坑是返回了每一个node,结果最后回到第一层的时候得到的是链表的末端,其实只需要修改链表,并不需要返回 node,所以一开始到达链表末端的时候直接返回那一个node就可以了。
\n1 | class Solution: |
一开始是用list来打草稿,不过想明白递归之后就大同小异了:
\n1 | def a(l:list)->list: |
注意栈是 FILO(First In Last Out),Python 的 list 是 FIFO(First In First Out)。
\n1 | class MyStack: |
注意栈是 FILO(First In Last Out),Python 的 list 是 FIFO(First In First Out)。
\n1 | class MyStack: |
一开始的思路是遍历一遍整个列表,用一个字典去记录每个元素出现的次数,当次数大于 $\\cfrac{n}{2}$ 时就可以得出结果。
\n1 | class Solution: |
Python 也有专门计数的库,写起来更简单一点:
\n1 | class Solution: |
由于要找的数出现次数大于 $\\cfrac{n}{2}$,脑子里掠过一下蒙特卡罗算法,后来在官方解答中也看到类似的思路了:
\n1 | class Solution: |
一开始的思路是遍历一遍整个列表,用一个字典去记录每个元素出现的次数,当次数大于 $\\cfrac{n}{2}$ 时就可以得出结果。
\n1 | class Solution: |
Python 也有专门计数的库,写起来更简单一点:
\n1 | class Solution: |
由于要找的数出现次数大于 $\\cfrac{n}{2}$,脑子里掠过一下蒙特卡罗算法,后来在官方解答中也看到类似的思路了:
\n1 | class Solution: |
一开始理解错题目了,以为是寻找字符串中的最长回文串,结果是构造。但是原理基本一样,由于回文中心对称,所以是由多个偶数个相同字母和至多一个奇数个相同字母组成。
\n这样只要数给出的字符串中有几个偶数个相同字母和几个奇数个相同字母就可以了。奇数个相同字母可以减少一个当偶数个用,最后再加回去一个。
\n1 | class Solution: |
一开始理解错题目了,以为是寻找字符串中的最长回文串,结果是构造。但是原理基本一样,由于回文中心对称,所以是由多个偶数个相同字母和至多一个奇数个相同字母组成。
\n这样只要数给出的字符串中有几个偶数个相同字母和几个奇数个相同字母就可以了。奇数个相同字母可以减少一个当偶数个用,最后再加回去一个。
\n1 | class Solution: |
这题做出来了但是没有通过运行时间的测试,主要还是没想明白二叉树的直径到底是什么东西,用了个蠢办法。
\n1 | # Definition for a binary tree node. |
L43 之前做的是以深度优先的方式遍历一遍树,得出每个点的路径。后面的是将所有路径组合在一起得出任意两个点间的路径,算出最大长度。
\n其实以某个点为根节点的树的直径,就是某个节点的左子树的深度和右子树的深度的和,用递归来处理这个会比较容易理解
\n1 | class Solution(object): |
这题做出来了但是没有通过运行时间的测试,主要还是没想明白二叉树的直径到底是什么东西,用了个蠢办法。
\n1 | # Definition for a binary tree node. |
L43 之前做的是以深度优先的方式遍历一遍树,得出每个点的路径。后面的是将所有路径组合在一起得出任意两个点间的路径,算出最大长度。
\n其实以某个点为根节点的树的直径,就是某个节点的左子树的深度和右子树的深度的和,用递归来处理这个会比较容易理解
\n1 | class Solution(object): |
看两个矩形有没有重叠,就看两个矩形在坐标轴上的投影有没有重叠。
\n1 | class Solution: |
看两个矩形有没有重叠,就看两个矩形在坐标轴上的投影有没有重叠。
\n1 | class Solution: |
因为是整数数组,如果能均分成三份,则数组和肯定是3的倍数。然后遍历数组逐端求和使得和为 sum(A)/3。
\n1 | class Solution: |
因为是整数数组,如果能均分成三份,则数组和肯定是3的倍数。然后遍历数组逐端求和使得和为 sum(A)/3。
\n1 | class Solution: |
思路是遍历一遍得到整个链表,讲每个 node 放进一个 list,就可以通过下标得到中间的。
\n1 | # Definition for singly-linked list. |
看官方解答,还有一个骚操作,通过两个速度不一样的指针,一个一次走一步,一个两次走一步,快的走到底时,慢的就在中间了。
\n1 | class Solution: |
思路是遍历一遍得到整个链表,讲每个 node 放进一个 list,就可以通过下标得到中间的。
\n1 | # Definition for singly-linked list. |
看官方解答,还有一个骚操作,通过两个速度不一样的指针,一个一次走一步,一个两次走一步,快的走到底时,慢的就在中间了。
\n1 | class Solution: |
将大牌堆分成多个牌数量相等的小牌堆,就是求每张牌数量的公约数。先遍历一遍得到每张牌的数量,然后找出比2大的公约数即可。
\n1 | class Solution: |
将大牌堆分成多个牌数量相等的小牌堆,就是求每张牌数量的公约数。先遍历一遍得到每张牌的数量,然后找出比2大的公约数即可。
\n1 | class Solution: |
如果存在这样字符串,那它最大的长度就是这两个字符串长度的最大公约数。
\n1 | class Solution: |
官方解答中还给了一种巧妙的解法,如果 str1 + str2 == str2 + str1 的话,可以证明必定存在这样一个字符串,其长度为两个字符串长度的最大公约数。
\n","site":{"data":{}},"excerpt":"如果存在这样字符串,那它最大的长度就是这两个字符串长度的最大公约数。
\n1 | class Solution: |
官方解答中还给了一种巧妙的解法,如果 str1 + str2 == str2 + str1 的话,可以证明必定存在这样一个字符串,其长度为两个字符串长度的最大公约数。
"},{"title":"leetcode-999","date":"2020-03-30T13:03:25.000Z","_content":"\n\n### 999. 可以被一步捕获的棋子数\n\n[题目](https://leetcode-cn.com/problems/available-captures-for-rook/)\n\n\n\n\n\n\n\n遍历一遍找到车的坐标,然后按上下左右四个方向循环一下看碰到的第一个棋子是什么。\n\n\n\n```python\nclass Solution:\n def numRookCaptures(self, board) -> int:\n i = j = 0\n for row in board:\n if 'R' in row:\n break\n i += 1\n j = row.index('R')\n count = 0\n # right\n for x in range(j + 1, 8):\n if row[x] == 'p':\n count += 1\n break\n if row[x] == 'B':\n break\n # left\n for x in range(j, 0, -1):\n if row[x] == 'p':\n count += 1\n break\n if row[x] == 'B':\n break\n # up\n for x in range(i, 0, -1):\n if board[x][j] == 'p':\n count += 1\n break\n if board[x][j] == 'B':\n break\n # down\n for x in range(i+1, 8):\n if board[x][j] == 'p':\n count += 1\n break\n if board[x][j] == 'B':\n break\n\n return count\n\n#36 ms\t13.6 MB\n\n```\n\n\n\n问题不难,官方解答中给了一个方向数组的概念,上下左右是 (0, 1) (0, -1) (-1, 0) (1, 0),有点像向量的意思。走的路线等于方向数组乘以步数。\n\n","source":"_posts/2020-03-30-leetcode-999.md","raw":"---\ntitle: leetcode-999\ndate: 2020-03-30 21:03:25\ntags:\ncategories: leetcode\n---\n\n\n### 999. 可以被一步捕获的棋子数\n\n[题目](https://leetcode-cn.com/problems/available-captures-for-rook/)\n\n\n\n\n\n\n\n遍历一遍找到车的坐标,然后按上下左右四个方向循环一下看碰到的第一个棋子是什么。\n\n\n\n```python\nclass Solution:\n def numRookCaptures(self, board) -> int:\n i = j = 0\n for row in board:\n if 'R' in row:\n break\n i += 1\n j = row.index('R')\n count = 0\n # right\n for x in range(j + 1, 8):\n if row[x] == 'p':\n count += 1\n break\n if row[x] == 'B':\n break\n # left\n for x in range(j, 0, -1):\n if row[x] == 'p':\n count += 1\n break\n if row[x] == 'B':\n break\n # up\n for x in range(i, 0, -1):\n if board[x][j] == 'p':\n count += 1\n break\n if board[x][j] == 'B':\n break\n # down\n for x in range(i+1, 8):\n if board[x][j] == 'p':\n count += 1\n break\n if board[x][j] == 'B':\n break\n\n return count\n\n#36 ms\t13.6 MB\n\n```\n\n\n\n问题不难,官方解答中给了一个方向数组的概念,上下左右是 (0, 1) (0, -1) (-1, 0) (1, 0),有点像向量的意思。走的路线等于方向数组乘以步数。\n\n","slug":"leetcode-999","published":1,"updated":"2020-03-30T13:03:41.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk4p000lsubu6henmdmq","content":"遍历一遍找到车的坐标,然后按上下左右四个方向循环一下看碰到的第一个棋子是什么。
\n1 | class Solution: |
问题不难,官方解答中给了一个方向数组的概念,上下左右是 (0, 1) (0, -1) (-1, 0) (1, 0),有点像向量的意思。走的路线等于方向数组乘以步数。
\n","site":{"data":{}},"excerpt":"遍历一遍找到车的坐标,然后按上下左右四个方向循环一下看碰到的第一个棋子是什么。
\n1 | class Solution: |
问题不难,官方解答中给了一个方向数组的概念,上下左右是 (0, 1) (0, -1) (-1, 0) (1, 0),有点像向量的意思。走的路线等于方向数组乘以步数。
"},{"title":"leetcode-1103","date":"2020-04-01T03:22:20.000Z","mathjax":true,"_content":"\n### 1103. 分糖果 II\n\n[题目](https://leetcode-cn.com/problems/distribute-candies-to-people/)\n\n\n\n\n\n\n\n小学奥数题。主要思路就是等差数列求和 $\\cfrac{(首项 + 末项)×项数}{2}$ 。可以用公式把每一个位置获得的总糖果数表示出来。我的方法稍微蠢了点,算了每一轮的总糖果数,其实可以直接求总共发了多少次糖果,除以每轮的人数就可以得出发了多少轮。\n\n\n\n```python\nclass Solution:\n def distributeCandies(self, candies: int, num_people: int):\n total = 0\n i = 0\n # import ipdb; ipdb.set_trace()\n while total <= candies:\n t = (num_people*i)*num_people + int((1+num_people)*num_people/2)\n if total + t <= candies:\n total += t\n i += 1\n else:\n break\n remaining = candies - total\n print(total, remaining, i)\n l = []\n for n in range(1, num_people+1):\n if not total:\n current_candy = n\n else:\n current_candy = n+i*num_people\n\n n_count = int((0+(i-1))*(i)/2)\n print(current_candy, n_count)\n if remaining >= current_candy:\n l.append(n_count*num_people + n*i + current_candy)\n remaining -= current_candy\n else:\n l.append(n_count*num_people + n*i + remaining)\n remaining = 0\n return l\n# 28 ms\t13.7 MB,\n```\n","source":"_posts/2020-04-01-leetcode-1103.md","raw":"---\ntitle: leetcode-1103\ndate: 2020-04-01 11:22:20\ntags:\ncategories: leetcode\nmathjax: true\n---\n\n### 1103. 分糖果 II\n\n[题目](https://leetcode-cn.com/problems/distribute-candies-to-people/)\n\n\n\n\n\n\n\n小学奥数题。主要思路就是等差数列求和 $\\cfrac{(首项 + 末项)×项数}{2}$ 。可以用公式把每一个位置获得的总糖果数表示出来。我的方法稍微蠢了点,算了每一轮的总糖果数,其实可以直接求总共发了多少次糖果,除以每轮的人数就可以得出发了多少轮。\n\n\n\n```python\nclass Solution:\n def distributeCandies(self, candies: int, num_people: int):\n total = 0\n i = 0\n # import ipdb; ipdb.set_trace()\n while total <= candies:\n t = (num_people*i)*num_people + int((1+num_people)*num_people/2)\n if total + t <= candies:\n total += t\n i += 1\n else:\n break\n remaining = candies - total\n print(total, remaining, i)\n l = []\n for n in range(1, num_people+1):\n if not total:\n current_candy = n\n else:\n current_candy = n+i*num_people\n\n n_count = int((0+(i-1))*(i)/2)\n print(current_candy, n_count)\n if remaining >= current_candy:\n l.append(n_count*num_people + n*i + current_candy)\n remaining -= current_candy\n else:\n l.append(n_count*num_people + n*i + remaining)\n remaining = 0\n return l\n# 28 ms\t13.7 MB,\n```\n","slug":"leetcode-1103","published":1,"updated":"2020-04-08T17:17:27.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk4q000nsubuw1s0x6b6","content":"小学奥数题。主要思路就是等差数列求和
1 | class Solution: |
小学奥数题。主要思路就是等差数列求和
1 | class Solution: |
利用列表 remove 方法,检查 chars 中是否有足够的字母拼写 word
\n1 | class Solution: |
利用列表 remove 方法,检查 chars 中是否有足够的字母拼写 word
\n1 | class Solution: |
遍历一遍字符串,遇到跟上一个字符不同的字符时记录上一个字符的重复长度。
\n1 | class Solution: |
遍历一遍字符串,遇到跟上一个字符不同的字符时记录上一个字符的重复长度。
\n1 | class Solution: |
又是小学奥数。由等差数列求和公式
由小到大遍历
1 | import math |
又是小学奥数。由等差数列求和公式
由小到大遍历
1 | import math |
看到顺序的链表就想到用倒序链表的方法做,折腾了半天
\n1 | class ListNode: |
最后面各种进位的处理应该还可以更清晰优雅一些,但是懒得搞了,感觉很蠢。翻了答案看到了小 tips,需要倒序处理的情况可以用栈。
\n1 | class Solution: |
不过就执行效率来看差不多。
\n","site":{"data":{}},"excerpt":"看到顺序的链表就想到用倒序链表的方法做,折腾了半天
\n1 | class ListNode: |
最后面各种进位的处理应该还可以更清晰优雅一些,但是懒得搞了,感觉很蠢。翻了答案看到了小 tips,需要倒序处理的情况可以用栈。
\n1 | class Solution: |
不过就执行效率来看差不多。
"},{"title":"leetcode-the-masseuse-lcci","date":"2020-04-08T16:35:26.000Z","mathjax":true,"_content":"\n### 面试题 17.16. 按摩师\n\n[题目](https://leetcode-cn.com/problems/the-masseuse-lcci/)\n\n\n\n\n\n\n\n一开始以为是用递归,想了半天没想出来,偷看了一下答案。答案的思路跟递归类似,假设在当前 $i$ 时刻,$dp[i][0]$ 为当前预约不接的情况下最长预约时间,$dp[i][1]$ 则为接受当前预约的最长预约时间。\n\n\n\n那很显然,由于不能接受相邻两个预约,$dp[i][1] = dp[i-1][0] + nums_i$\n\n不接受当前预约的话,上一个预约接不接受都可以,$dp[i][0] = max(dp[i-1][0], dp[i-1][1])$\n\n最后只要比较两种情况即可 $max(dp[i][0], dp[i][1])$\n\n\n\n```python\nclass Solution:\n def massage(self, nums) -> int:\n if not nums:\n return 0\n n = len(nums)\n not_choose = 0\n choose = 0\n for n in nums:\n not_choose, choose = max(not_choose, choose), not_choose+n\n return max(not_choose, choose)\n # 52 ms\t13.6 MB\n```\n\n\n\n这种问题原来有个名字叫[动态规划](https://zh.wikipedia.org/wiki/动态规划),上面推导的方程叫[状态转移方程](https://baike.baidu.com/item/状态转移方程),可以找找资料来看一下。\n","source":"_posts/2020-04-09-leetcode-the-masseuse-lcci.md","raw":"---\ntitle: leetcode-the-masseuse-lcci\ndate: 2020-04-09 00:35:26\ntags:\ncategories: leetcode\nmathjax: true\n---\n\n### 面试题 17.16. 按摩师\n\n[题目](https://leetcode-cn.com/problems/the-masseuse-lcci/)\n\n\n\n\n\n\n\n一开始以为是用递归,想了半天没想出来,偷看了一下答案。答案的思路跟递归类似,假设在当前 $i$ 时刻,$dp[i][0]$ 为当前预约不接的情况下最长预约时间,$dp[i][1]$ 则为接受当前预约的最长预约时间。\n\n\n\n那很显然,由于不能接受相邻两个预约,$dp[i][1] = dp[i-1][0] + nums_i$\n\n不接受当前预约的话,上一个预约接不接受都可以,$dp[i][0] = max(dp[i-1][0], dp[i-1][1])$\n\n最后只要比较两种情况即可 $max(dp[i][0], dp[i][1])$\n\n\n\n```python\nclass Solution:\n def massage(self, nums) -> int:\n if not nums:\n return 0\n n = len(nums)\n not_choose = 0\n choose = 0\n for n in nums:\n not_choose, choose = max(not_choose, choose), not_choose+n\n return max(not_choose, choose)\n # 52 ms\t13.6 MB\n```\n\n\n\n这种问题原来有个名字叫[动态规划](https://zh.wikipedia.org/wiki/动态规划),上面推导的方程叫[状态转移方程](https://baike.baidu.com/item/状态转移方程),可以找找资料来看一下。\n","slug":"leetcode-the-masseuse-lcci","published":1,"updated":"2020-04-08T17:14:04.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk4v000xsubu9bmmz6rr","content":"一开始以为是用递归,想了半天没想出来,偷看了一下答案。答案的思路跟递归类似,假设在当前
那很显然,由于不能接受相邻两个预约,
不接受当前预约的话,上一个预约接不接受都可以,
最后只要比较两种情况即可
1 | class Solution: |
这种问题原来有个名字叫动态规划,上面推导的方程叫状态转移方程,可以找找资料来看一下。
\n","site":{"data":{}},"excerpt":"一开始以为是用递归,想了半天没想出来,偷看了一下答案。答案的思路跟递归类似,假设在当前
那很显然,由于不能接受相邻两个预约,
不接受当前预约的话,上一个预约接不接受都可以,
最后只要比较两种情况即可
1 | class Solution: |
这种问题原来有个名字叫动态规划,上面推导的方程叫状态转移方程,可以找找资料来看一下。
\n"},{"title":"leetcode-01-matrix","date":"2020-04-16T04:26:34.000Z","_content":"\n### 542. 01 矩阵\n\n[题目](https://leetcode-cn.com/problems/01-matrix/)\n\n\n\n\n\n\n\n想了两种思路\n\n1. 0 位置的上下左右是 1, 上下左右中有跟 1 相邻的就是 2,以此类推,从 0 的坐标开始往上下左右四个方向扩散。如果我们把同意个距离的看作是一层,可以用一个队列依次存放每一层的坐标,直至每个坐标都被计算过。\n\n ```python\n class Solution:\n def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:\n m, n = len(matrix), len(matrix[0])\n dist = [[0] * n for _ in range(m)]\n zeroes_pos = [(i, j) for i in range(m) for j in range(n) if matrix[i][j] == 0]\n # 将所有的 0 添加进初始队列中\n q = collections.deque(zeroes_pos)\n seen = set(zeroes_pos)\n\n # 广度优先搜索\n while q:\n i, j = q.popleft()\n for ni, nj in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:\n if 0 <= ni < m and 0 <= nj < n and (ni, nj) not in seen:\n dist[ni][nj] = dist[i][j] + 1\n q.append((ni, nj))\n seen.add((ni, nj))\n\n return dist\n ```\n\n\n\n2. 从左上角开往右下角遍历矩阵,当前坐标的距离由左和上两个位置的值确定。遍历一遍后,再反过来从右下角开始往左上角遍历,当前坐标的距离根据右和下两个位置的值确定,比较这两次得出的值中较小的一个即为该点的距离。\n\n","source":"_posts/2020-04-16-leetcode-01-matrix.md","raw":"---\ntitle: leetcode-01-matrix\ndate: 2020-04-16 12:26:34\ntags:\ncategories: leetcode\n---\n\n### 542. 01 矩阵\n\n[题目](https://leetcode-cn.com/problems/01-matrix/)\n\n\n\n\n\n\n\n想了两种思路\n\n1. 0 位置的上下左右是 1, 上下左右中有跟 1 相邻的就是 2,以此类推,从 0 的坐标开始往上下左右四个方向扩散。如果我们把同意个距离的看作是一层,可以用一个队列依次存放每一层的坐标,直至每个坐标都被计算过。\n\n ```python\n class Solution:\n def updateMatrix(self, matrix: List[List[int]]) -> List[List[int]]:\n m, n = len(matrix), len(matrix[0])\n dist = [[0] * n for _ in range(m)]\n zeroes_pos = [(i, j) for i in range(m) for j in range(n) if matrix[i][j] == 0]\n # 将所有的 0 添加进初始队列中\n q = collections.deque(zeroes_pos)\n seen = set(zeroes_pos)\n\n # 广度优先搜索\n while q:\n i, j = q.popleft()\n for ni, nj in [(i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)]:\n if 0 <= ni < m and 0 <= nj < n and (ni, nj) not in seen:\n dist[ni][nj] = dist[i][j] + 1\n q.append((ni, nj))\n seen.add((ni, nj))\n\n return dist\n ```\n\n\n\n2. 从左上角开往右下角遍历矩阵,当前坐标的距离由左和上两个位置的值确定。遍历一遍后,再反过来从右下角开始往左上角遍历,当前坐标的距离根据右和下两个位置的值确定,比较这两次得出的值中较小的一个即为该点的距离。\n\n","slug":"leetcode-01-matrix","published":1,"updated":"2020-04-16T04:26:56.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk4w000zsubuceivn0ao","content":"想了两种思路
\n0 位置的上下左右是 1, 上下左右中有跟 1 相邻的就是 2,以此类推,从 0 的坐标开始往上下左右四个方向扩散。如果我们把同意个距离的看作是一层,可以用一个队列依次存放每一层的坐标,直至每个坐标都被计算过。
\n1 | class Solution: |
从左上角开往右下角遍历矩阵,当前坐标的距离由左和上两个位置的值确定。遍历一遍后,再反过来从右下角开始往左上角遍历,当前坐标的距离根据右和下两个位置的值确定,比较这两次得出的值中较小的一个即为该点的距离。
\n想了两种思路
\n0 位置的上下左右是 1, 上下左右中有跟 1 相邻的就是 2,以此类推,从 0 的坐标开始往上下左右四个方向扩散。如果我们把同意个距离的看作是一层,可以用一个队列依次存放每一层的坐标,直至每个坐标都被计算过。
\n1 | class Solution: |
从左上角开往右下角遍历矩阵,当前坐标的距离由左和上两个位置的值确定。遍历一遍后,再反过来从右下角开始往左上角遍历,当前坐标的距离根据右和下两个位置的值确定,比较这两次得出的值中较小的一个即为该点的距离。
\n做出来倒是很简单,由于没有并发和特别的条件,测试数据量也不大。一开始搞错了,以为传入的 twitterId 就是自增的 id,结果其实是每条推的内容,所以增加了一个计数器去标记 id。
主要的考点应该是 多路归并 这个东西。我用的是排序,在数据量大的时候应该会有些问题。
1 | class Twitter: |
做出来倒是很简单,由于没有并发和特别的条件,测试数据量也不大。一开始搞错了,以为传入的 twitterId 就是自增的 id,结果其实是每条推的内容,所以增加了一个计数器去标记 id。
主要的考点应该是 多路归并 这个东西。我用的是排序,在数据量大的时候应该会有些问题。
1 | class Twitter: |
首先将区间按起点由小到大排序,这样相邻的两个就能通过终点判断是否重合。
\n1 | class Solution: |
首先将区间按起点由小到大排序,这样相邻的两个就能通过终点判断是否重合。
\n1 | class Solution: |
这种矩阵题现在第一反应就是用广度优先搜索做,类似之前算和0之间的距离那题。遍历矩阵,遇到 1 就将 1 改成 0,然后广度优先搜索找出 1 相邻的所有 1,这就是一个岛屿,以此类推。
\n1 | import collections |
这种矩阵题现在第一反应就是用广度优先搜索做,类似之前算和0之间的距离那题。遍历矩阵,遇到 1 就将 1 改成 0,然后广度优先搜索找出 1 相邻的所有 1,这就是一个岛屿,以此类推。
\n1 | import collections |
没什么好说的,注意各种情况,识别到数字之后就一直要是数字。
\n1 | class Solution: |
没什么好说的,注意各种情况,识别到数字之后就一直要是数字。
\n1 | class Solution: |
Partitioning refers to splitting what is logically one large table inot smaller physical pieces.
Currently, PostgreSQL supports partitioning via table inheritance. Each partition must be created as a child table of a single parent table. The parent table itself is normally empty; It exists just to represent the entire data set.
\nThere are two forms of partitioning can be implemented in PostgreSQL:
\nRange Partitioning
\n 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.
\nList Partitioning
\n The table is partitioned by explicitly listing which key values appear in each partition.
\nCreate the “master” / “parent” table, from which all the partitions will inherit.
\n 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.
\nCreate “child” tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master.
\nAdd table constraints to the partition tables to define the allowed key values in each partitions.
\n 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.
\nCreate indexes on column(s) for each partitions.
\nOptionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.
\nEnsure hte constraint_exclusion configuration parameter is not disabled in postgresql.conf. If it is, queries will not be optimized as desired.
As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.
\n","site":{"data":{}},"excerpt":"","more":"Partitioning refers to splitting what is logically one large table inot smaller physical pieces.
Currently, PostgreSQL supports partitioning via table inheritance. Each partition must be created as a child table of a single parent table. The parent table itself is normally empty; It exists just to represent the entire data set.
\nThere are two forms of partitioning can be implemented in PostgreSQL:
\nRange Partitioning
\n 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.
\nList Partitioning
\n The table is partitioned by explicitly listing which key values appear in each partition.
\nCreate the “master” / “parent” table, from which all the partitions will inherit.
\n 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.
\nCreate “child” tables that each inherit form the master table. Normally, these tables will not add any columns to the set inherited from the master.
\nAdd table constraints to the partition tables to define the allowed key values in each partitions.
\n 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.
\nCreate indexes on column(s) for each partitions.
\nOptionally, define a trigger or rule to redirect data inserted into the master table to the appropriate partition.
\nEnsure hte constraint_exclusion configuration parameter is not disabled in postgresql.conf. If it is, queries will not be optimized as desired.
As we are creating new table and hopping data insered to right partition, a trigger function and a trigger are needed.
\n"},{"title":"Django Manager Method","date":"2016-04-24T16:00:01.000Z","_content":"\n\n\n#### Django Manager\n\nDjango 里会为每一个 model 生成一个 Manager,默认名字为 objects,一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理,QuerySet 方法是对数据库操作的封装。\n\neg.\n\n```python\nfrom django.db import models\n\nclass Person(models.Model):\n\t...\n\tpeople = models.Manager()\n```\n\n上面这个 model,`Person.objects`会产生一个`AttributeError`,但是`Person.people`就可以正常操作。因为默认的 manager 已经变成 people,objects 这个 manager 没有重新声明,不起作用。\n\n#### 自定义 Manager\n\n通常需要自定义 manager 的情况有两点:\n\n1. 需要修改/扩展 Django 的 manager 方法\n2. 需要修改返回的 QuerySet\n\n#### 默认 Manager\n\n如果使用自定义的 manager 需要注意的是,Django 将 model 中定义的第一个 manager 认为是默认 manager,而且 Django 框架中会用到默认 manager。\n\n笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager,并放在第一个。\n\neg.\n\n```python\nclass Book(models.Model):\n\ttitle = models.CharField(max_length=100)\n\tauthor = models.CharField(max_length=50)\n\t\n\tobjects = models.Manager() # default manager\n\tcustom_objects = CustomBOokManager() # custom manager\n```\n\n\n\n[source](http://blog.csdn.net/sicofield/article/details/49283751)\n\n\n\n","source":"_posts/Django-Manager-Method.md","raw":"---\ntitle: Django Manager Method\ndate: 2016-04-25 00:00:01\n---\n\n\n\n#### Django Manager\n\nDjango 里会为每一个 model 生成一个 Manager,默认名字为 objects,一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理,QuerySet 方法是对数据库操作的封装。\n\neg.\n\n```python\nfrom django.db import models\n\nclass Person(models.Model):\n\t...\n\tpeople = models.Manager()\n```\n\n上面这个 model,`Person.objects`会产生一个`AttributeError`,但是`Person.people`就可以正常操作。因为默认的 manager 已经变成 people,objects 这个 manager 没有重新声明,不起作用。\n\n#### 自定义 Manager\n\n通常需要自定义 manager 的情况有两点:\n\n1. 需要修改/扩展 Django 的 manager 方法\n2. 需要修改返回的 QuerySet\n\n#### 默认 Manager\n\n如果使用自定义的 manager 需要注意的是,Django 将 model 中定义的第一个 manager 认为是默认 manager,而且 Django 框架中会用到默认 manager。\n\n笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager,并放在第一个。\n\neg.\n\n```python\nclass Book(models.Model):\n\ttitle = models.CharField(max_length=100)\n\tauthor = models.CharField(max_length=50)\n\t\n\tobjects = models.Manager() # default manager\n\tcustom_objects = CustomBOokManager() # custom manager\n```\n\n\n\n[source](http://blog.csdn.net/sicofield/article/details/49283751)\n\n\n\n","slug":"Django-Manager-Method","published":1,"updated":"2018-05-15T14:58:35.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk52001bsubuwh45f7em","content":"Django 里会为每一个 model 生成一个 Manager,默认名字为 objects,一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理,QuerySet 方法是对数据库操作的封装。
\neg.
\n1 | from django.db import models |
上面这个 model,Person.objects会产生一个AttributeError,但是Person.people就可以正常操作。因为默认的 manager 已经变成 people,objects 这个 manager 没有重新声明,不起作用。
通常需要自定义 manager 的情况有两点:
\n如果使用自定义的 manager 需要注意的是,Django 将 model 中定义的第一个 manager 认为是默认 manager,而且 Django 框架中会用到默认 manager。
\n笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager,并放在第一个。
\neg.
\n1 | class Book(models.Model): |
Django 里会为每一个 model 生成一个 Manager,默认名字为 objects,一般情况下对 model 进行的处理都是通过 model.objects.XXX( ) 来进行的。其实是调用了 model 的 manager 的方法,而 manager 之中的方法是 QuerySet 方法的代理,QuerySet 方法是对数据库操作的封装。
\neg.
\n1 | from django.db import models |
上面这个 model,Person.objects会产生一个AttributeError,但是Person.people就可以正常操作。因为默认的 manager 已经变成 people,objects 这个 manager 没有重新声明,不起作用。
通常需要自定义 manager 的情况有两点:
\n如果使用自定义的 manager 需要注意的是,Django 将 model 中定义的第一个 manager 认为是默认 manager,而且 Django 框架中会用到默认 manager。
\n笨方法是使用自定义 manager 的时候,对于 model 依然提供 objects 这个默认 manager,并放在第一个。
\neg.
\n1 | class Book(models.Model): |
Create a folder named microblog (or whatever you want). Then cd into that folder and run following prompt in terminal:
1 | $ python3 -m venv flask |
Now you’ll have a folder named flask inside microblog, containing a private version of Python interpreter.
And you should install flask and extensions by the commands below:
\n1 | $ flask/bin/pip install flask |
After that, let’s create the basic structure for our application: app app/static app/templates tmp.
app — where the application package isstatic — stores static files like images, javascripts, and css.templates — where templates will go.Then you can start with __init__.py which should put into app folder (file app/__init__.py):
1 | from flask import Flask |
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.
\nLet’s see what a views function looks like (file app/views.py):
1 | from flask import Flask |
Finally we should create a script to starts up the web server with our application(file run.py):
1 | #!flask/bin/python |
To indicating that is an executable file you need to run this in terminal:
\n1 | $ chmod a+x run.py |
Now the file structure should look like:
\n1 | microblog\\ |
Then start to write the template (file app/templates/index.html):
1 | <html> |
Now let’s write the view function that uses this template (file app/views.py):
1 | from flask import render_template |
render_template function is what we import from Flask framework to render the template. It uses Jinja2 templating engine.
Create a folder named microblog (or whatever you want). Then cd into that folder and run following prompt in terminal:
1 | $ python3 -m venv flask |
Now you’ll have a folder named flask inside microblog, containing a private version of Python interpreter.
And you should install flask and extensions by the commands below:
\n1 | $ flask/bin/pip install flask |
After that, let’s create the basic structure for our application: app app/static app/templates tmp.
app — where the application package isstatic — stores static files like images, javascripts, and css.templates — where templates will go.Then you can start with __init__.py which should put into app folder (file app/__init__.py):
1 | from flask import Flask |
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.
\nLet’s see what a views function looks like (file app/views.py):
1 | from flask import Flask |
Finally we should create a script to starts up the web server with our application(file run.py):
1 | #!flask/bin/python |
To indicating that is an executable file you need to run this in terminal:
\n1 | $ chmod a+x run.py |
Now the file structure should look like:
\n1 | microblog\\ |
Then start to write the template (file app/templates/index.html):
1 | <html> |
Now let’s write the view function that uses this template (file app/views.py):
1 | from flask import render_template |
render_template function is what we import from Flask framework to render the template. It uses Jinja2 templating engine.
To handle web forms we use Flask-WTF . So we need to write a config file (file config.py):
1 | WTF_CSRF_ENABLED = True |
And then you need to use this config (file app/__init__.py):
1 | from flask import Flask |
Let’s build a simple form (file app/forms.app):
1 | from flask.ext.wtf import Form |
The DataRequired() is a validator that checks the field is empty or not.
After that, we need a HTML page to show the form (file app/templates/login.html):
1 | <!-- extend from base layout --> |
The final step is to code a view function that renders the template and receiving data from form (file app/views.py):
1 | from flask import render_template, flash, redirect |
To handle web forms we use Flask-WTF . So we need to write a config file (file config.py):
1 | WTF_CSRF_ENABLED = True |
And then you need to use this config (file app/__init__.py):
1 | from flask import Flask |
Let’s build a simple form (file app/forms.app):
1 | from flask.ext.wtf import Form |
The DataRequired() is a validator that checks the field is empty or not.
After that, we need a HTML page to show the form (file app/templates/login.html):
1 | <!-- extend from base layout --> |
The final step is to code a view function that renders the template and receiving data from form (file app/views.py):
1 | from flask import render_template, flash, redirect |
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.
\nUnderstanding the process of creating a resource.
\nAdd API URL in the urls.py of app.
\n
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.
\nThis dehydrate_field is uesd to modify field on the response JSON.
Dehydrate method is useful for aadding additional fields to bundle (response data).
\nSimilarly using hydrate method we can alter the bundle data which is generated from request at the time of PUT or POST methods.
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.
\nUnderstanding the process of creating a resource.
\nAdd API URL in the urls.py of app.
\n
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.
\nThis dehydrate_field is uesd to modify field on the response JSON.
Dehydrate method is useful for aadding additional fields to bundle (response data).
\nSimilarly using hydrate method we can alter the bundle data which is generated from request at the time of PUT or POST methods.
This is the very first post I wrote,
\n\n","site":{"data":{}},"excerpt":"","more":"This is the very first post I wrote,
\n\n"},{"title":"TastyPie Note 1","date":"2016-05-10T09:00:00.000Z","_content":"\n### Flow Through The Request/Response Cycle\n\nTastypie 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.\n\nWalking through what a GET request to a list endpoint looks like:\n\n- The `Resource.urls` are checked by Django's url resolvers.\n\n- On a match for the list view, `Resource.wrap_view('dispatch_list')` is called. `wrap_view` provides basic error handling & allows for returning serilized errors.\n\n- Because dispatch_list was passed to `wrap_view`, `Resource.dispatch_list` is called next. This is a thin wrapper around `Resource.dispatch`.\n\n- `dispatch` does a bunch of havy lifting. It ensures:\n\n - the requested HTTP method is in `allowed_methos` (`method_check`).\n - the class has a method that can handle the request(`get_list`)\n - the user is authenticated(`is_authenticated`)\n - the user has no exceeded their throttle(`throttle_check`).\n\n At this point, `dispatch` actually calls the requested method (`get_list`).\n\n- `get_list` does the actual work of API. It does:\n\n - A fetch of the available objects via `Resource.obj_get_list`. In the case of `ModelResource`, this builds the ORM filters to apply (`ModelResource.build_filters`). It then gets the `QuerySet` via `ModelResource.get_object_list` (which performs `Resource.authorized_read_list` to possibly limit the set the user can work with) and applies the built filters to it.\n - It then sorts the objects based on user input (`ModelResource.apply_sorting`).\n - Then it paginates the results using the supplied `Paginator` & pulls out the data to be serialized.\n - The objects in the page have `full_dehydrate` applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.\n - Finally, it calls `Resource.create_response`.\n\n- `create_response` is a shortcut method that:\n\n - Determines the desired response format (`Resource.determine_format`).\n - Serializes the data given to it in the proper format.\n - Returns a Django `HttpResponse` (200 OK) with the serialized data.\n\n- We bubble back up the call stack to `dispatch`. The last thing `dispatch` does is potentially store that a request occured for future throttling (`Resource.log_throttled_access`) then either returns the `HttpResponse` or wraps whatever data came back in a response (so Django doesn't freak out).\n\n\n\n","source":"_posts/TastyPie-Note-1.md","raw":"---\ntitle: TastyPie Note 1\ndate: 2016-05-10 17:00:00\n---\n\n### Flow Through The Request/Response Cycle\n\nTastypie 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.\n\nWalking through what a GET request to a list endpoint looks like:\n\n- The `Resource.urls` are checked by Django's url resolvers.\n\n- On a match for the list view, `Resource.wrap_view('dispatch_list')` is called. `wrap_view` provides basic error handling & allows for returning serilized errors.\n\n- Because dispatch_list was passed to `wrap_view`, `Resource.dispatch_list` is called next. This is a thin wrapper around `Resource.dispatch`.\n\n- `dispatch` does a bunch of havy lifting. It ensures:\n\n - the requested HTTP method is in `allowed_methos` (`method_check`).\n - the class has a method that can handle the request(`get_list`)\n - the user is authenticated(`is_authenticated`)\n - the user has no exceeded their throttle(`throttle_check`).\n\n At this point, `dispatch` actually calls the requested method (`get_list`).\n\n- `get_list` does the actual work of API. It does:\n\n - A fetch of the available objects via `Resource.obj_get_list`. In the case of `ModelResource`, this builds the ORM filters to apply (`ModelResource.build_filters`). It then gets the `QuerySet` via `ModelResource.get_object_list` (which performs `Resource.authorized_read_list` to possibly limit the set the user can work with) and applies the built filters to it.\n - It then sorts the objects based on user input (`ModelResource.apply_sorting`).\n - Then it paginates the results using the supplied `Paginator` & pulls out the data to be serialized.\n - The objects in the page have `full_dehydrate` applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.\n - Finally, it calls `Resource.create_response`.\n\n- `create_response` is a shortcut method that:\n\n - Determines the desired response format (`Resource.determine_format`).\n - Serializes the data given to it in the proper format.\n - Returns a Django `HttpResponse` (200 OK) with the serialized data.\n\n- We bubble back up the call stack to `dispatch`. The last thing `dispatch` does is potentially store that a request occured for future throttling (`Resource.log_throttled_access`) then either returns the `HttpResponse` or wraps whatever data came back in a response (so Django doesn't freak out).\n\n\n\n","slug":"TastyPie-Note-1","published":1,"updated":"2018-05-15T14:58:35.000Z","comments":1,"layout":"post","photos":[],"link":"","_id":"ck99fqk55001jsubuwea2oyll","content":"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.
\nWalking through what a GET request to a list endpoint looks like:
\nThe Resource.urls are checked by Django’s url resolvers.
On a match for the list view, Resource.wrap_view('dispatch_list') is called. wrap_view provides basic error handling & allows for returning serilized errors.
Because dispatch_list was passed to wrap_view, Resource.dispatch_list is called next. This is a thin wrapper around Resource.dispatch.
dispatch does a bunch of havy lifting. It ensures:
allowed_methos (method_check).get_list)is_authenticated)throttle_check).At this point, dispatch actually calls the requested method (get_list).
get_list does the actual work of API. It does:
Resource.obj_get_list. In the case of ModelResource, this builds the ORM filters to apply (ModelResource.build_filters). It then gets the QuerySet via ModelResource.get_object_list (which performs Resource.authorized_read_list to possibly limit the set the user can work with) and applies the built filters to it.ModelResource.apply_sorting).Paginator & pulls out the data to be serialized.full_dehydrate applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.Resource.create_response.create_response is a shortcut method that:
Resource.determine_format).HttpResponse (200 OK) with the serialized data.We bubble back up the call stack to dispatch. The last thing dispatch does is potentially store that a request occured for future throttling (Resource.log_throttled_access) then either returns the HttpResponse or wraps whatever data came back in a response (so Django doesn’t freak out).
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.
\nWalking through what a GET request to a list endpoint looks like:
\nThe Resource.urls are checked by Django’s url resolvers.
On a match for the list view, Resource.wrap_view('dispatch_list') is called. wrap_view provides basic error handling & allows for returning serilized errors.
Because dispatch_list was passed to wrap_view, Resource.dispatch_list is called next. This is a thin wrapper around Resource.dispatch.
dispatch does a bunch of havy lifting. It ensures:
allowed_methos (method_check).get_list)is_authenticated)throttle_check).At this point, dispatch actually calls the requested method (get_list).
get_list does the actual work of API. It does:
Resource.obj_get_list. In the case of ModelResource, this builds the ORM filters to apply (ModelResource.build_filters). It then gets the QuerySet via ModelResource.get_object_list (which performs Resource.authorized_read_list to possibly limit the set the user can work with) and applies the built filters to it.ModelResource.apply_sorting).Paginator & pulls out the data to be serialized.full_dehydrate applied to each of them, causing Tastypie to traslate the raw object data into the fields the endpoint supports.Resource.create_response.create_response is a shortcut method that:
Resource.determine_format).HttpResponse (200 OK) with the serialized data.We bubble back up the call stack to dispatch. The last thing dispatch does is potentially store that a request occured for future throttling (Resource.log_throttled_access) then either returns the HttpResponse or wraps whatever data came back in a response (so Django doesn’t freak out).