Skip to content

Commit

Permalink
finish check if array is sorted and rotated
Browse files Browse the repository at this point in the history
  • Loading branch information
Checky-Hu committed Apr 6, 2021
1 parent 34cecef commit 87e447a
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions 36-50/check-if-array-is-sorted-and-rotated/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "check-if-array-is-sorted-and-rotated"
version = "0.1.0"
authors = ["Checky Hu <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
46 changes: 46 additions & 0 deletions 36-50/check-if-array-is-sorted-and-rotated/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use std::env;
use std::str::FromStr;

struct Solution {}

impl Solution {
pub fn check(nums: Vec<i32>) -> bool {
let len: usize = nums.len();
if len == 0 {
return true;
}
let mut status: bool = false;
for i in 1..len {
if nums[i] < nums[i - 1] {
if status {
return false;
} else {
status = true;
}
}
}
!(status && nums[0] < nums[len - 1])
}
}

fn main() {
let mut ret: usize = 0;
let mut nums: Vec<i32> = Vec::new();
for (index, arg) in env::args().enumerate() {
match index {
0 => (),
_ => {
ret += 1;
let num: i32 = i32::from_str(&arg).expect("Error parse.");
nums.push(num);
}
}
}

if 0 == ret {
println!("Require at least 1 parameter.");
return;
}

println!("Is sorted and rotated: {}", Solution::check(nums));
}

0 comments on commit 87e447a

Please sign in to comment.