From 090773dc8a2d6f99582fbda0b40f934a8089399e Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Sat, 30 Jul 2022 10:48:56 +0530 Subject: [PATCH 1/9] fixed issue where it failed test command with "error: sequence, expected string" --- src/cache/mod.rs | 2 +- src/cache/models.rs | 6 +++--- src/cmds/edit.rs | 5 +---- src/plugins/leetcode.rs | 1 - 4 files changed, 5 insertions(+), 9 deletions(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 2eec037..1cc5967 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -81,7 +81,7 @@ impl Cache { if maybe_json.is_err() && self.is_session_bad().await { Err(Error::CookieError) } else { - Ok(maybe_json?) + Ok(maybe_json.unwrap()) } } diff --git a/src/cache/models.rs b/src/cache/models.rs index b338adf..c1d192d 100644 --- a/src/cache/models.rs +++ b/src/cache/models.rs @@ -255,8 +255,8 @@ pub struct VerifyResult { code_output: Vec, #[serde(default, deserialize_with = "ssr")] expected_output: Vec, - #[serde(default)] - std_output: String, + #[serde(default, deserialize_with = "ssr")] + std_output: Vec, // flatten // #[serde(flatten, default)] @@ -471,7 +471,7 @@ impl std::fmt::Display for VerifyResult { f, "{}{}", &"Stdout:".after_spaces(8).purple(), - &self.std_output.replace('\n', &"\n".after_spaces(15)) + &self.std_output[0].replace('\n', &"\n".after_spaces(15)) ) } else { write!(f, "") diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 039ad2e..a082cf7 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -101,10 +101,7 @@ impl Command for EditCommand { file_code.write_all((CODE_START.to_string() + "\n").as_bytes())?; file_code.write_all((d.code.to_string() + "\n").as_bytes())?; file_code.write_all((CODE_END.to_string() + "\n").as_bytes())?; - - if test_flag { - file_tests.write_all(question.all_cases.as_bytes())?; - } + file_tests.write_all(question.all_cases.as_bytes())?; } } diff --git a/src/plugins/leetcode.rs b/src/plugins/leetcode.rs index e06627f..90304aa 100644 --- a/src/plugins/leetcode.rs +++ b/src/plugins/leetcode.rs @@ -30,7 +30,6 @@ impl LeetCode { headers.insert(name.unwrap(), value.unwrap()); } - Ok(headers) } From bb3be23b3d81201c2b41b8d5a202c61149f363f8 Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Sat, 30 Jul 2022 23:33:38 +0530 Subject: [PATCH 2/9] add requested changes --- src/cache/mod.rs | 2 +- src/cmds/edit.rs | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 1cc5967..2eec037 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -81,7 +81,7 @@ impl Cache { if maybe_json.is_err() && self.is_session_bad().await { Err(Error::CookieError) } else { - Ok(maybe_json.unwrap()) + Ok(maybe_json?) } } diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index a082cf7..d70129b 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -101,6 +101,8 @@ impl Command for EditCommand { file_code.write_all((CODE_START.to_string() + "\n").as_bytes())?; file_code.write_all((d.code.to_string() + "\n").as_bytes())?; file_code.write_all((CODE_END.to_string() + "\n").as_bytes())?; + } + if test_flag { file_tests.write_all(question.all_cases.as_bytes())?; } } From 10c4e52056d78ca2ed25940ad9e0f8d47340223b Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Sat, 30 Jul 2022 23:37:26 +0530 Subject: [PATCH 3/9] code cleanup --- src/cmds/edit.rs | 1 + src/plugins/leetcode.rs | 1 + 2 files changed, 2 insertions(+) diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index d70129b..51aa440 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -102,6 +102,7 @@ impl Command for EditCommand { file_code.write_all((d.code.to_string() + "\n").as_bytes())?; file_code.write_all((CODE_END.to_string() + "\n").as_bytes())?; } + if test_flag { file_tests.write_all(question.all_cases.as_bytes())?; } diff --git a/src/plugins/leetcode.rs b/src/plugins/leetcode.rs index 90304aa..e06627f 100644 --- a/src/plugins/leetcode.rs +++ b/src/plugins/leetcode.rs @@ -30,6 +30,7 @@ impl LeetCode { headers.insert(name.unwrap(), value.unwrap()); } + Ok(headers) } From 6b426c992e4e2dc13b407fa9e2542a1e53783e91 Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Sat, 30 Jul 2022 23:41:01 +0530 Subject: [PATCH 4/9] fixed some more mistakes --- src/cmds/edit.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 51aa440..039ad2e 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -101,10 +101,10 @@ impl Command for EditCommand { file_code.write_all((CODE_START.to_string() + "\n").as_bytes())?; file_code.write_all((d.code.to_string() + "\n").as_bytes())?; file_code.write_all((CODE_END.to_string() + "\n").as_bytes())?; - } - if test_flag { - file_tests.write_all(question.all_cases.as_bytes())?; + if test_flag { + file_tests.write_all(question.all_cases.as_bytes())?; + } } } From 3281434f890cd67d262270ac8da5085585b7ec86 Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Mon, 19 Sep 2022 13:53:37 +0530 Subject: [PATCH 5/9] create dev branch --- Cargo.toml | 12 ++++----- README.md | 2 +- src/cache/mod.rs | 15 ++++++++---- src/cache/models.rs | 21 +++++++++------- src/cfg.rs | 35 ++++++++++++++++++++------ src/cmds/edit.rs | 48 +++++++++++++++++++++--------------- src/cmds/mod.rs | 2 -- src/err.rs | 33 ++++++++++++++++++------- src/helper.rs | 57 ++++++++++++++++++++++++++++++++++++++----- src/plugins/chrome.rs | 4 +-- 10 files changed, 161 insertions(+), 68 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 9b37f4a..3ab4163 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,20 +17,20 @@ readme = './README.md' [dependencies] async-trait = "0.1.56" -tokio = { version = "1", features = ["full"] } -clap = { version = "3.2.8", features = ["cargo"] } +tokio = { version = "1.19.2", features = ["full"] } +clap = { version = "3.2.10", features = ["cargo"] } colored = "2.0.0" dirs = "4.0.0" env_logger = "0.9.0" -keyring = "1.1.2" +keyring = "1.2.0" log = "0.4.17" -openssl = "0.10.40" +openssl = "0.10.41" pyo3 = { version = "0.16.5", optional = true } rand = "0.8.5" -serde = { version = "1.0.138", features = ["derive"] } +serde = { version = "1.0.139", features = ["derive"] } serde_json = "1.0.82" toml = "0.5.9" -regex = "1.5.6" +regex = "1.6.0" scraper = "0.13.0" [dependencies.diesel] diff --git a/README.md b/README.md index faa092c..d7a3517 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ SUBCOMMANDS: ## Example -For example, given this config (can be found in `~/.leetcode/leetcode.toml`): +For example, given this config (can be found in `~/.leetcode/leetcode.toml`, it can be generated automatically with command: `leetcode list` if you are a new user): ```toml [code] diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 2eec037..345b3ea 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -6,7 +6,6 @@ mod sql; use self::models::*; use self::schemas::{problems::dsl::*, tags::dsl::*}; use self::sql::*; -use crate::cmds::{CODE_END, CODE_START}; use crate::helper::test_cases_path; use crate::{cfg, err::Error, plugins::LeetCode}; use colored::Colorize; @@ -285,10 +284,16 @@ impl Cache { File::open(code_path(&p, None)?)?.read_to_string(&mut code)?; - let begin = code.find(CODE_START).unwrap_or(0); - let end = code.find(CODE_END).unwrap_or(code.len()); - let code = if let Some(solution) = code.get(begin..end) { - solution.to_string() + let code = if conf.code.edit_code_marker { + let begin = code.find(&conf.code.start_marker); + let end = code.find(&conf.code.end_marker); + if let (Some(l), Some(r)) = (begin, end) { + code.get((l + conf.code.start_marker.len())..r) + .map(|s| s.to_string()) + .unwrap_or_else(|| code) + } else { + code + } } else { code }; diff --git a/src/cache/models.rs b/src/cache/models.rs index c1d192d..0a9a6ab 100644 --- a/src/cache/models.rs +++ b/src/cache/models.rs @@ -39,11 +39,12 @@ impl Problem { _ => "Unknown", } } - pub fn desc_comment(&self) -> String { + pub fn desc_comment(&self, conf: &Config) -> String { let mut res = String::new(); - res += format!("// Category: {}\n", self.category).as_str(); - res += format!("// Level: {}\n", self.display_level(),).as_str(); - res += format!("// Percent: {}%\n\n", self.percent).as_str(); + let comment_leading = &conf.code.comment_leading; + res += format!("{} Category: {}\n", comment_leading, self.category).as_str(); + res += format!("{} Level: {}\n", comment_leading, self.display_level(),).as_str(); + res += format!("{} Percent: {}%\n\n", comment_leading, self.percent).as_str(); res + "\n" } @@ -148,13 +149,13 @@ impl Question { self.content.render() } - pub fn desc_comment(&self) -> String { + pub fn desc_comment(&self, conf: &Config) -> String { let desc = self.content.render(); - let mut res = desc - .lines() - .fold("/*\n".to_string(), |acc, e| acc + " * " + e + "\n"); - res += " */\n"; + let mut res = desc.lines().fold("\n".to_string(), |acc, e| { + acc + " " + conf.code.comment_leading.as_str() + " " + e + "\n" + }); + res += " \n"; res } @@ -481,7 +482,9 @@ impl std::fmt::Display for VerifyResult { } } +use crate::Config; use verify::*; + mod verify { use super::super::parser::ssr; use serde::Deserialize; diff --git a/src/cfg.rs b/src/cfg.rs index 21079e4..184e3e0 100644 --- a/src/cfg.rs +++ b/src/cfg.rs @@ -9,7 +9,7 @@ use crate::Error; use serde::{Deserialize, Serialize}; use std::{collections::HashMap, fs, path::PathBuf}; -const DEFAULT_CONFIG: &str = r##" +pub const DEFAULT_CONFIG: &str = r##" # usually you don't wanna change those [sys] categories = [ @@ -57,6 +57,12 @@ favorite_delete = "https://leetcode.com/list/api/questions/$hash/$id" [code] editor = "vim" lang = "rust" +edit_code_marker = false +comment_problem_desc = false +comment_leading = "///" +start_marker = "@lc code=start" +end_marker = "@lc code=start" +test = true pick = "${fid}.${slug}" submission = "${fid}.${slug}.${sid}.${ac}" @@ -66,10 +72,10 @@ session = "" [storage] root = "~/.leetcode" -cache = "Problems" scripts = "scripts" -# absolutely path for the code, other use root as parent dir code = "code" +# absolutely path for the cache, other use root as parent dir +cache = "~/.cache/leetcode" "##; /// Sync with `~/.leetcode/config.toml` @@ -134,6 +140,12 @@ pub struct Code { pub editor: String, #[serde(rename(serialize = "editor-args", deserialize = "editor-args"))] pub editor_args: Option>, + pub edit_code_marker: bool, + pub start_marker: String, + pub end_marker: String, + pub comment_problem_desc: bool, + pub comment_leading: String, + pub test: bool, pub lang: String, pub pick: String, pub submission: String, @@ -163,16 +175,23 @@ impl Storage { /// get cache path pub fn cache(&self) -> Result { - let root = &self.root()?; - Ok(PathBuf::from(root) - .join(&self.cache) + let home = dirs::home_dir() + .ok_or(Error::NoneError)? .to_string_lossy() - .to_string()) + .to_string(); + let path = PathBuf::from(self.cache.replace('~', &home)); + if !path.is_dir() { + info!("Generate cache dir at {:?}.", &path); + fs::DirBuilder::new().recursive(true).create(&path)?; + } + + Ok(path.join("Problems").to_string_lossy().to_string()) } /// get code path pub fn code(&self) -> Result { - let p = PathBuf::from(&self.code); + let root = &self.root()?; + let p = PathBuf::from(root).join(&self.code); if !PathBuf::from(&p).exists() { fs::create_dir(&p)? } diff --git a/src/cmds/edit.rs b/src/cmds/edit.rs index 039ad2e..a6ec981 100644 --- a/src/cmds/edit.rs +++ b/src/cmds/edit.rs @@ -22,9 +22,6 @@ use clap::{Arg, ArgMatches, Command as ClapCommand}; /// ``` pub struct EditCommand; -pub const CODE_START: &str = r#"// @lc code=start"#; -pub const CODE_END: &str = r#"// @lc code=end"#; - #[async_trait] impl Command for EditCommand { /// `edit` usage @@ -45,13 +42,6 @@ impl Command for EditCommand { .required(true) .help("question id"), ) - .arg( - Arg::with_name("test") - .long("test") - .short('t') - .required(false) - .help("write test file"), - ) } /// `edit` handler @@ -66,16 +56,16 @@ impl Command for EditCommand { let problem = cache.get_problem(id)?; let mut conf = cache.to_owned().0.conf; - let test_flag = m.contains_id("test"); + let test_flag = conf.code.test; - let p_desc_comment = problem.desc_comment(); + let p_desc_comment = problem.desc_comment(&conf); // condition language if m.contains_id("lang") { conf.code.lang = m.value_of("lang").ok_or(Error::NoneError)?.to_string(); conf.sync()?; } - let lang = conf.code.lang; + let lang = &conf.code.lang; let path = crate::helper::code_path(&problem, Some(lang.to_owned()))?; if !Path::new(&path).exists() { @@ -87,22 +77,40 @@ impl Command for EditCommand { let question: Question = qr?; let mut file_code = File::create(&path)?; - let question_desc = question.desc_comment() + "\n"; + let question_desc = question.desc_comment(&conf) + "\n"; let test_path = crate::helper::test_cases_path(&problem)?; - let mut file_tests = File::create(&test_path)?; let mut flag = false; for d in question.defs.0 { - if d.value == lang { + if d.value == *lang { flag = true; - file_code.write_all(p_desc_comment.as_bytes())?; - file_code.write_all(question_desc.as_bytes())?; - file_code.write_all((CODE_START.to_string() + "\n").as_bytes())?; + if conf.code.comment_problem_desc { + file_code.write_all(p_desc_comment.as_bytes())?; + file_code.write_all(question_desc.as_bytes())?; + } + if conf.code.edit_code_marker { + file_code.write_all( + (conf.code.comment_leading.clone() + + " " + + &conf.code.start_marker + + "\n") + .as_bytes(), + )?; + } file_code.write_all((d.code.to_string() + "\n").as_bytes())?; - file_code.write_all((CODE_END.to_string() + "\n").as_bytes())?; + if conf.code.edit_code_marker { + file_code.write_all( + (conf.code.comment_leading.clone() + + " " + + &conf.code.end_marker + + "\n") + .as_bytes(), + )?; + } if test_flag { + let mut file_tests = File::create(&test_path)?; file_tests.write_all(question.all_cases.as_bytes())?; } } diff --git a/src/cmds/mod.rs b/src/cmds/mod.rs index 30e5841..58a9dc3 100644 --- a/src/cmds/mod.rs +++ b/src/cmds/mod.rs @@ -38,5 +38,3 @@ pub use list::ListCommand; pub use pick::PickCommand; pub use stat::StatCommand; pub use test::TestCommand; - -pub use edit::{CODE_END, CODE_START}; diff --git a/src/err.rs b/src/err.rs index 3ae8cc1..344cf1b 100644 --- a/src/err.rs +++ b/src/err.rs @@ -1,4 +1,5 @@ //! Errors in leetcode-cli +use crate::cfg::{root, DEFAULT_CONFIG}; use crate::cmds::{Command, DataCommand}; use colored::Colorize; use std::fmt; @@ -18,6 +19,7 @@ pub enum Error { DecryptError, SilentError, NoneError, + ChromeNotLogin, } impl std::fmt::Debug for Error { @@ -56,7 +58,8 @@ impl std::fmt::Debug for Error { Error::NoneError => write!(f, "json from response parse failed, please open a new issue at: {}.", "https://github.com/clearloop/leetcode-cli/".underline(), - ) + ), + Error::ChromeNotLogin => write!(f, "maybe you not login on the Chrome, you can login and retry.") } } } @@ -98,19 +101,31 @@ impl std::convert::From for Error { // toml impl std::convert::From for Error { - fn from(err: toml::de::Error) -> Self { - Error::ParseError(format!( - "{}, {}{}{}{}{}{}{}{}", - err, + fn from(_err: toml::de::Error) -> Self { + let conf = root().unwrap().join("leetcode_tmp.toml"); + std::fs::write(&conf, &DEFAULT_CONFIG[1..]).unwrap(); + #[cfg(debug_assertions)] + let err_msg = format!( + "{}, {}{}{}{}{}{}", + _err, + "Parse config file failed, ", + "leetcode-cli has just generated a new leetcode.toml at ", + "~/.leetcode/leetcode_tmp.toml,".green().bold().underline(), + " the current one at ", + "~/.leetcode/leetcode.toml".yellow().bold().underline(), + " seems missing some keys, Please compare the new file and add the missing keys.\n", + ); + #[cfg(not(debug_assertions))] + let err_msg = format!( + "{}{}{}{}{}{}", "Parse config file failed, ", "leetcode-cli has just generated a new leetcode.toml at ", "~/.leetcode/leetcode_tmp.toml,".green().bold().underline(), " the current one at ", "~/.leetcode/leetcode.toml".yellow().bold().underline(), - "seems missing some keys, please compare to ", - "the tmp one and add them up : )\n", - ".", - )) + " seems missing some keys, Please compare the new file and add the missing keys.\n", + ); + Error::ParseError(err_msg) } } diff --git a/src/helper.rs b/src/helper.rs index c4a70a3..60efcd8 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -90,9 +90,46 @@ mod filter { } } +pub fn superscript(n: u8) -> String { + match n { + x if x >= 10 => format!("{}{}", superscript(n / 10), superscript(n % 10)), + 0 => "⁰".to_string(), + 1 => "¹".to_string(), + 2 => "²".to_string(), + 3 => "³".to_string(), + 4 => "⁴".to_string(), + 5 => "⁵".to_string(), + 6 => "⁶".to_string(), + 7 => "⁷".to_string(), + 8 => "⁸".to_string(), + 9 => "⁹".to_string(), + _ => n.to_string(), + } +} + +pub fn subscript(n: u8) -> String { + match n { + x if x >= 10 => format!("{}{}", subscript(n / 10), subscript(n % 10)), + 0 => "₀".to_string(), + 1 => "₁".to_string(), + 2 => "₂".to_string(), + 3 => "₃".to_string(), + 4 => "₄".to_string(), + 5 => "₅".to_string(), + 6 => "₆".to_string(), + 7 => "₇".to_string(), + 8 => "₈".to_string(), + 9 => "₉".to_string(), + _ => n.to_string(), + } +} + /// Render html to command-line mod html { + use crate::helper::{subscript, superscript}; + use regex::Captures; use scraper::Html; + /// Html render plugin pub trait HTML { fn render(&self) -> String; @@ -100,12 +137,20 @@ mod html { impl HTML for String { fn render(&self) -> String { - let rep = self - .replace(r#""#, "") - .replace(r#""#, "^") - .replace(r#""#, "") - .replace(r#""#, "_"); - let frag = Html::parse_fragment(rep.as_str()); + let sup_re = regex::Regex::new(r"(?P[0-9]*)").unwrap(); + let sub_re = regex::Regex::new(r"(?P[0-9]*)").unwrap(); + + let res = sup_re.replace_all(self, |cap: &Captures| { + let num: u8 = cap["num"].to_string().parse().unwrap(); + superscript(num) + }); + + let res = sub_re.replace_all(&res, |cap: &Captures| { + let num: u8 = cap["num"].to_string().parse().unwrap(); + subscript(num) + }); + + let frag = Html::parse_fragment(&res); let res = frag .root_element() diff --git a/src/plugins/chrome.rs b/src/plugins/chrome.rs index 20ba976..8893999 100644 --- a/src/plugins/chrome.rs +++ b/src/plugins/chrome.rs @@ -85,10 +85,10 @@ pub fn cookies() -> Result { } Ok(Ident { - csrf: m.get("csrftoken").ok_or(Error::NoneError)?.to_string(), + csrf: m.get("csrftoken").ok_or(Error::ChromeNotLogin)?.to_string(), session: m .get("LEETCODE_SESSION") - .ok_or(Error::NoneError)? + .ok_or(Error::ChromeNotLogin)? .to_string(), }) } From 92bc67e4ac94f2b96e7d0bba9824cc5915a35efb Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Sat, 30 Jul 2022 10:48:56 +0530 Subject: [PATCH 6/9] fixed issue where it failed test command with "error: sequence, expected string" --- code/1.two-sum.rs | 69 ++++++++++++++++ code/1.two-sum.tests.dat | 0 code/100.same-tree.rs | 65 +++++++++++++++ code/100.same-tree.tests.dat | 0 code/143.reorder-list.rs | 64 +++++++++++++++ code/143.reorder-list.tests.dat | 2 + code/150.evaluate-reverse-polish-notation.rs | 79 +++++++++++++++++++ ...evaluate-reverse-polish-notation.tests.dat | 3 + ...53.find-minimum-in-rotated-sorted-array.rs | 61 ++++++++++++++ ...-minimum-in-rotated-sorted-array.tests.dat | 0 ...find-minimum-in-rotated-sorted-array-ii.rs | 51 ++++++++++++ ...nimum-in-rotated-sorted-array-ii.tests.dat | 0 ...intersection-of-two-linked-lists.tests.dat | 0 leetcode.toml | 59 ++++++++++++++ src/cache/mod.rs | 2 +- src/plugins/leetcode.rs | 1 - 16 files changed, 454 insertions(+), 2 deletions(-) create mode 100644 code/1.two-sum.rs create mode 100644 code/1.two-sum.tests.dat create mode 100644 code/100.same-tree.rs create mode 100644 code/100.same-tree.tests.dat create mode 100644 code/143.reorder-list.rs create mode 100644 code/143.reorder-list.tests.dat create mode 100644 code/150.evaluate-reverse-polish-notation.rs create mode 100644 code/150.evaluate-reverse-polish-notation.tests.dat create mode 100644 code/153.find-minimum-in-rotated-sorted-array.rs create mode 100644 code/153.find-minimum-in-rotated-sorted-array.tests.dat create mode 100644 code/154.find-minimum-in-rotated-sorted-array-ii.rs create mode 100644 code/154.find-minimum-in-rotated-sorted-array-ii.tests.dat create mode 100644 code/160.intersection-of-two-linked-lists.tests.dat create mode 100644 leetcode.toml diff --git a/code/1.two-sum.rs b/code/1.two-sum.rs new file mode 100644 index 0000000..b485c52 --- /dev/null +++ b/code/1.two-sum.rs @@ -0,0 +1,69 @@ +// Category: algorithms +// Level: Easy +// Percent: 48.887383% + + +/* + * Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. + * + * You may assume that each input would have exactly one solution, and you may not use the same element twice. + * + * You can return the answer in any order. + * + *   + * Example 1: + * + * Input: nums = [2,7,11,15], target = 9 + * Output: [0,1] + * Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. + * + * + * Example 2: + * + * Input: nums = [3,2,4], target = 6 + * Output: [1,2] + * + * + * Example 3: + * + * Input: nums = [3,3], target = 6 + * Output: [0,1] + * + * + *   + * Constraints: + * + * + * 2 <= nums.length <= 10^4 + * -10^9 <= nums[i] <= 10^9 + * -10^9 <= target <= 10^9 + * Only one valid answer exists. + * + * + *   + * Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity? + */ + +// @lc code=start +impl Solution { + pub fn two_sum(nums: Vec, target: i32) -> Vec { + let mut ans: Vec = vec![]; + let size = nums.len(); + for i in 0..size { + let j = target - nums[i]; + let index = match nums.iter().position(|&r| r == j) { + Some(x) => x, + _ => usize::MAX + }; + if (index != i) && (index != usize::MAX) { + ans.push(i as i32); + ans.push(index as i32); + break; + } else { + continue; + } + } + ans + } +} +// @lc code=end diff --git a/code/1.two-sum.tests.dat b/code/1.two-sum.tests.dat new file mode 100644 index 0000000..e69de29 diff --git a/code/100.same-tree.rs b/code/100.same-tree.rs new file mode 100644 index 0000000..d1e2636 --- /dev/null +++ b/code/100.same-tree.rs @@ -0,0 +1,65 @@ +// Category: algorithms +// Level: Easy +// Percent: 55.96335% + + +/* + * Given the roots of two binary trees p and q, write a function to check if they are the same or not. + * + * Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. + * + *   + * Example 1: + * + * Input: p = [1,2,3], q = [1,2,3] + * Output: true + * + * + * Example 2: + * + * Input: p = [1,2], q = [1,null,2] + * Output: false + * + * + * Example 3: + * + * Input: p = [1,2,1], q = [1,1,2] + * Output: false + * + * + *   + * Constraints: + * + * + * The number of nodes in both trees is in the range [0, 100]. + * -10^4 <= Node.val <= 10^4 + * + */ + +// @lc code=start +// Definition for a binary tree node. +// #[derive(Debug, PartialEq, Eq)] +// pub struct TreeNode { +// pub val: i32, +// pub left: Option>>, +// pub right: Option>>, +// } +// +// impl TreeNode { +// #[inline] +// pub fn new(val: i32) -> Self { +// TreeNode { +// val, +// left: None, +// right: None +// } +// } +// } +use std::rc::Rc; +use std::cell::RefCell; +impl Solution { + pub fn is_same_tree(p: Option>>, q: Option>>) -> bool { + + } +} +// @lc code=end diff --git a/code/100.same-tree.tests.dat b/code/100.same-tree.tests.dat new file mode 100644 index 0000000..e69de29 diff --git a/code/143.reorder-list.rs b/code/143.reorder-list.rs new file mode 100644 index 0000000..2b7a426 --- /dev/null +++ b/code/143.reorder-list.rs @@ -0,0 +1,64 @@ +// Category: algorithms +// Level: Medium +// Percent: 49.535244% + + +/* + * You are given the head of a singly linked-list. The list can be represented as: + * + * L_0 → L_1 → … → L_n - 1 → L_n + * + * + * Reorder the list to be on the following form: + * + * L_0 → L_n → L_1 → L_n - 1 → L_2 → L_n - 2 → … + * + * + * You may not modify the values in the list's nodes. Only nodes themselves may be changed. + * + *   + * Example 1: + * + * Input: head = [1,2,3,4] + * Output: [1,4,2,3] + * + * + * Example 2: + * + * Input: head = [1,2,3,4,5] + * Output: [1,5,2,4,3] + * + * + *   + * Constraints: + * + * + * The number of nodes in the list is in the range [1, 5 * 10^4]. + * 1 <= Node.val <= 1000 + * + */ + +// @lc code=start +// Definition for singly-linked list. +// #[derive(PartialEq, Eq, Clone, Debug)] +// pub struct ListNode { +// pub val: i32, +// pub next: Option> +// } +// +// impl ListNode { +// #[inline] +// fn new(val: i32) -> Self { +// ListNode { +// next: None, +// val +// } +// } +// } +impl Solution { + pub fn reorder_list(head: &mut Option>) { + + + } +} +// @lc code=end diff --git a/code/143.reorder-list.tests.dat b/code/143.reorder-list.tests.dat new file mode 100644 index 0000000..affbdf0 --- /dev/null +++ b/code/143.reorder-list.tests.dat @@ -0,0 +1,2 @@ +[1,2,3,4] +[1,2,3,4,5] \ No newline at end of file diff --git a/code/150.evaluate-reverse-polish-notation.rs b/code/150.evaluate-reverse-polish-notation.rs new file mode 100644 index 0000000..9d5a9aa --- /dev/null +++ b/code/150.evaluate-reverse-polish-notation.rs @@ -0,0 +1,79 @@ +// Category: algorithms +// Level: Medium +// Percent: 43.61331% + + +/* + * Evaluate the value of an arithmetic expression in Reverse Polish Notation. + * + * Valid operators are +, -, *, and /. Each operand may be an integer or another expression. + * + * Note that division between two integers should truncate toward zero. + * + * It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation. + * + *   + * Example 1: + * + * Input: tokens = ["2","1","+","3","*"] + * Output: 9 + * Explanation: ((2 + 1) * 3) = 9 + * + * + * Example 2: + * + * Input: tokens = ["4","13","5","/","+"] + * Output: 6 + * Explanation: (4 + (13 / 5)) = 6 + * + * + * Example 3: + * + * Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"] + * Output: 22 + * Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 + * = ((10 * (6 / (12 * -11))) + 17) + 5 + * = ((10 * (6 / -132)) + 17) + 5 + * = ((10 * 0) + 17) + 5 + * = (0 + 17) + 5 + * = 17 + 5 + * = 22 + * + * + *   + * Constraints: + * + * + * 1 <= tokens.length <= 10^4 + * tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200]. + * + */ + +// @lc code=start +impl Solution { + pub fn eval_rpn(tokens: Vec) -> i32 { + use std::collections::VecDeque; + let mut ss = VecDeque::new(); + for i in tokens { + if i=="+" || i == "-" || i=="*" || i=="/" { + let op: Vec = i.chars().collect(); + let op1 = *ss.back().unwrap(); ss.pop_back(); + let op2 = *ss.back().unwrap(); ss.pop_back(); + let ans = match op[0] { + '+' => op1+op2, + '-' => op2-op1, + '/' => op2/op1, + '*' => op1*op2, + _ => 0, + }; + ss.push_back(ans); + } + + else { + ss.push_back(i.parse().unwrap()); + } + } + *ss.back().unwrap() + 100 + } +} +// @lc code=end diff --git a/code/150.evaluate-reverse-polish-notation.tests.dat b/code/150.evaluate-reverse-polish-notation.tests.dat new file mode 100644 index 0000000..3e083b1 --- /dev/null +++ b/code/150.evaluate-reverse-polish-notation.tests.dat @@ -0,0 +1,3 @@ +["2","1","+","3","*"] +["4","13","5","/","+"] +["10","6","9","3","+","-11","*","/","*","17","+","5","+"] \ No newline at end of file diff --git a/code/153.find-minimum-in-rotated-sorted-array.rs b/code/153.find-minimum-in-rotated-sorted-array.rs new file mode 100644 index 0000000..4dd42a4 --- /dev/null +++ b/code/153.find-minimum-in-rotated-sorted-array.rs @@ -0,0 +1,61 @@ +// Category: algorithms +// Level: Medium +// Percent: 48.2175% + + +/* + * Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: + * + * + * [4,5,6,7,0,1,2] if it was rotated 4 times. + * [0,1,2,4,5,6,7] if it was rotated 7 times. + * + * + * Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. + * + * Given the sorted rotated array nums of unique elements, return the minimum element of this array. + * + * You must write an algorithm that runs in O(log n) time. + * + *   + * Example 1: + * + * Input: nums = [3,4,5,1,2] + * Output: 1 + * Explanation: The original array was [1,2,3,4,5] rotated 3 times. + * + * + * Example 2: + * + * Input: nums = [4,5,6,7,0,1,2] + * Output: 0 + * Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. + * + * + * Example 3: + * + * Input: nums = [11,13,15,17] + * Output: 11 + * Explanation: The original array was [11,13,15,17] and it was rotated 4 times. + * + * + *   + * Constraints: + * + * + * n == nums.length + * 1 <= n <= 5000 + * -5000 <= nums[i] <= 5000 + * All the integers of nums are unique. + * nums is sorted and rotated between 1 and n times. + * + */ + +// @lc code=start +impl Solution { + pub fn find_min(nums: Vec) -> i32 { + + 7743 + } +} +// @lc code=end diff --git a/code/153.find-minimum-in-rotated-sorted-array.tests.dat b/code/153.find-minimum-in-rotated-sorted-array.tests.dat new file mode 100644 index 0000000..e69de29 diff --git a/code/154.find-minimum-in-rotated-sorted-array-ii.rs b/code/154.find-minimum-in-rotated-sorted-array-ii.rs new file mode 100644 index 0000000..bd90a41 --- /dev/null +++ b/code/154.find-minimum-in-rotated-sorted-array-ii.rs @@ -0,0 +1,51 @@ +// Category: algorithms +// Level: Hard +// Percent: 43.299023% + + +/* + * Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become: + * + * + * [4,5,6,7,0,1,4] if it was rotated 4 times. + * [0,1,4,4,5,6,7] if it was rotated 7 times. + * + * + * Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. + * + * Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array. + * + * You must decrease the overall operation steps as much as possible. + * + *   + * Example 1: + * Input: nums = [1,3,5] + * Output: 1 + * Example 2: + * Input: nums = [2,2,2,0,1] + * Output: 0 + * + *   + * Constraints: + * + * + * n == nums.length + * 1 <= n <= 5000 + * -5000 <= nums[i] <= 5000 + * nums is sorted and rotated between 1 and n times. + * + * + *   + * Follow up: This problem is similar to Find Minimum in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why? + * + *   + */ + +// @lc code=start +impl Solution { + pub fn find_min(nums: Vec) -> i32 { + + 432 + } +} +// @lc code=end diff --git a/code/154.find-minimum-in-rotated-sorted-array-ii.tests.dat b/code/154.find-minimum-in-rotated-sorted-array-ii.tests.dat new file mode 100644 index 0000000..e69de29 diff --git a/code/160.intersection-of-two-linked-lists.tests.dat b/code/160.intersection-of-two-linked-lists.tests.dat new file mode 100644 index 0000000..e69de29 diff --git a/leetcode.toml b/leetcode.toml new file mode 100644 index 0000000..0b03d68 --- /dev/null +++ b/leetcode.toml @@ -0,0 +1,59 @@ +# usually you don't wanna change those +[sys] +categories = [ + "algorithms", + "concurrency", + "database", + "shell" +] + +langs = [ + "bash", + "c", + "cpp", + "csharp", + "golang", + "java", + "javascript", + "kotlin", + "mysql", + "php", + "python", + "python3", + "ruby", + "rust", + "scala", + "swift" +] + +[sys.urls] +base = "https://leetcode.com" +graphql = "https://leetcode.com/graphql" +login = "https://leetcode.com/accounts/login/" +problems = "https://leetcode.com/api/problems/$category/" +problem = "https://leetcode.com/problems/$slug/description/" +tag = "https://leetcode.com/tag/$slug/" +test = "https://leetcode.com/problems/$slug/interpret_solution/" +session = "https://leetcode.com/session/" +submit = "https://leetcode.com/problems/$slug/submit/" +submissions = "https://leetcode.com/api/submissions/$slug" +submission = "https://leetcode.com/submissions/detail/$id/" +verify = "https://leetcode.com/submissions/detail/$id/check/" +favorites = "https://leetcode.com/list/api/questions" +favorite_delete = "https://leetcode.com/list/api/questions/$hash/$id" + +[code] +editor = "neovide" +lang = "rust" +pick = "${fid}.${slug}" +submission = "${fid}.${slug}.${sid}.${ac}" + +[cookies] +csrf = "NNahhfgjeBNVp09BYk9T8uBuUGHW1JjpURKvklAF3S1w9jEJdyHxHihwz9vURc4w" +session = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfYXV0aF91c2VyX2lkIjoiNDczNzc2MiIsIl9hdXRoX3VzZXJfYmFja2VuZCI6ImFsbGF1dGguYWNjb3VudC5hdXRoX2JhY2tlbmRzLkF1dGhlbnRpY2F0aW9uQmFja2VuZCIsIl9hdXRoX3VzZXJfaGFzaCI6IjBiZGJhOGI2NjYwZDIyZTdmMjY4MGY4M2U3YjYyNTY0NmJiNDAyNTgiLCJpZCI6NDczNzc2MiwiZW1haWwiOiJwbWFuYW5rMjAwNUBnbWFpbC5jb20iLCJ1c2VybmFtZSI6Im1hbmFuazIwIiwidXNlcl9zbHVnIjoibWFuYW5rMjAiLCJhdmF0YXIiOiJodHRwczovL2Fzc2V0cy5sZWV0Y29kZS5jb20vdXNlcnMvbWFuYW5rMjAvYXZhdGFyXzE2Mjg5MjIyNTMucG5nIiwicmVmcmVzaGVkX2F0IjoxNjU5MDc2Mzc2LCJpcCI6IjQzLjI1MC4xNjUuMTg1IiwiaWRlbnRpdHkiOiI3NzQ0N2U5MTY4MTY2OWI1ZmI3MjE1YTYyNDEzOWJkNiIsInNlc3Npb25faWQiOjI1MjE2ODc1fQ.5h0o96lmxqUFc1vY9-JkM-FvoIw_lJ6F2OmJTVAF0K0" + +[storage] +cache = "Problems" +code = "code" +root = "~/.leetcode" +scripts = "scripts" diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 345b3ea..184acd3 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -80,7 +80,7 @@ impl Cache { if maybe_json.is_err() && self.is_session_bad().await { Err(Error::CookieError) } else { - Ok(maybe_json?) + Ok(maybe_json.unwrap()) } } diff --git a/src/plugins/leetcode.rs b/src/plugins/leetcode.rs index e06627f..90304aa 100644 --- a/src/plugins/leetcode.rs +++ b/src/plugins/leetcode.rs @@ -30,7 +30,6 @@ impl LeetCode { headers.insert(name.unwrap(), value.unwrap()); } - Ok(headers) } From 3cfe4c9ad392e0342e22fee92c01df0774951648 Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Sat, 30 Jul 2022 23:33:38 +0530 Subject: [PATCH 7/9] add requested changes --- code/1.two-sum.rs | 69 ---------------- code/1.two-sum.tests.dat | 0 code/100.same-tree.rs | 65 --------------- code/100.same-tree.tests.dat | 0 code/143.reorder-list.rs | 64 --------------- code/143.reorder-list.tests.dat | 2 - code/150.evaluate-reverse-polish-notation.rs | 79 ------------------- ...evaluate-reverse-polish-notation.tests.dat | 3 - ...53.find-minimum-in-rotated-sorted-array.rs | 61 -------------- ...-minimum-in-rotated-sorted-array.tests.dat | 0 ...find-minimum-in-rotated-sorted-array-ii.rs | 51 ------------ ...nimum-in-rotated-sorted-array-ii.tests.dat | 0 ...intersection-of-two-linked-lists.tests.dat | 0 src/cache/mod.rs | 2 +- 14 files changed, 1 insertion(+), 395 deletions(-) delete mode 100644 code/1.two-sum.rs delete mode 100644 code/1.two-sum.tests.dat delete mode 100644 code/100.same-tree.rs delete mode 100644 code/100.same-tree.tests.dat delete mode 100644 code/143.reorder-list.rs delete mode 100644 code/143.reorder-list.tests.dat delete mode 100644 code/150.evaluate-reverse-polish-notation.rs delete mode 100644 code/150.evaluate-reverse-polish-notation.tests.dat delete mode 100644 code/153.find-minimum-in-rotated-sorted-array.rs delete mode 100644 code/153.find-minimum-in-rotated-sorted-array.tests.dat delete mode 100644 code/154.find-minimum-in-rotated-sorted-array-ii.rs delete mode 100644 code/154.find-minimum-in-rotated-sorted-array-ii.tests.dat delete mode 100644 code/160.intersection-of-two-linked-lists.tests.dat diff --git a/code/1.two-sum.rs b/code/1.two-sum.rs deleted file mode 100644 index b485c52..0000000 --- a/code/1.two-sum.rs +++ /dev/null @@ -1,69 +0,0 @@ -// Category: algorithms -// Level: Easy -// Percent: 48.887383% - - -/* - * Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. - * - * You may assume that each input would have exactly one solution, and you may not use the same element twice. - * - * You can return the answer in any order. - * - *   - * Example 1: - * - * Input: nums = [2,7,11,15], target = 9 - * Output: [0,1] - * Explanation: Because nums[0] + nums[1] == 9, we return [0, 1]. - * - * - * Example 2: - * - * Input: nums = [3,2,4], target = 6 - * Output: [1,2] - * - * - * Example 3: - * - * Input: nums = [3,3], target = 6 - * Output: [0,1] - * - * - *   - * Constraints: - * - * - * 2 <= nums.length <= 10^4 - * -10^9 <= nums[i] <= 10^9 - * -10^9 <= target <= 10^9 - * Only one valid answer exists. - * - * - *   - * Follow-up: Can you come up with an algorithm that is less than O(n^2) time complexity? - */ - -// @lc code=start -impl Solution { - pub fn two_sum(nums: Vec, target: i32) -> Vec { - let mut ans: Vec = vec![]; - let size = nums.len(); - for i in 0..size { - let j = target - nums[i]; - let index = match nums.iter().position(|&r| r == j) { - Some(x) => x, - _ => usize::MAX - }; - if (index != i) && (index != usize::MAX) { - ans.push(i as i32); - ans.push(index as i32); - break; - } else { - continue; - } - } - ans - } -} -// @lc code=end diff --git a/code/1.two-sum.tests.dat b/code/1.two-sum.tests.dat deleted file mode 100644 index e69de29..0000000 diff --git a/code/100.same-tree.rs b/code/100.same-tree.rs deleted file mode 100644 index d1e2636..0000000 --- a/code/100.same-tree.rs +++ /dev/null @@ -1,65 +0,0 @@ -// Category: algorithms -// Level: Easy -// Percent: 55.96335% - - -/* - * Given the roots of two binary trees p and q, write a function to check if they are the same or not. - * - * Two binary trees are considered the same if they are structurally identical, and the nodes have the same value. - * - *   - * Example 1: - * - * Input: p = [1,2,3], q = [1,2,3] - * Output: true - * - * - * Example 2: - * - * Input: p = [1,2], q = [1,null,2] - * Output: false - * - * - * Example 3: - * - * Input: p = [1,2,1], q = [1,1,2] - * Output: false - * - * - *   - * Constraints: - * - * - * The number of nodes in both trees is in the range [0, 100]. - * -10^4 <= Node.val <= 10^4 - * - */ - -// @lc code=start -// Definition for a binary tree node. -// #[derive(Debug, PartialEq, Eq)] -// pub struct TreeNode { -// pub val: i32, -// pub left: Option>>, -// pub right: Option>>, -// } -// -// impl TreeNode { -// #[inline] -// pub fn new(val: i32) -> Self { -// TreeNode { -// val, -// left: None, -// right: None -// } -// } -// } -use std::rc::Rc; -use std::cell::RefCell; -impl Solution { - pub fn is_same_tree(p: Option>>, q: Option>>) -> bool { - - } -} -// @lc code=end diff --git a/code/100.same-tree.tests.dat b/code/100.same-tree.tests.dat deleted file mode 100644 index e69de29..0000000 diff --git a/code/143.reorder-list.rs b/code/143.reorder-list.rs deleted file mode 100644 index 2b7a426..0000000 --- a/code/143.reorder-list.rs +++ /dev/null @@ -1,64 +0,0 @@ -// Category: algorithms -// Level: Medium -// Percent: 49.535244% - - -/* - * You are given the head of a singly linked-list. The list can be represented as: - * - * L_0 → L_1 → … → L_n - 1 → L_n - * - * - * Reorder the list to be on the following form: - * - * L_0 → L_n → L_1 → L_n - 1 → L_2 → L_n - 2 → … - * - * - * You may not modify the values in the list's nodes. Only nodes themselves may be changed. - * - *   - * Example 1: - * - * Input: head = [1,2,3,4] - * Output: [1,4,2,3] - * - * - * Example 2: - * - * Input: head = [1,2,3,4,5] - * Output: [1,5,2,4,3] - * - * - *   - * Constraints: - * - * - * The number of nodes in the list is in the range [1, 5 * 10^4]. - * 1 <= Node.val <= 1000 - * - */ - -// @lc code=start -// Definition for singly-linked list. -// #[derive(PartialEq, Eq, Clone, Debug)] -// pub struct ListNode { -// pub val: i32, -// pub next: Option> -// } -// -// impl ListNode { -// #[inline] -// fn new(val: i32) -> Self { -// ListNode { -// next: None, -// val -// } -// } -// } -impl Solution { - pub fn reorder_list(head: &mut Option>) { - - - } -} -// @lc code=end diff --git a/code/143.reorder-list.tests.dat b/code/143.reorder-list.tests.dat deleted file mode 100644 index affbdf0..0000000 --- a/code/143.reorder-list.tests.dat +++ /dev/null @@ -1,2 +0,0 @@ -[1,2,3,4] -[1,2,3,4,5] \ No newline at end of file diff --git a/code/150.evaluate-reverse-polish-notation.rs b/code/150.evaluate-reverse-polish-notation.rs deleted file mode 100644 index 9d5a9aa..0000000 --- a/code/150.evaluate-reverse-polish-notation.rs +++ /dev/null @@ -1,79 +0,0 @@ -// Category: algorithms -// Level: Medium -// Percent: 43.61331% - - -/* - * Evaluate the value of an arithmetic expression in Reverse Polish Notation. - * - * Valid operators are +, -, *, and /. Each operand may be an integer or another expression. - * - * Note that division between two integers should truncate toward zero. - * - * It is guaranteed that the given RPN expression is always valid. That means the expression would always evaluate to a result, and there will not be any division by zero operation. - * - *   - * Example 1: - * - * Input: tokens = ["2","1","+","3","*"] - * Output: 9 - * Explanation: ((2 + 1) * 3) = 9 - * - * - * Example 2: - * - * Input: tokens = ["4","13","5","/","+"] - * Output: 6 - * Explanation: (4 + (13 / 5)) = 6 - * - * - * Example 3: - * - * Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"] - * Output: 22 - * Explanation: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 - * = ((10 * (6 / (12 * -11))) + 17) + 5 - * = ((10 * (6 / -132)) + 17) + 5 - * = ((10 * 0) + 17) + 5 - * = (0 + 17) + 5 - * = 17 + 5 - * = 22 - * - * - *   - * Constraints: - * - * - * 1 <= tokens.length <= 10^4 - * tokens[i] is either an operator: "+", "-", "*", or "/", or an integer in the range [-200, 200]. - * - */ - -// @lc code=start -impl Solution { - pub fn eval_rpn(tokens: Vec) -> i32 { - use std::collections::VecDeque; - let mut ss = VecDeque::new(); - for i in tokens { - if i=="+" || i == "-" || i=="*" || i=="/" { - let op: Vec = i.chars().collect(); - let op1 = *ss.back().unwrap(); ss.pop_back(); - let op2 = *ss.back().unwrap(); ss.pop_back(); - let ans = match op[0] { - '+' => op1+op2, - '-' => op2-op1, - '/' => op2/op1, - '*' => op1*op2, - _ => 0, - }; - ss.push_back(ans); - } - - else { - ss.push_back(i.parse().unwrap()); - } - } - *ss.back().unwrap() + 100 - } -} -// @lc code=end diff --git a/code/150.evaluate-reverse-polish-notation.tests.dat b/code/150.evaluate-reverse-polish-notation.tests.dat deleted file mode 100644 index 3e083b1..0000000 --- a/code/150.evaluate-reverse-polish-notation.tests.dat +++ /dev/null @@ -1,3 +0,0 @@ -["2","1","+","3","*"] -["4","13","5","/","+"] -["10","6","9","3","+","-11","*","/","*","17","+","5","+"] \ No newline at end of file diff --git a/code/153.find-minimum-in-rotated-sorted-array.rs b/code/153.find-minimum-in-rotated-sorted-array.rs deleted file mode 100644 index 4dd42a4..0000000 --- a/code/153.find-minimum-in-rotated-sorted-array.rs +++ /dev/null @@ -1,61 +0,0 @@ -// Category: algorithms -// Level: Medium -// Percent: 48.2175% - - -/* - * Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become: - * - * - * [4,5,6,7,0,1,2] if it was rotated 4 times. - * [0,1,2,4,5,6,7] if it was rotated 7 times. - * - * - * Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. - * - * Given the sorted rotated array nums of unique elements, return the minimum element of this array. - * - * You must write an algorithm that runs in O(log n) time. - * - *   - * Example 1: - * - * Input: nums = [3,4,5,1,2] - * Output: 1 - * Explanation: The original array was [1,2,3,4,5] rotated 3 times. - * - * - * Example 2: - * - * Input: nums = [4,5,6,7,0,1,2] - * Output: 0 - * Explanation: The original array was [0,1,2,4,5,6,7] and it was rotated 4 times. - * - * - * Example 3: - * - * Input: nums = [11,13,15,17] - * Output: 11 - * Explanation: The original array was [11,13,15,17] and it was rotated 4 times. - * - * - *   - * Constraints: - * - * - * n == nums.length - * 1 <= n <= 5000 - * -5000 <= nums[i] <= 5000 - * All the integers of nums are unique. - * nums is sorted and rotated between 1 and n times. - * - */ - -// @lc code=start -impl Solution { - pub fn find_min(nums: Vec) -> i32 { - - 7743 - } -} -// @lc code=end diff --git a/code/153.find-minimum-in-rotated-sorted-array.tests.dat b/code/153.find-minimum-in-rotated-sorted-array.tests.dat deleted file mode 100644 index e69de29..0000000 diff --git a/code/154.find-minimum-in-rotated-sorted-array-ii.rs b/code/154.find-minimum-in-rotated-sorted-array-ii.rs deleted file mode 100644 index bd90a41..0000000 --- a/code/154.find-minimum-in-rotated-sorted-array-ii.rs +++ /dev/null @@ -1,51 +0,0 @@ -// Category: algorithms -// Level: Hard -// Percent: 43.299023% - - -/* - * Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,4,4,5,6,7] might become: - * - * - * [4,5,6,7,0,1,4] if it was rotated 4 times. - * [0,1,4,4,5,6,7] if it was rotated 7 times. - * - * - * Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]]. - * - * Given the sorted rotated array nums that may contain duplicates, return the minimum element of this array. - * - * You must decrease the overall operation steps as much as possible. - * - *   - * Example 1: - * Input: nums = [1,3,5] - * Output: 1 - * Example 2: - * Input: nums = [2,2,2,0,1] - * Output: 0 - * - *   - * Constraints: - * - * - * n == nums.length - * 1 <= n <= 5000 - * -5000 <= nums[i] <= 5000 - * nums is sorted and rotated between 1 and n times. - * - * - *   - * Follow up: This problem is similar to Find Minimum in Rotated Sorted Array, but nums may contain duplicates. Would this affect the runtime complexity? How and why? - * - *   - */ - -// @lc code=start -impl Solution { - pub fn find_min(nums: Vec) -> i32 { - - 432 - } -} -// @lc code=end diff --git a/code/154.find-minimum-in-rotated-sorted-array-ii.tests.dat b/code/154.find-minimum-in-rotated-sorted-array-ii.tests.dat deleted file mode 100644 index e69de29..0000000 diff --git a/code/160.intersection-of-two-linked-lists.tests.dat b/code/160.intersection-of-two-linked-lists.tests.dat deleted file mode 100644 index e69de29..0000000 diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 184acd3..345b3ea 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -80,7 +80,7 @@ impl Cache { if maybe_json.is_err() && self.is_session_bad().await { Err(Error::CookieError) } else { - Ok(maybe_json.unwrap()) + Ok(maybe_json?) } } From 55fbede4b33e9c82f2930705d57eb73d35c0eb1d Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Tue, 4 Oct 2022 18:51:30 +0530 Subject: [PATCH 8/9] Resolved merge conflict --- src/plugins/leetcode.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/plugins/leetcode.rs b/src/plugins/leetcode.rs index 90304aa..e06627f 100644 --- a/src/plugins/leetcode.rs +++ b/src/plugins/leetcode.rs @@ -30,6 +30,7 @@ impl LeetCode { headers.insert(name.unwrap(), value.unwrap()); } + Ok(headers) } From dfd2524daa1ce2539a4affc47c69db15060b02be Mon Sep 17 00:00:00 2001 From: Manank Patel Date: Tue, 4 Oct 2022 19:19:32 +0530 Subject: [PATCH 9/9] fixed mistake --- leetcode.toml | 59 --------------------------------------------------- 1 file changed, 59 deletions(-) delete mode 100644 leetcode.toml diff --git a/leetcode.toml b/leetcode.toml deleted file mode 100644 index 0b03d68..0000000 --- a/leetcode.toml +++ /dev/null @@ -1,59 +0,0 @@ -# usually you don't wanna change those -[sys] -categories = [ - "algorithms", - "concurrency", - "database", - "shell" -] - -langs = [ - "bash", - "c", - "cpp", - "csharp", - "golang", - "java", - "javascript", - "kotlin", - "mysql", - "php", - "python", - "python3", - "ruby", - "rust", - "scala", - "swift" -] - -[sys.urls] -base = "https://leetcode.com" -graphql = "https://leetcode.com/graphql" -login = "https://leetcode.com/accounts/login/" -problems = "https://leetcode.com/api/problems/$category/" -problem = "https://leetcode.com/problems/$slug/description/" -tag = "https://leetcode.com/tag/$slug/" -test = "https://leetcode.com/problems/$slug/interpret_solution/" -session = "https://leetcode.com/session/" -submit = "https://leetcode.com/problems/$slug/submit/" -submissions = "https://leetcode.com/api/submissions/$slug" -submission = "https://leetcode.com/submissions/detail/$id/" -verify = "https://leetcode.com/submissions/detail/$id/check/" -favorites = "https://leetcode.com/list/api/questions" -favorite_delete = "https://leetcode.com/list/api/questions/$hash/$id" - -[code] -editor = "neovide" -lang = "rust" -pick = "${fid}.${slug}" -submission = "${fid}.${slug}.${sid}.${ac}" - -[cookies] -csrf = "NNahhfgjeBNVp09BYk9T8uBuUGHW1JjpURKvklAF3S1w9jEJdyHxHihwz9vURc4w" -session = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJfYXV0aF91c2VyX2lkIjoiNDczNzc2MiIsIl9hdXRoX3VzZXJfYmFja2VuZCI6ImFsbGF1dGguYWNjb3VudC5hdXRoX2JhY2tlbmRzLkF1dGhlbnRpY2F0aW9uQmFja2VuZCIsIl9hdXRoX3VzZXJfaGFzaCI6IjBiZGJhOGI2NjYwZDIyZTdmMjY4MGY4M2U3YjYyNTY0NmJiNDAyNTgiLCJpZCI6NDczNzc2MiwiZW1haWwiOiJwbWFuYW5rMjAwNUBnbWFpbC5jb20iLCJ1c2VybmFtZSI6Im1hbmFuazIwIiwidXNlcl9zbHVnIjoibWFuYW5rMjAiLCJhdmF0YXIiOiJodHRwczovL2Fzc2V0cy5sZWV0Y29kZS5jb20vdXNlcnMvbWFuYW5rMjAvYXZhdGFyXzE2Mjg5MjIyNTMucG5nIiwicmVmcmVzaGVkX2F0IjoxNjU5MDc2Mzc2LCJpcCI6IjQzLjI1MC4xNjUuMTg1IiwiaWRlbnRpdHkiOiI3NzQ0N2U5MTY4MTY2OWI1ZmI3MjE1YTYyNDEzOWJkNiIsInNlc3Npb25faWQiOjI1MjE2ODc1fQ.5h0o96lmxqUFc1vY9-JkM-FvoIw_lJ6F2OmJTVAF0K0" - -[storage] -cache = "Problems" -code = "code" -root = "~/.leetcode" -scripts = "scripts"