| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,13 @@ | |||
| 1 | + //执行用时 : 1 ms, 在Find Minimum in Rotated Sorted Array的Java提交中击败了92.41% 的用户 | ||
| 2 | + //内存消耗 : 37.5 MB, 在Find Minimum in Rotated Sorted Array的Java提交中击败了7.22%的用户 | ||
| 3 | + | ||
| 4 | + class Solution { | ||
| 5 | + public int findMin(int[] nums) { | ||
| 6 | + int i = 1; | ||
| 7 | + int min = nums[0]; | ||
| 8 | + for(;i<nums.length;i++){ | ||
| 9 | + if(min>nums[i]) min=nums[i]; | ||
| 10 | + } | ||
| 11 | + return min; | ||
| 12 | + } | ||
| 13 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,112 +1,57 @@ | |||
| 1 | - 2.0 | ||
| 2 | - //先设置链表头 | ||
| 3 | - //任一为空,直接连接另一条链表 | ||
| 4 | - //执行用时 : 2 ms, 在Merge Two Sorted Lists的Java提交中击败了99.44% 的用户 | ||
| 5 | - //内存消耗 : 35.3 MB, 在Merge Two Sorted Lists的Java提交中击败了90.56% 的用户 | ||
| 1 | + 1,申请额外空间 | ||
| 2 | + | ||
| 3 | + | ||
| 4 | + //执行用时 : 3 ms, 在Sort Array By Parity的Java提交中击败了99.59% 的用户 | ||
| 5 | + //内存消耗 : 46.5 MB, 在Sort Array By Parity的Java提交中击败了68.41% 的用户 | ||
| 6 | + | ||
| 6 | 7 | class Solution { | |
| 7 | - public ListNode mergeTwoLists(ListNode l1, ListNode l2) { | ||
| 8 | - ListNode head = null; | ||
| 9 | - ListNode p = null; | ||
| 8 | + public int[] sortArrayByParity(int[] A) { | ||
| 10 | 9 | ||
| 11 | - //if(l1 == null && l2 == null) return l1; 包含在下一句中 | ||
| 12 | - if(l1 == null) return l2; | ||
| 13 | - if(l2 == null) return l1; | ||
| 10 | + int j = A.length; | ||
| 11 | + if (j<=1) return A; | ||
| 12 | + int[] B = new int[j]; | ||
| 14 | 13 | ||
| 15 | - if(l1.val <= l2.val){ | ||
| 16 | - head = l1; | ||
| 17 | - l1 = l1.next; | ||
| 18 | - p = head; | ||
| 19 | - } | ||
| 20 | - else{ | ||
| 21 | - head = l2; | ||
| 22 | - l2 = l2.next; | ||
| 23 | - p = head; | ||
| 24 | - } | ||
| 14 | + int x=0; | ||
| 15 | + int y=j-1; | ||
| 25 | 16 | ||
| 26 | - while (l1 != null || l2 != null){ | ||
| 27 | - ListNode t; | ||
| 28 | - if (l2 == null) { | ||
| 29 | - p.next = l1; | ||
| 30 | - break; | ||
| 31 | - } | ||
| 32 | - else if(l1 == null){ | ||
| 33 | - p.next = l2; | ||
| 34 | - break; | ||
| 17 | + for(int i=0; i<j; i++){ //注意是i<j,而不是i<=j | ||
| 18 | + | ||
| 19 | + if(A[i]%2 != 0){ | ||
| 20 | + B[y] = A[i]; | ||
| 21 | + y--; | ||
| 35 | 22 | } | |
| 36 | 23 | else{ | |
| 37 | - if(l1.val <= l2.val){ | ||
| 38 | - t = l1; | ||
| 39 | - l1 = l1.next; | ||
| 40 | - p.next = t; | ||
| 41 | - p = p.next; | ||
| 42 | - } | ||
| 43 | - else{ | ||
| 44 | - t = l2; | ||
| 45 | - l2 = l2.next; | ||
| 46 | - p.next = t; | ||
| 47 | - p = p.next; | ||
| 48 | - } | ||
| 49 | - } | ||
| 24 | + B[x] = A[i]; | ||
| 25 | + x++; | ||
| 26 | + } | ||
| 50 | 27 | } | |
| 51 | - return head; | ||
| 28 | + return B; | ||
| 52 | 29 | } | |
| 53 | 30 | } | |
| 54 | 31 | ||
| 55 | 32 | ||
| 56 | - 1.0 | ||
| 57 | - //先设置链表头 | ||
| 58 | - //任一为空,后续不高效 | ||
| 59 | - //执行用时 : 2 ms, 在Merge Two Sorted Lists的Java提交中击败了99.44% 的用户 | ||
| 60 | - //内存消耗 : 34.8 MB, 在Merge Two Sorted Lists的Java提交中击败了95.63% 的用户 | ||
| 33 | + | ||
| 34 | + 2,不利用额外空间,前后指针,交换 | ||
| 61 | 35 | class Solution { | |
| 62 | - public ListNode mergeTwoLists(ListNode l1, ListNode l2) { | ||
| 63 | - ListNode head = null; | ||
| 64 | - ListNode p = null; | ||
| 65 | - | ||
| 66 | - //if(l1 == null && l2 == null) return l1; 包含在下一句中 | ||
| 67 | - if(l1 == null) return l2; | ||
| 68 | - if(l2 == null) return l1; | ||
| 36 | + public int[] sortArrayByParity(int[] A) { | ||
| 69 | 37 | ||
| 70 | - if(l1.val <= l2.val){ | ||
| 71 | - head = l1; | ||
| 72 | - l1 = l1.next; | ||
| 73 | - p = head; | ||
| 74 | - } | ||
| 75 | - else{ | ||
| 76 | - head = l2; | ||
| 77 | - l2 = l2.next; | ||
| 78 | - p = head; | ||
| 79 | - } | ||
| 38 | + if(A == null || A.length == 1) return A; | ||
| 39 | + int j = A.length - 1; | ||
| 40 | + int i = 0; | ||
| 80 | 41 | ||
| 81 | - while (l1 != null || l2 != null){ //注意是 ||,不是&& | ||
| 82 | - ListNode t; | ||
| 83 | - if (l2 == null) { | ||
| 84 | - t = l1; | ||
| 85 | - l1 = l1.next; | ||
| 86 | - p.next = t; | ||
| 87 | - p = p.next; | ||
| 88 | - } | ||
| 89 | - else if(l1 == null){ | ||
| 90 | - t = l2; | ||
| 91 | - l2 = l2.next; | ||
| 92 | - p.next = t; | ||
| 93 | - p = p.next; | ||
| 42 | + while(i<=j){ //注意不是i!=j,比如数组长度为2,会导致越界 | ||
| 43 | + if(A[i]%2!=0 && A[j]%2==0){ | ||
| 44 | + int temp = A[i]; | ||
| 45 | + A[i] = A[j]; | ||
| 46 | + A[j] = temp; | ||
| 47 | + i++; | ||
| 48 | + j--; | ||
| 94 | 49 | } | |
| 95 | 50 | else{ | |
| 96 | - if(l1.val <= l2.val){ | ||
| 97 | - t = l1; | ||
| 98 | - l1 = l1.next; | ||
| 99 | - p.next = t; | ||
| 100 | - p = p.next; | ||
| 101 | - } | ||
| 102 | - else{ | ||
| 103 | - t = l2; | ||
| 104 | - l2 = l2.next; | ||
| 105 | - p.next = t; | ||
| 106 | - p = p.next; | ||
| 107 | - } | ||
| 51 | + if(A[i]%2==0) i++; | ||
| 52 | + if(A[j]%2!=0) j--; | ||
| 108 | 53 | } | |
| 109 | 54 | } | |
| 110 | - return head; | ||
| 55 | + return A; | ||
| 111 | 56 | } | |
| 112 | - } | ||
| 57 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,10 @@ | |||
| 1 | + 1,遍历 | ||
| 2 | + https://leetcode-cn.com/problems/search-in-rotated-sorted-array-ii/comments/ | ||
| 3 | + class Solution { | ||
| 4 | + public boolean search(int[] nums, int target) { | ||
| 5 | + for(int i = 0; i<nums.length; i++){ | ||
| 6 | + if(target == nums[i]) return true; | ||
| 7 | + } | ||
| 8 | + return false; | ||
| 9 | + } | ||
| 10 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,64 @@ | |||
| 1 | + 1,建立新数组 | ||
| 2 | + | ||
| 3 | + //执行用时 : 6 ms, 在Sort Array By Parity II的Java提交中击败了83.69% 的用户 | ||
| 4 | + //内存消耗 : 48.9 MB, 在Sort Array By Parity II的Java提交中击败了67.91% 的用户 | ||
| 5 | + | ||
| 6 | + class Solution { | ||
| 7 | + public int[] sortArrayByParityII(int[] A) { | ||
| 8 | + if(A == null || A.length == 1) return A; | ||
| 9 | + int k = A.length-1; | ||
| 10 | + int[] B = new int[A.length]; | ||
| 11 | + | ||
| 12 | + int i = 0; | ||
| 13 | + int j = 0; | ||
| 14 | + int x = 1; | ||
| 15 | + while(i<=k){ | ||
| 16 | + if(A[i]%2 == 0){ | ||
| 17 | + B[j] = A[i]; | ||
| 18 | + j=j+2; | ||
| 19 | + } | ||
| 20 | + else{ | ||
| 21 | + B[x] = A[i]; | ||
| 22 | + x = x+2; | ||
| 23 | + } | ||
| 24 | + i++; | ||
| 25 | + } | ||
| 26 | + return B; | ||
| 27 | + } | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + 2, | ||
| 33 | + //执行用时 : 7 ms, 在Sort Array By Parity II的Java提交中击败了77.23% 的用户 | ||
| 34 | + //内存消耗 : 49.7 MB, 在Sort Array By Parity II的Java提交中击败了51.80% 的用户 | ||
| 35 | + | ||
| 36 | + //执行用时 : 5 ms, 在Sort Array By Parity II的Java提交中击败了93.39% 的用户 | ||
| 37 | + //内存消耗 : 41.5 MB, 在Sort Array By Parity II的Java提交中击败了92.67% 的用户 | ||
| 38 | + | ||
| 39 | + //执行用时 : 7 ms, 在Sort Array By Parity II的Java提交中击败了77.23% 的用户 | ||
| 40 | + //内存消耗 : 52.3 MB, 在Sort Array By Parity II的Java提交中击败了9.49% 的用户 | ||
| 41 | + class Solution { | ||
| 42 | + public int[] sortArrayByParityII(int[] A) { | ||
| 43 | + if(A == null || A.length == 1) return A; | ||
| 44 | + int k = A.length-1; | ||
| 45 | + | ||
| 46 | + int i = 0; | ||
| 47 | + int j = 1; | ||
| 48 | + | ||
| 49 | + while(i<=k && j<=k){ | ||
| 50 | + if(A[i]%2 != 0 && A[j]%2 == 0 ){ | ||
| 51 | + int temp = A[i]; | ||
| 52 | + A[i] = A[j]; | ||
| 53 | + A[j]=temp; | ||
| 54 | + i = i+2; | ||
| 55 | + j = j+2; | ||
| 56 | + } | ||
| 57 | + else{ | ||
| 58 | + if(A[i]%2==0) i = i+2; | ||
| 59 | + if(A[j]%2!=0) j = j+2; | ||
| 60 | + } | ||
| 61 | + } | ||
| 62 | + return A; | ||
| 63 | + } | ||
| 64 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments