GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/java/581_Shortest_Unsorted_Continuous_Subarray.java at master · tinha90/leetcode Expand file tree/ Copy path581_Shortest_Unsorted_Continuous_Subarray.javaMore file actions 87 lines (85 loc) · 2.79 KB / Copy path581_Shortest_Unsorted_Continuous_Subarray.javaFile metadata and controls87 lines (85 loc) · 2.79 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 public class Solution { // https://leetcode.com/problems/shortest-unsorted-continuous-subarray/solution/ /* public int findUnsortedSubarray(int[] nums) { int[] snums = nums.clone(); Arrays.sort(snums); int start = snums.length, end = 0; for (int i = 0; i < snums.length; i++) { if (snums[i] != nums[i]) { start = Math.min(start, i); end = Math.max(end, i); } } return (end - start >= 0 ? end - start + 1 : 0); } */ public int findUnsortedSubarray(int[] nums) { Stack < Integer > stack = new Stack < Integer > (); int l = nums.length, r = 0; for (int i = 0; i < nums.length; i++) { while (!stack.isEmpty() && nums[stack.peek()] > nums[i]) l = Math.min(l, stack.pop()); stack.push(i); } stack.clear(); for (int i = nums.length - 1; i >= 0; i--) { while (!stack.isEmpty() && nums[stack.peek()] < nums[i]) r = Math.max(r, stack.pop()); stack.push(i); } return r - l > 0 ? r - l + 1 : 0; } /* public int findUnsortedSubarray(int[] nums) { int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; boolean flag = false; for (int i = 1; i < nums.length; i++) { if (nums[i] < nums[i - 1]) flag = true; if (flag) min = Math.min(min, nums[i]); } flag = false; for (int i = nums.length - 2; i >= 0; i--) { if (nums[i] > nums[i + 1]) flag = true; if (flag) max = Math.max(max, nums[i]); } int l, r; for (l = 0; l < nums.length; l++) { if (min < nums[l]) break; } for (r = nums.length - 1; r >= 0; r--) { if (max > nums[r]) break; } return r - l < 0 ? 0 : r - l + 1; } */ /* public int findUnsortedSubarray(int[] nums) { int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE; boolean flag = false; for (int i = 1; i < nums.length; i++) { if (nums[i] < nums[i - 1]) flag = true; if (flag) min = Math.min(min, nums[i]); } flag = false; for (int i = nums.length - 2; i >= 0; i--) { if (nums[i] > nums[i + 1]) flag = true; if (flag) max = Math.max(max, nums[i]); } int l, r; for (l = 0; l < nums.length; l++) { if (min < nums[l]) break; } for (r = nums.length - 1; r >= 0; r--) { if (max > nums[r]) break; } return r - l < 0 ? 0 : r - l + 1; } */ } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |