pub struct Solution;
Implementations§
Source§impl Solution
impl Solution
Sourcepub fn compare_version(version1: String, version2: String) -> i32
pub fn compare_version(version1: String, version2: String) -> i32
使用 Iterator
和 Pattern Matching 很简洁
s.split(".")
拆成一个 Iterator,这种东西是 lazy 的,不需要调用 collect()
收集成 Vec
,可以直接用 Iterator 的方式处理。
这里用了 fuse()
为了让 Iterator 可以持续调用 next()
直到两个 Iterator 都返回 None
才退出(说明比较完了)。
然后是 Pattern Matching 的方式,直接把所有情况列出。
- 相同的情况,继续比较下一个
- 不同的情况,可以直接返回结果
- 其中一个为
None
的情况,说明这个字符串已经结束了,相当于后面都是0
,只要另一个还有非零的值就比它大(这里题目假设了输入都是有效的,所以不可能是负数)。 - 最后全部比较完还没有差异,就是相同了,返回
0
Auto Trait Implementations§
impl Freeze for Solution
impl RefUnwindSafe for Solution
impl Send for Solution
impl Sync for Solution
impl Unpin for Solution
impl UnwindSafe for Solution
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more