Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

单调数列 #119

Open
yankewei opened this issue Feb 27, 2021 · 1 comment
Open

单调数列 #119

yankewei opened this issue Feb 27, 2021 · 1 comment
Labels
数组 题目类型为数组 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

如果数组是单调递增或单调递减的,那么它是单调的。

如果对于所有 i <= j,A[i] <= A[j],那么数组 A 是单调递增的。 如果对于所有 i <= j,A[i]> = A[j],那么数组 A 是单调递减的。

当给定的数组 A 是单调数组时返回 true,否则返回 false。

示例 1:

输入:[1,2,2,3]
输出:true

示例 2:

输入:[6,5,4,4]
输出:true

示例 3:

输入:[1,3,2]
输出:false

示例 4:

输入:[1,2,4,5]
输出:true

示例 5:

输入:[1,1,1]
输出:true

提示:

  • 1 <= A.length <= 50000
  • -100000 <= A[i] <= 100000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/monotonic-array
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

@yankewei yankewei added 数组 题目类型为数组 简单 题目难度为简单 labels Feb 27, 2021
@yankewei
Copy link
Owner Author

没什么说的

func isMonotonic(A []int) bool {
    if len(A) == 1 {
        return true
    }
    if A[0] == A[len(A)-1] {
        for i := 1; i < len(A); i++ {
            if A[i] != A[i-1] {
                return false 
            }            
        }
    } else if A[0] > A[len(A)-1] {
        for i := 1; i < len(A); i++ {
            if A[i] > A[i-1] {
                return false
            }
        }
    } else {
        for i := 1; i < len(A); i++ {
            if A[i] < A[i-1] {
                return false
            }
        }
    }

    return true
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
数组 题目类型为数组 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant