Created
September 29, 2023 21:49
-
-
Save rndD/01724e2a67d3a9b9add8be7d43fd9096 to your computer and use it in GitHub Desktop.
some leetcode problem, just to share in chat
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
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