diff --git a/README.md b/README.md index d8906956..6838188c 100644 --- a/README.md +++ b/README.md @@ -17,6 +17,6 @@ ## 注意事项 -1. **代码文件命名规则:**`LeetCode_题目序号_学号`,比如学号为 `0` 的学员完成 [LeetCode 的第 2 题](https://leetcode.com/problems/add-two-numbers/description/) 后,请将代码文件名保存为 `LeetCode_2_0.py` (假设你使用的是 Python 语言)。 +1. **代码文件命名规则:**`LeetCode_题目序号_学号`,比如学号为 `0` 的学员完成 [LeetCode_33_108 的第 2 题](https://leetcode.com/problems/add-two-numbers/description/) 后,请将代码文件名保存为 `LeetCode_2_0.py` (假设你使用的是 Python 语言)。 2. **作业公布地址:** 我们会在置顶的 issue 中公布当周的算法练习题以及其他注意事项。 3. 如果对 Git 和 GitHub 不太了解,请参考 [Git 官方文档](https://git-scm.com/book/zh/v2) 或者极客时间的[《玩转 Git 三剑客》](https://time.geekbang.org/course/intro/145)视频课程。 diff --git a/Week_01/id_108/LeetCode_20_108.java b/Week_01/id_108/LeetCode_20_108.java new file mode 100644 index 00000000..aafe9ceb --- /dev/null +++ b/Week_01/id_108/LeetCode_20_108.java @@ -0,0 +1,49 @@ +import org.junit.Test; + +import java.util.Stack; + +/** + * @author zhangruihao.zhang + * @version v1.0.0 + * @since 2019/04/29 + */ +public class LeetCode_20_108 { + public static void main(String[] args) { + String str = "()[]{}"; + System.out.println(isValid(str)); + } + public static boolean isValid(String s) { + Stack stack = new Stack(); + for(int i=0;i target){ + //target处于旋转的后半段 + return binarySerach(nums,min,end,target); + }else{ + //target有可能处于旋转的前半段 + return binarySerach(nums,0,min-1,target); + } + } + + //查找旋转点 + private int min(int[] nums){ + int low = 0; + int high = nums.length - 1; + int mid; + while(low < high){ + mid = low + ((high-low)>>1); + if(nums[mid]>nums[high]){ + low = mid + 1; + }else{ + high = mid; + } + } + return low; + } + + //二分查找 + private int binarySerach(int[] nums,int left,int right,int target){ + int mid = 0; + while(left>1); + if(nums[mid]==target){ + return mid; + } + if(nums[mid]target){ + right = mid - 1; + continue; + } + } + if(nums[left] == target){ + return left; + } + return -1; + } + } +} diff --git a/Week_01/id_108/LeetCode_503_108.java b/Week_01/id_108/LeetCode_503_108.java new file mode 100644 index 00000000..1f98abff --- /dev/null +++ b/Week_01/id_108/LeetCode_503_108.java @@ -0,0 +1,33 @@ +import java.util.Stack; + +/** + * @author zhangruihao.zhang + * @version v1.0.0 + * @since 2019/04/29 + */ +public class LeetCode_503_108 { + class Solution { + public int[] nextGreaterElements(int[] nums) { + + Stack stack = new Stack<>(); + int index[] = new int[nums.length]; + for (int i = 0; i < index.length; i++) { + index[i] = -1; + } + if (nums.length == 1) { + return index; + } + int count = 0; + while (count < 2) { + for (int i = 0; i < nums.length; i++) { + while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) { + index[stack.pop()] = nums[i]; + } + stack.push(i); + } + count++; + } + return index; + } + } +} diff --git a/Week_01/id_108/LeetCode_687_108.java b/Week_01/id_108/LeetCode_687_108.java new file mode 100644 index 00000000..295b6383 --- /dev/null +++ b/Week_01/id_108/LeetCode_687_108.java @@ -0,0 +1,50 @@ +/** + * @author zhangruihao.zhang + * @version v1.0.0 + * @since 2019/05/03 + */ +public class LeetCode_687_108 { + /** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode(int x) { val = x; } + * } + */ + class Solution { + int maxLen = 0; + public int longestUnivaluePath(TreeNode root) { + if(root == null){ + return 0; + } + longest(root); + return maxLen; + } + + private int longest(TreeNode root){ + if(root == null){ + return 0; + } + int leftLen=longest( root.left); + int rightLen=longest( root.right); + int leftUnivalueLen = 0; + int rightUnivalue = 0; + if(root.left != null && root.left.val == root.val){ + leftUnivalueLen = leftLen + 1; + } + + if(root.right != null && root.right.val == root.val){ + rightUnivalue = rightLen + 1; + } + + maxLen = max(maxLen , leftUnivalueLen + rightUnivalue ); + return max(leftUnivalueLen,rightUnivalue); + } + + private int max(int x,int y){ + return x>y ? x : y; + } + } +} diff --git a/Week_01/id_108/LeetCode_698_108.java b/Week_01/id_108/LeetCode_698_108.java new file mode 100644 index 00000000..ec86aeef --- /dev/null +++ b/Week_01/id_108/LeetCode_698_108.java @@ -0,0 +1,60 @@ +import java.util.Arrays; + +/** + * @author zhangruihao.zhang + * @version v1.0.0 + * @since 2019/05/04 + */ +public class LeetCode_698_108 { + //这道题没调出来,看的别人的答案 + class Solution { + public boolean canPartitionKSubsets(int[] nums, int k) { + //因为题目限制条件不用担心溢出 + int sum = 0; + for(int i = 0; i < nums.length; i++){ + sum += nums[i]; + } + if(sum % k != 0){ + return false; + } + //求出子集的和 + sum = sum / k; + //排序 小的放最前面大的放最后面 + Arrays.sort(nums); + //如果子集的和小于数组最大的直接返回false + if(nums[nums.length - 1] > sum){ + return false; + } + //建立一个长度为k的桶 + int[] arr = new int[k]; + //桶的每一个值都是子集的和 + Arrays.fill(arr, sum); + //从数组最后一个数开始进行递归 + return help(nums, nums.length - 1, arr, k); + } + + boolean help(int[] nums, int cur, int[] arr, int k){ + //已经遍历到了-1说明前面的所有数都正好可以放入桶里,那所有桶的值此时都为0,说明找到了结果,返回true + if(cur < 0){ + return true; + } + //遍历k个桶 + for(int i = 0; i < k; i++){ + //如果正好能放下当前的数或者放下当前的数后,还有机会继续放前面的数(剪枝) + if(arr[i] == nums[cur] || (cur > 0 && arr[i] - nums[cur] >= nums[0])){ + //放当前的数到桶i里 + arr[i] -= nums[cur]; + //开始放下一个数 + if(help(nums, cur - 1, arr, k)){ + return true; + } + //这个数不该放在桶i中 + //从桶中拿回当前的数 + arr[i] += nums[cur]; + } + } + return false; + } + + } +} diff --git a/Week_01/id_108/LeetCode_895_108.java b/Week_01/id_108/LeetCode_895_108.java new file mode 100644 index 00000000..fc188a29 --- /dev/null +++ b/Week_01/id_108/LeetCode_895_108.java @@ -0,0 +1,83 @@ +import org.junit.Test; + +import java.util.HashMap; +import java.util.PriorityQueue; + +/** + * @author zhangruihao.zhang + * @version v1.0.0 + * @since 2019/05/03 + */ +public class LeetCode_895_108 { + + class FreqStack { + + private PriorityQueue queue = null; + private HashMap itemCountMap = null; + private int count; + + public FreqStack() { + itemCountMap = new HashMap<>(); + queue = new PriorityQueue<>((o1, o2) -> + o1.getCount().compareTo(o2.getCount()) == 0 ? + -o1.getOrder().compareTo(o2.getOrder()) : -o1.getCount().compareTo(o2.getCount())); + } + + public void push(int x) { + count ++; + itemCountMap.put(x, itemCountMap.getOrDefault(x, 0) + 1); + queue.offer(new Pair(x, count, itemCountMap.getOrDefault(x, 0) + 1)); + } + + public int pop() { + Pair peek = queue.poll(); + int item = peek == null ? -1 : peek.item; + Integer integer = itemCountMap.get(item); + if(integer == 1){ + itemCountMap.remove(item); + }else { + itemCountMap.put(item, itemCountMap.get(item) - 1); + } + return item; + } + + private class Pair { + private int item; + //添加入队的顺序 + private Integer order; + //共有几个相同的元素 + private Integer count; + + public Pair(int item, Integer order, Integer count) { + this.item = item; + this.order = order; + this.count = count; + } + + public int getItem() { + return item; + } + + public void setItem(int item) { + this.item = item; + } + + public Integer getOrder() { + return order; + } + + public void setOrder(Integer order) { + this.order = order; + } + + public Integer getCount() { + return count; + } + + public void setCount(Integer count) { + this.count = count; + } + } + } + +} diff --git a/Week_01/id_127/NOTE.md b/Week_01/id_127/NOTE.md index 9f3b5f1f..eed2a91e 100644 --- a/Week_01/id_127/NOTE.md +++ b/Week_01/id_127/NOTE.md @@ -1,8 +1,8 @@ # 学习笔记 -### LeetCode-83思路: +### LeetCode_33_108-83思路: 见图片: `83.jpeg` -### LeetCode-21思路: +### LeetCode_33_108-21思路: diff --git a/Week_01/id_3/LeetCode_142_3.c b/Week_01/id_3/LeetCode_142_3.c new file mode 100644 index 00000000..c626514a --- /dev/null +++ b/Week_01/id_3/LeetCode_142_3.c @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ +struct ListNode *detectCycle(struct ListNode *head) { + if (head == NULL || head->next == NULL) return NULL; + struct ListNode *fast = head, *slow = head; + + // 1st loop, check the existence of cycle + while (fast != NULL && fast->next != NULL) { + fast = fast->next->next; + slow = slow->next; + if (fast == slow) break; + } + + if (fast == NULL || fast->next == NULL) return NULL; + + // 2st loop, check the place of node which tail node connects to + struct ListNode *curr = head; + while (curr != slow) { + curr = curr->next; + slow = slow->next; + } + return curr; +} \ No newline at end of file diff --git a/Week_01/id_3/LeetCode_24_3.c b/Week_01/id_3/LeetCode_24_3.c new file mode 100644 index 00000000..0b12498a --- /dev/null +++ b/Week_01/id_3/LeetCode_24_3.c @@ -0,0 +1,53 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * struct ListNode *next; + * }; + */ + +struct ListNode { + int val; + struct ListNode *next; +}; + +struct ListNode* swapPairs(struct ListNode* head){ + struct ListNode* prev; + struct ListNode* p; + struct ListNode *pnext; + +//特殊处理 + if(!head){ + return head; + } + + p = head; + pnext = p->next; + if(!pnext){ + return head; + } + + p->next = pnext->next; + pnext->next = p; + head = pnext; + + prev = p; + p = p->next; + + while(p){ + pnext = p->next; + if(!pnext){ + return head; + } + + prev->next = pnext; + p->next = pnext->next; + pnext->next = p; + + prev = p; + p = p->next; + } + + return head; +} + diff --git a/Week_01/id_3/LeetCode_905_3.c b/Week_01/id_3/LeetCode_905_3.c new file mode 100644 index 00000000..f6c8b167 --- /dev/null +++ b/Week_01/id_3/LeetCode_905_3.c @@ -0,0 +1,31 @@ +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ + +int* sortArrayByParity(int* A, int ASize, int* returnSize){ + int* B = NULL; + int loop; + int evenIndex = 0; + int oddIndex = ASize - 1; + + if(!A){ + return NULL; + } + + B = malloc(ASize * sizeof(int)); + + for(loop = 0; loop < ASize; loop++){ + if(A[loop]%2){ + B[oddIndex] = A[loop]; + oddIndex--; + } + else{ + B[evenIndex] = A[loop]; + evenIndex++; + } + + } + + *returnSize = ASize; + return B; +} \ No newline at end of file diff --git a/Week_01/id_3/LeetCode_922_3.c b/Week_01/id_3/LeetCode_922_3.c new file mode 100644 index 00000000..cb9d80bd --- /dev/null +++ b/Week_01/id_3/LeetCode_922_3.c @@ -0,0 +1,31 @@ +/** + * Note: The returned array must be malloced, assume caller calls free(). + */ + +int* sortArrayByParityII(int* A, int ASize, int* returnSize){ + int* B = NULL; + int loop; + int evenIndex = 0; + int oddIndex = 1; + + if(!A){ + return NULL; + } + + B = malloc(ASize * sizeof(int)); + + for(loop = 0; loop < ASize; loop++){ + if(A[loop]%2){ + B[oddIndex] = A[loop]; + oddIndex+=2; + } + else{ + B[evenIndex] = A[loop]; + evenIndex+=2; + } + + } + + *returnSize = ASize; + return B; +} \ No newline at end of file diff --git a/Week_01/id_65/LeetCode_441_065.java b/Week_01/id_65/LeetCode_441_065.java new file mode 100644 index 00000000..e47b0fb3 --- /dev/null +++ b/Week_01/id_65/LeetCode_441_065.java @@ -0,0 +1,49 @@ +package com.imooc.activiti.helloworld; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * @author shironghui on 2019/4/18. + */ + + +class Solution14 { + /** + * 让我们按一定规律排列,第一行放1个,第二行放2个,以此类推, + * 问我们有多少行能放满。通过分析题目中的例子可以得知最后一行只有两种情况,放满和没放满。 + * 由于是按等差数列排放的,我们可以快速计算出前i行的硬币总数。 + * 我们先来看一种O(n)的方法,非常简单粗暴,就是从第一行开始,一行一行的从n中减去, + * 如果此时剩余的硬币没法满足下一行需要的硬币数了,我们之间返回当前行数即可 + * @param n + * @return + */ + public int arrangeCoins(int n) { + for(int i=1;i=0){ + n=n-i; + } + else { + return i-2; + } + } + return -1; + } +} + +public class TestArrangeCoins { + public static void main(String[] args) throws IOException { + BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); + String line; + while ((line = in.readLine()) != null) { + int n = Integer.parseInt(line); + + int ret = new Solution14().arrangeCoins(n); + + String out = String.valueOf(ret); + + System.out.print(out); + } + } +} diff --git a/Week_01/id_77/leet_code.md b/Week_01/id_77/leet_code.md new file mode 100644 index 00000000..5b66b7de --- /dev/null +++ b/Week_01/id_77/leet_code.md @@ -0,0 +1,804 @@ +### 链表 + +#### [83. 删除排序链表中的重复元素](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/) + +给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。 + +**示例 1:** + +``` +输入: 1->1->2 +输出: 1->2 +``` + +**示例 2:** + +``` +输入: 1->1->2->3->3 +输出: 1->2->3 +``` + +**代码:** + +```java +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode deleteDuplicates(ListNode head) { + ListNode pointer = head; + + while(pointer!=null){ + if(pointer.next!=null && pointer.val== pointer.next.val){ + pointer.next = pointer.next.next; + }else{ + pointer = pointer.next; + } + + } + return head; + } +} +``` + +#### [21. 合并两个有序链表](https://leetcode-cn.com/problems/merge-two-sorted-lists/) + +将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 + +**示例:** + +``` +输入:1->2->4, 1->3->4 +输出:1->1->2->3->4->4 +``` + +**代码:** + +```java +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode p1 = l1; + ListNode p2 = l2; + + if(l1==null){ + return l2; + }else if(l2==null){ + return l1; + } + ListNode listNew ; + if(p1.val2->3->4, 你应该返回 2->1->4->3. +``` + +**代码:** + +```java +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode swapPairs(ListNode head) { + if(head == null || head.next == null){ + return head; + } + ListNode slowPointer = head; + ListNode fastPointer = head.next; + ListNode p=fastPointer.next; + + fastPointer.next = slowPointer; + slowPointer.next =p; + + if(p == null){ + return fastPointer; + } + head = fastPointer; + ListNode headPointer = fastPointer.next; + + slowPointer = p; + fastPointer = p.next; + + while(slowPointer != null && fastPointer !=null){ + p=fastPointer.next; + fastPointer.next = slowPointer; + slowPointer.next =p; + headPointer.next = fastPointer; + headPointer = fastPointer.next; + slowPointer = p; + if(p!=null){ + fastPointer = p.next; + }else{ + fastPointer= null; + } + } + return head; + } +} +``` + +#### [142. 环形链表 II](https://leetcode-cn.com/problems/linked-list-cycle-ii/) + +给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 `null`。 + +为了表示给定链表中的环,我们使用整数 `pos` 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 `pos` 是 `-1`,则在该链表中没有环。 + +**说明:**不允许修改给定的链表。 + + + +**示例 1:** + +``` +输入:head = [3,2,0,-4], pos = 1 +输出:tail connects to node index 1 +解释:链表中有一个环,其尾部连接到第二个节点。 +``` + +![img](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist.png) + +**示例 2:** + +``` +输入:head = [1,2], pos = 0 +输出:tail connects to node index 0 +解释:链表中有一个环,其尾部连接到第一个节点。 +``` + +![img](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test2.png) + +**示例 3:** + +``` +输入:head = [1], pos = -1 +输出:no cycle +解释:链表中没有环。 +``` + +![img](https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2018/12/07/circularlinkedlist_test3.png) + +**代码:** + +```java +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution { + public ListNode detectCycle(ListNode head) { + Set cache = new HashSet(); + + while(head != null){ + if(cache.contains(head)){ + return head; + }else{ + cache.add(head); + head = head.next; + } + } + + return null; + + } +} +``` + +#### 25.k个一组翻转链表 + +给出一个链表,每 *k* 个节点一组进行翻转,并返回翻转后的链表。 + +*k* 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 *k* 的整数倍,那么将最后剩余节点保持原有顺序。 + +**示例 :** + +给定这个链表:`1->2->3->4->5` + +当 *k* = 2 时,应当返回: `2->1->4->3->5` + +当 *k* = 3 时,应当返回: `3->2->1->4->5` + +**说明 :** + +- 你的算法只能使用常数的额外空间。 +- **你不能只是单纯的改变节点内部的值**,而是需要实际的进行节点交换。 + +**代码:** + +```java +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode reverseKGroup(ListNode head, int k) { + + if(head == null){ + return null; + } + + //记录头节点 + ListNode headPointer = head; + //记录尾节点 + ListNode tailPointer = head; + //记录节点个数 + int n = 0; + while(tailPointer != null){ + n++; + if(n == k){ + //k以后的节点递归处理 + ListNode tmpTail = reverseKGroup(tailPointer.next,k); + //反转前k个节点 并链接尾节点 + reverseK(headPointer,k).next = tmpTail; + break; + } + tailPointer = tailPointer.next; + } + + if(n < k){ + //不足k个节点 直接返回头节点 + return headPointer; + }else{ + //有k个节点 返回反转后的头节点 即原来的尾节点 + return tailPointer; + } + + + } + + public ListNode reverseK(ListNode head, int k){ + if(k == 1){ + return head; + } + ListNode headNew = reverseK(head.next,k-1); + headNew.next = head; + head.next = null; + + return head; + } + + +} +``` + +### 数组 + +#### [905. 按奇偶排序数组](https://leetcode-cn.com/problems/sort-array-by-parity/) + +给定一个非负整数数组 `A`,返回一个由 `A` 的所有偶数元素组成的数组,后面跟 `A` 的所有奇数元素。 + +你可以返回满足此条件的任何数组作为答案。 + + + +**示例:** + +``` +输入:[3,1,2,4] +输出:[2,4,3,1] +输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。 +``` + + + +**提示:** + +1. `1 <= A.length <= 5000` +2. `0 <= A[i] <= 5000` + +```java +class Solution { + + + public int[] sortArrayByParity(int[] A) { + int[] arr =new int[A.length]; + int start = -1, end = A.length; + for(int i=0;i -1; + }else if(nums[0] == target){ + return true; + }else{ + while(left < end && nums[left] == nums[left+1]){ + left++; + } + while(right > 0 && nums[right] == nums[right-1]){ + right--; + } + } + + int maxIndex = -1; + + while(left <= right){ + mid = left + ((right - left) >> 1); + if(nums[mid] > nums[mid+1]){ + maxIndex = mid; + break; + }else if(nums[mid] < nums[0]){ + right = mid -1; + }else{ + left = mid +1; + } + } + if(target < nums[0]){ + return binarySearch(nums,target,maxIndex+1 ,end) > -1; + }else{ + return binarySearch(nums,target,0 ,maxIndex) > -1; + } + + } + + public int binarySearch(int[] nums,int target,int left ,int right){ + if(left>right){ + return -1; + } + int mid = left + ((right - left) >> 1); + if(nums[mid] > target){ + return binarySearch(nums,target,left,mid-1); + }else if(nums[mid] < target){ + return binarySearch(nums,target,mid + 1,right); + }else{ + return mid; + } + } +} +``` + +#### [153. 寻找旋转排序数组中的最小值](https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/) + +假设按照升序排序的数组在预先未知的某个点上进行了旋转。 + +( 例如,数组 `[0,1,2,4,5,6,7]` 可能变为 `[4,5,6,7,0,1,2]` )。 + +请找出其中最小的元素。 + +你可以假设数组中不存在重复元素。 + +**示例 1:** + +``` +输入: [3,4,5,1,2] +输出: 1 +``` + +**示例 2:** + +``` +输入: [4,5,6,7,0,1,2] +输出: 0 +``` + +**代码:** + +```java +class Solution { + public int findMin(int[] nums) { + int left = 0,right = nums.length-1,end = nums.length-1,mid; + if(end < 0){ + return -1; + }else if(end == 0){ + return nums[0]; + } + + if(nums[0]> 1); + if(nums[mid] > nums[mid+1]){ + maxIndex = mid; + break; + }else if(nums[mid] < nums[0]){ + right = mid -1; + }else{ + left = mid +1; + } + } + return nums[maxIndex + 1]; + } +} +``` + +#### [33. 搜索旋转排序数组](https://leetcode-cn.com/problems/search-in-rotated-sorted-array/) + +假设按照升序排序的数组在预先未知的某个点上进行了旋转。 + +( 例如,数组 `[0,1,2,4,5,6,7]` 可能变为 `[4,5,6,7,0,1,2]` )。 + +搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 `-1` 。 + +你可以假设数组中不存在重复的元素。 + +你的算法时间复杂度必须是 *O*(log *n*) 级别。 + +**示例 1:** + +``` +输入: nums = [4,5,6,7,0,1,2], target = 0 +输出: 4 +``` + +**示例 2:** + +``` +输入: nums = [4,5,6,7,0,1,2], target = 3 +输出: -1 +``` + +**代码:** + +```java +class Solution { + + public int search(int[] nums, int target) { + int left = 0,right = nums.length-1,end = nums.length-1,mid; + if(end < 0){ + return -1; + }else if(end == 0){ + return nums[0] == target ? 0 : -1; + } + + if(nums[0]> 1); + if(nums[mid] > nums[mid+1]){ + maxIndex = mid; + break; + }else if(nums[mid] < nums[0]){ + right = mid -1; + }else{ + left = mid +1; + } + } + + //比较最大值 进行区间二分查找 + if(target < nums[0]){ + return binarySearch(nums,target,maxIndex+1 ,end) ; + }else{ + return binarySearch(nums,target,0 ,maxIndex) ; + } + +} + +public int binarySearch(int[] nums,int target,int left ,int right){ + if(left>right){ + return -1; + } + int mid = left + ((right - left) >> 1); + if(nums[mid] > target){ + return binarySearch(nums,target,left,mid-1); + }else if(nums[mid] < target){ + return binarySearch(nums,target,mid + 1,right); + }else{ + return mid; + } +} +} +``` + +### 栈 + +#### [20. 有效的括号](https://leetcode-cn.com/problems/valid-parentheses/) + +```java +给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效。 + +有效字符串需满足: + +左括号必须用相同类型的右括号闭合。 +左括号必须以正确的顺序闭合。 +注意空字符串可被认为是有效字符串。 + +示例 1: + +输入: "()" +输出: true +示例 2: + +输入: "()[]{}" +输出: true +示例 3: + +输入: "(]" +输出: false +示例 4: + +输入: "([)]" +输出: false +示例 5: + +输入: "{[]}" +输出: true +``` + +**代码:** + +```java +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack(); + if(s.length() == 0){ + return true; + } + boolean flag = true; + char ch; + for(int i=0;i0){ + return false; + }else { + return flag; + } + } +} +``` + +#### [496. 下一个更大元素 I](https://leetcode-cn.com/problems/next-greater-element-i/) + +给定两个**没有重复元素**的数组 `nums1` 和 `nums2` ,其中`nums1` 是 `nums2` 的子集。找到 `nums1` 中每个元素在 `nums2` 中的下一个比其大的值。 + +`nums1` 中数字 **x** 的下一个更大元素是指 **x** 在 `nums2` 中对应位置的右边的第一个比 **x** 大的元素。如果不存在,对应位置输出-1。 + +**示例 1:** + +``` +输入: nums1 = [4,1,2], nums2 = [1,3,4,2]. +输出: [-1,3,-1] +解释: + 对于num1中的数字4,你无法在第二个数组中找到下一个更大的数字,因此输出 -1。 + 对于num1中的数字1,第二个数组中数字1右边的下一个较大数字是 3。 + 对于num1中的数字2,第二个数组中没有下一个更大的数字,因此输出 -1。 +``` + +**示例 2:** + +``` +输入: nums1 = [2,4], nums2 = [1,2,3,4]. +输出: [3,-1] +解释: + 对于num1中的数字2,第二个数组中的下一个较大数字是3。 + 对于num1中的数字4,第二个数组中没有下一个更大的数字,因此输出 -1。 +``` + +**注意:** + +1. `nums1`和`nums2`中所有元素是唯一的。 +2. `nums1`和`nums2` 的数组大小都不超过1000。 + +**代码:** + +```java +class Solution { + // 默认初始值 + private static final Integer VALUED_EFAULT = Integer.valueOf(-1); + + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + + Stack stack = new Stack(); + //利用哨兵来去除判空 + stack.push(Integer.MAX_VALUE); + Map map = new HashMap(); + int[] arr = new int[nums1.length]; + for(Integer node:nums2){ + while( node > stack.peek()){ + map.put(stack.pop(),node); + } + stack.push(node); + } + + for(int i = 0;i < nums1.length;i++){ + //获取为空时添加默认值 + arr[i] = map.getOrDefault(nums1[i],VALUED_EFAULT); + } + return arr; + + } +} +``` + diff --git a/Week_01/id_77/leetcode_153_077.java b/Week_01/id_77/leetcode_153_077.java new file mode 100644 index 00000000..ad770ea9 --- /dev/null +++ b/Week_01/id_77/leetcode_153_077.java @@ -0,0 +1,29 @@ +class Solution { + public int findMin(int[] nums) { + int left = 0,right = nums.length-1,end = nums.length-1,mid; + if(end < 0){ + return -1; + }else if(end == 0){ + return nums[0]; + } + + if(nums[0]> 1); + if(nums[mid] > nums[mid+1]){ + maxIndex = mid; + break; + }else if(nums[mid] < nums[0]){ + right = mid -1; + }else{ + left = mid +1; + } + } + return nums[maxIndex + 1]; + } +} \ No newline at end of file diff --git a/Week_01/id_77/leetcode_20_077.java b/Week_01/id_77/leetcode_20_077.java new file mode 100644 index 00000000..442cfb66 --- /dev/null +++ b/Week_01/id_77/leetcode_20_077.java @@ -0,0 +1,45 @@ +class Solution { + public boolean isValid(String s) { + Stack stack = new Stack(); + if(s.length() == 0){ + return true; + } + boolean flag = true; + char ch; + for(int i=0;i0){ + return false; + }else { + return flag; + } + } +} \ No newline at end of file diff --git a/Week_01/id_77/leetcode_21_077.java b/Week_01/id_77/leetcode_21_077.java new file mode 100644 index 00000000..fd74782b --- /dev/null +++ b/Week_01/id_77/leetcode_21_077.java @@ -0,0 +1,51 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode mergeTwoLists(ListNode l1, ListNode l2) { + ListNode p1 = l1; + ListNode p2 = l2; + + if(l1==null){ + return l2; + }else if(l2==null){ + return l1; + } + ListNode listNew ; + if(p1.val> 1); + if(nums[mid] > nums[mid+1]){ + maxIndex = mid; + break; + }else if(nums[mid] < nums[0]){ + right = mid -1; + }else{ + left = mid +1; + } + } + + //比较最大值 进行区间二分查找 + if(target < nums[0]){ + return binarySearch(nums,target,maxIndex+1 ,end) ; + }else{ + return binarySearch(nums,target,0 ,maxIndex) ; + } + +} + +public int binarySearch(int[] nums,int target,int left ,int right){ + if(left>right){ + return -1; + } + int mid = left + ((right - left) >> 1); + if(nums[mid] > target){ + return binarySearch(nums,target,left,mid-1); + }else if(nums[mid] < target){ + return binarySearch(nums,target,mid + 1,right); + }else{ + return mid; + } +} +} \ No newline at end of file diff --git a/Week_01/id_77/leetcode_33_077.java b/Week_01/id_77/leetcode_33_077.java new file mode 100644 index 00000000..c85c8c4e --- /dev/null +++ b/Week_01/id_77/leetcode_33_077.java @@ -0,0 +1,51 @@ +class Solution { + + public int search(int[] nums, int target) { + int left = 0,right = nums.length-1,end = nums.length-1,mid; + if(end < 0){ + return -1; + }else if(end == 0){ + return nums[0] == target ? 0 : -1; + } + + if(nums[0]> 1); + if(nums[mid] > nums[mid+1]){ + maxIndex = mid; + break; + }else if(nums[mid] < nums[0]){ + right = mid -1; + }else{ + left = mid +1; + } + } + + //比较最大值 进行区间二分查找 + if(target < nums[0]){ + return binarySearch(nums,target,maxIndex+1 ,end) ; + }else{ + return binarySearch(nums,target,0 ,maxIndex) ; + } + +} + +public int binarySearch(int[] nums,int target,int left ,int right){ + if(left>right){ + return -1; + } + int mid = left + ((right - left) >> 1); + if(nums[mid] > target){ + return binarySearch(nums,target,left,mid-1); + }else if(nums[mid] < target){ + return binarySearch(nums,target,mid + 1,right); + }else{ + return mid; + } +} +} \ No newline at end of file diff --git a/Week_01/id_77/leetcode_496_077.java b/Week_01/id_77/leetcode_496_077.java new file mode 100644 index 00000000..1f3ba616 --- /dev/null +++ b/Week_01/id_77/leetcode_496_077.java @@ -0,0 +1,26 @@ +class Solution { + // 默认初始值 + private static final Integer VALUED_EFAULT = Integer.valueOf(-1); + + public int[] nextGreaterElement(int[] nums1, int[] nums2) { + + Stack stack = new Stack(); + //利用哨兵来去除判空 + stack.push(Integer.MAX_VALUE); + Map map = new HashMap(); + int[] arr = new int[nums1.length]; + for(Integer node:nums2){ + while( node > stack.peek()){ + map.put(stack.pop(),node); + } + stack.push(node); + } + + for(int i = 0;i < nums1.length;i++){ + //获取为空时添加默认值 + arr[i] = map.getOrDefault(nums1[i],VALUED_EFAULT); + } + return arr; + + } +} \ No newline at end of file diff --git a/Week_01/id_77/leetcode_81_077.java b/Week_01/id_77/leetcode_81_077.java new file mode 100644 index 00000000..e8d2e5b3 --- /dev/null +++ b/Week_01/id_77/leetcode_81_077.java @@ -0,0 +1,57 @@ +class Solution { + public boolean search(int[] nums, int target) { + int left = 0,right = nums.length-1,end = nums.length-1,mid; + if(end < 0){ + return false; + }else if(end == 0){ + return nums[0] == target; + } + + if(nums[0] -1; + }else if(nums[0] == target){ + return true; + }else{ + while(left < end && nums[left] == nums[left+1]){ + left++; + } + while(right > 0 && nums[right] == nums[right-1]){ + right--; + } + } + + int maxIndex = -1; + + while(left <= right){ + mid = left + ((right - left) >> 1); + if(nums[mid] > nums[mid+1]){ + maxIndex = mid; + break; + }else if(nums[mid] < nums[0]){ + right = mid -1; + }else{ + left = mid +1; + } + } + if(target < nums[0]){ + return binarySearch(nums,target,maxIndex+1 ,end) > -1; + }else{ + return binarySearch(nums,target,0 ,maxIndex) > -1; + } + + } + + public int binarySearch(int[] nums,int target,int left ,int right){ + if(left>right){ + return -1; + } + int mid = left + ((right - left) >> 1); + if(nums[mid] > target){ + return binarySearch(nums,target,left,mid-1); + }else if(nums[mid] < target){ + return binarySearch(nums,target,mid + 1,right); + }else{ + return mid; + } + } +} \ No newline at end of file diff --git a/Week_01/id_77/leetcode_83_077.java b/Week_01/id_77/leetcode_83_077.java new file mode 100644 index 00000000..308f163c --- /dev/null +++ b/Week_01/id_77/leetcode_83_077.java @@ -0,0 +1,23 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { val = x; } + * } + */ +class Solution { + public ListNode deleteDuplicates(ListNode head) { + ListNode pointer = head; + + while(pointer!=null){ + if(pointer.next!=null && pointer.val== pointer.next.val){ + pointer.next = pointer.next.next; + }else{ + pointer = pointer.next; + } + + } + return head; + } +} \ No newline at end of file diff --git a/Week_01/id_77/leetcode_905_077.java b/Week_01/id_77/leetcode_905_077.java new file mode 100644 index 00000000..b3e0c9b4 --- /dev/null +++ b/Week_01/id_77/leetcode_905_077.java @@ -0,0 +1,20 @@ +class Solution { + + + public int[] sortArrayByParity(int[] A) { + int[] arr =new int[A.length]; + int start = -1, end = A.length; + for(int i=0;i + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +