-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finish check if array is sorted and rotated
- Loading branch information
Showing
2 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |