2043. 简易银行系统
This commit is contained in:
parent
4f601269e8
commit
c54a750759
68
src/arr.rs
68
src/arr.rs
|
|
@ -13,13 +13,63 @@ pub fn make_arr(s: &str) -> Vec<i32> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
||||||
s.trim_matches(&['[', ']'][..])
|
let s = s.trim();
|
||||||
.split("],[")
|
|
||||||
.map(|row| {
|
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||||
row.trim_matches(&['[', ']'][..])
|
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||||
.split(',')
|
&s[1..s.len() - 1]
|
||||||
.map(|x| x.trim().parse::<i32>().unwrap())
|
} else {
|
||||||
.collect()
|
s
|
||||||
})
|
}
|
||||||
.collect()
|
.trim();
|
||||||
|
|
||||||
|
if inner.is_empty() {
|
||||||
|
return Vec::new();
|
||||||
|
}
|
||||||
|
|
||||||
|
let bytes = inner.as_bytes();
|
||||||
|
let mut i = 0usize;
|
||||||
|
let mut rows: Vec<Vec<i32>> = 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<i32> = row_str
|
||||||
|
.split(',')
|
||||||
|
.map(|x| {
|
||||||
|
let x = x.trim();
|
||||||
|
if x == "null" || x.is_empty() {
|
||||||
|
-1
|
||||||
|
} else {
|
||||||
|
x.parse::<i32>().unwrap()
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
rows.push(row_vec);
|
||||||
|
}
|
||||||
|
|
||||||
|
rows
|
||||||
}
|
}
|
||||||
81
src/main.rs
81
src/main.rs
|
|
@ -1,26 +1,71 @@
|
||||||
mod arr;
|
mod arr;
|
||||||
struct Solution {}
|
struct Bank {
|
||||||
|
_balance: Vec<i64>,
|
||||||
|
}
|
||||||
|
|
||||||
impl Solution {
|
|
||||||
pub fn number_of_beams(bank: Vec<String>) -> i32 {
|
/**
|
||||||
let mut ans = 0;
|
* `&self` means the method takes an immutable reference.
|
||||||
let mut last_ones = 0;
|
* If you need a mutable reference, change it to `&mut self` instead.
|
||||||
for i in 0..bank.len() {
|
*/
|
||||||
let new_ones = bank[i].chars().fold(0,|last, c| last + if c == '1'{1}else {0});
|
impl Bank {
|
||||||
if new_ones == 0 {
|
|
||||||
continue;
|
fn new(balance: Vec<i64>) -> Self {
|
||||||
}
|
Bank { _balance: balance }
|
||||||
ans += last_ones * new_ones;
|
}
|
||||||
last_ones = new_ones;
|
|
||||||
|
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() {
|
fn main() {
|
||||||
let src = vec!["011001", "000000", "010100", "001000"]
|
let oper = ["withdraw", "transfer", "deposit", "transfer", "withdraw"];
|
||||||
.iter()
|
let val = arr::make_matrix("[[3, 10], [5, 1, 20], [5, 20], [3, 4, 15], [10, 50]]");
|
||||||
.map(|s| s.to_string())
|
//输出:
|
||||||
.collect();
|
//[true, true, true, false, false]
|
||||||
println!("{:?}", Solution::number_of_beams(src));
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue