Skip to content

Instantly share code, notes, and snippets.

@rndD
Created September 29, 2023 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rndD/01724e2a67d3a9b9add8be7d43fd9096 to your computer and use it in GitHub Desktop.
Save rndD/01724e2a67d3a9b9add8be7d43fd9096 to your computer and use it in GitHub Desktop.
some leetcode problem, just to share in chat
func isMonotonic(nums []int) bool {
direction := 0
if len(nums) == 1 {
return true
}
for i := 0; i < len(nums); i++ {
if i+1 == len(nums) {
break
}
if direction == 0 {
if nums[i] > nums[i+1] {
direction = -1
}
if nums[i] < nums[i+1] {
direction = 1
}
} else {
if direction == 1 && nums[i] > nums[i+1] {
return false
}
if direction == -1 && nums[i] < nums[i+1] {
return false
}
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment