diff --git a/src/arr.rs b/src/arr.rs index 5a21a23..f66682c 100644 --- a/src/arr.rs +++ b/src/arr.rs @@ -13,13 +13,63 @@ pub fn make_arr(s: &str) -> Vec { } pub fn make_matrix(s: &str) -> Vec> { - s.trim_matches(&['[', ']'][..]) - .split("],[") - .map(|row| { - row.trim_matches(&['[', ']'][..]) - .split(',') - .map(|x| x.trim().parse::().unwrap()) - .collect() - }) - .collect() + let s = s.trim(); + + // Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]" + let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') { + &s[1..s.len() - 1] + } else { + s + } + .trim(); + + if inner.is_empty() { + return Vec::new(); + } + + let bytes = inner.as_bytes(); + let mut i = 0usize; + let mut rows: Vec> = Vec::new(); + while i < bytes.len() { + // find next '[' + while i < bytes.len() && bytes[i] != b'[' { + i += 1; + } + if i >= bytes.len() { + break; + } + let start = i + 1; // byte index after '[' + i = start; + // find matching ']' + while i < bytes.len() && bytes[i] != b']' { + i += 1; + } + let end = i; // exclusive + // move i past ']' for next iteration (if in bounds) + if i < bytes.len() { + i += 1; + } + + if end <= start { + // empty row like [] + rows.push(Vec::new()); + continue; + } + + let row_str = &inner[start..end]; + let row_vec: Vec = row_str + .split(',') + .map(|x| { + let x = x.trim(); + if x == "null" || x.is_empty() { + -1 + } else { + x.parse::().unwrap() + } + }) + .collect(); + rows.push(row_vec); + } + + rows } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index fbb6022..25971f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,26 +1,71 @@ mod arr; -struct Solution {} +struct Bank { + _balance: Vec, +} -impl Solution { - pub fn number_of_beams(bank: Vec) -> i32 { - let mut ans = 0; - let mut last_ones = 0; - for i in 0..bank.len() { - let new_ones = bank[i].chars().fold(0,|last, c| last + if c == '1'{1}else {0}); - if new_ones == 0 { - continue; - } - ans += last_ones * new_ones; - last_ones = new_ones; + +/** + * `&self` means the method takes an immutable reference. + * If you need a mutable reference, change it to `&mut self` instead. + */ +impl Bank { + + fn new(balance: Vec) -> Self { + Bank { _balance: balance } + } + + fn valid(&self, account: i32,money: i64) -> bool { + account > 0 && account as usize <= self._balance.len() && self._balance[account as usize - 1] >= money + } + fn transfer(&mut self, account1: i32, account2: i32, money: i64) -> bool { + if !self.valid(account1, money) { + return false; + } + self._balance[account1 as usize - 1] -= money; + self._balance[account2 as usize - 1] += money; + true + } + + fn deposit(&mut self, account: i32, money: i64) -> bool { + if !self.valid(account, 0) { + return false; + } + self._balance[account as usize - 1] += money; + true + } + + fn withdraw(&mut self, account: i32, money: i64) -> bool { + if !self.valid(account, money) { + false + } else { + self._balance[account as usize - 1] -= money; + true } - ans } } +/** + * Your Bank object will be instantiated and called as such: + * let obj = Bank::new(balance); + * let ret_1: bool = obj.transfer(account1, account2, money); + * let ret_2: bool = obj.deposit(account, money); + * let ret_3: bool = obj.withdraw(account, money); + */ + fn main() { - let src = vec!["011001", "000000", "010100", "001000"] - .iter() - .map(|s| s.to_string()) - .collect(); - println!("{:?}", Solution::number_of_beams(src)); + let oper = ["withdraw", "transfer", "deposit", "transfer", "withdraw"]; + let val = arr::make_matrix("[[3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]"); + //输出: + //[true, true, true, false, false] + let mut bank = Bank::new(vec![10, 100, 20, 50, 30]); + for i in 0..oper.len() { + let result = match oper[i] { + "withdraw" => bank.withdraw(val[i][0], val[i][1] as i64), + "transfer" => bank.transfer(val[i][0], val[i][1], val[i][2] as i64), + "deposit" => bank.deposit(val[i][0], val[i][1] as i64), + _ => false, + }; + println!("{}", result); + } + }