Leetcode Note: Go - Check if Array Is Sorted and Rotated

Check if Array Is Sorted and Rotated - LeetCode
https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/

  • Go 言語で取り組んだメモ

回答

Go 100% time and 100% space | O(n) - Check if Array Is Sorted and Rotated - LeetCode
https://leetcode.com/problems/check-if-array-is-sorted-and-rotated/solutions/1663211/go-100-time-and-100-space-o-n/

func check(nums []int) bool {
	i := 1
	for ; i < len(nums) && nums[i-1] <= nums[i]; i++ {
	}

	if i == len(nums) {
		return true
	}

	for i++; i < len(nums) && nums[i-1] <= nums[i]; i++ {
	}

	return i == len(nums) && nums[i-1] <= nums[0]
}