Golang Leetcode 896. Monotonic Array.go

2019-05-05 11:54:38 浏览数 (1)

版权声明:原创勿转 https://cloud.tencent.com/developer/article/1420474

思路

定义两个标志位,单调递增和单调递减

扫描一遍,判断是否递增或者递减

code

代码语言:javascript复制
func isMonotonic(A []int) bool {
	increasing, decreasing := true, true
	for i := 1; i < len(A); i   {
		if A[i] > A[i-1] {
			decreasing = false
		}
		if A[i] < A[i-1] {
			increasing = false
		}
	}
	return increasing || decreasing
}

0 人点赞