classSolution: deffindUnsortedSubarray(self, nums: List[int]) -> int: n = len(nums)
defisSorted() -> bool: for i inrange(1, n): if nums[i - 1] > nums[i]: returnFalse returnTrue if isSorted(): return0 numsSorted = sorted(nums) left = 0 while nums[left] == numsSorted[left]: left += 1
right = n - 1 while nums[right] == numsSorted[right]: right -= 1 return right - left + 1
var findUnsortedSubarray = function(nums) { if (isSorted(nums)) { return0; } const numsSorted = [...nums].sort((a, b) => a - b); let left = 0; while (nums[left] === numsSorted[left]) { left++; } let right = nums.length - 1; while (nums[right] == numsSorted[right]) { right--; } return right - left + 1; };
constisSorted = (nums) => { for (let i = 1; i < nums.length; i++) { if (nums[i] < nums[i - 1]) { returnfalse; } } returntrue; }
[sol1-Golang]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcfindUnsortedSubarray(nums []int)int { if sort.IntsAreSorted(nums) { return0 } numsSorted := append([]int(nil), nums...) sort.Ints(numsSorted) left, right := 0, len(nums)-1 for nums[left] == numsSorted[left] { left++ } for nums[right] == numsSorted[right] { right-- } return right - left + 1 }
boolis_sorted(int* arr, int arrSize) { for (int i = 1; i < arrSize; i++) { if (arr[i - 1] > arr[i]) { returnfalse; } } returntrue; }
intcmp(int* a, int* b) { return *a - *b; }
intfindUnsortedSubarray(int* nums, int numsSize) { if (is_sorted(nums, numsSize)) { return0; } int numsSorted[numsSize]; memcpy(numsSorted, nums, sizeof(int) * numsSize); qsort(numsSorted, numsSize, sizeof(int), cmp); int left = 0; while (nums[left] == numsSorted[left]) { left++; } int right = numsSize - 1; while (nums[right] == numsSorted[right]) { right--; } return right - left + 1; }
classSolution { public: intfindUnsortedSubarray(vector<int>& nums){ int n = nums.size(); int maxn = INT_MIN, right = -1; int minn = INT_MAX, left = -1; for (int i = 0; i < n; i++) { if (maxn > nums[i]) { right = i; } else { maxn = nums[i]; } if (minn < nums[n - i - 1]) { left = n - i - 1; } else { minn = nums[n - i - 1]; } } return right == -1 ? 0 : right - left + 1; } };
publicclassSolution { publicintFindUnsortedSubarray(int[] nums) { int n = nums.Length; int maxn = int.MinValue, right = -1; int minn = int.MaxValue, left = -1; for (int i = 0; i < n; i++) { if (maxn > nums[i]) { right = i; } else { maxn = nums[i]; } if (minn < nums[n - i - 1]) { left = n - i - 1; } else { minn = nums[n - i - 1]; } } return right == -1 ? 0 : right - left + 1; } }
[sol2-Python3]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
classSolution: deffindUnsortedSubarray(self, nums: List[int]) -> int: n = len(nums) maxn, right = float("-inf"), -1 minn, left = float("inf"), -1
for i inrange(n): if maxn > nums[i]: right = i else: maxn = nums[i] if minn < nums[n - i - 1]: left = n - i - 1 else: minn = nums[n - i - 1] return0if right == -1else right - left + 1
[sol2-JavaScript]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
var findUnsortedSubarray = function(nums) { const n = nums.length; let maxn = -Number.MAX_VALUE, right = -1; let minn = Number.MAX_VALUE, left = -1; for (let i = 0; i < n; i++) { if (maxn > nums[i]) { right = i; } else { maxn = nums[i]; } if (minn < nums[n - i - 1]) { left = n - i - 1; } else { minn = nums[n - i - 1]; } } return right === -1 ? 0 : right - left + 1; };
funcfindUnsortedSubarray(nums []int)int { n := len(nums) minn, maxn := math.MaxInt64, math.MinInt64 left, right := -1, -1 for i, num := range nums { if maxn > num { right = i } else { maxn = num } if minn < nums[n-i-1] { left = n - i - 1 } else { minn = nums[n-i-1] } } if right == -1 { return0 } return right - left + 1 }
[sol2-C]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
intfindUnsortedSubarray(int* nums, int numsSize) { int n = numsSize; int maxn = INT_MIN, right = -1; int minn = INT_MAX, left = -1; for (int i = 0; i < n; i++) { if (maxn > nums[i]) { right = i; } else { maxn = nums[i]; } if (minn < nums[n - i - 1]) { left = n - i - 1; } else { minn = nums[n - i - 1]; } } return right == -1 ? 0 : right - left + 1; }