| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -17,6 +17,6 @@ | |||
| 17 | 17 | ||
| 18 | 18 | ## 注意事项 | |
| 19 | 19 | ||
| 20 | - 1. **代码文件命名规则:**`LeetCode_题目序号_学号`,比如学号为 `0` 的学员完成 [LeetCode 的第 2 题](https://leetcode.com/problems/add-two-numbers/description/) 后,请将代码文件名保存为 `LeetCode_2_0.py` (假设你使用的是 Python 语言)。 | ||
| 20 | + 1. **代码文件命名规则:**`LeetCode_题目序号_学号`,比如学号为 `0` 的学员完成 [LeetCode_33_108 的第 2 题](https://leetcode.com/problems/add-two-numbers/description/) 后,请将代码文件名保存为 `LeetCode_2_0.py` (假设你使用的是 Python 语言)。 | ||
| 21 | 21 | 2. **作业公布地址:** 我们会在置顶的 issue 中公布当周的算法练习题以及其他注意事项。 | |
| 22 | 22 | 3. 如果对 Git 和 GitHub 不太了解,请参考 [Git 官方文档](https://git-scm.com/book/zh/v2) 或者极客时间的[《玩转 Git 三剑客》](https://time.geekbang.org/course/intro/145)视频课程。 | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,29 @@ | |||
| 1 | + /** | ||
| 2 | + * Definition for singly-linked list. | ||
| 3 | + * public class ListNode { | ||
| 4 | + * int val; | ||
| 5 | + * ListNode next; | ||
| 6 | + * ListNode(int x) { val = x; } | ||
| 7 | + * } | ||
| 8 | + */ | ||
| 9 | + class Solution { | ||
| 10 | + public ListNode mergeTwoLists(ListNode n1, ListNode n2) { | ||
| 11 | + ListNode result = new ListNode(0); | ||
| 12 | + ListNode n0 = result; | ||
| 13 | + while (n1 != null && n1 != null) { | ||
| 14 | + //System.out.println("n1-----" + n1.val + "--------n2-------" + n2.val | ||
| 15 | + if (n1.val < n2.val) { | ||
| 16 | + n0.next = new ListNode(n1.val); | ||
| 17 | + n1 = n1.next; | ||
| 18 | + } else { | ||
| 19 | + n0.next = new ListNode(n2.val); | ||
| 20 | + n2 = n2.next; | ||
| 21 | + } | ||
| 22 | + | ||
| 23 | + n0 = n0.next; | ||
| 24 | + } | ||
| 25 | + if(n1 != null) n0.next = n1; | ||
| 26 | + else n0.next = n2; | ||
| 27 | + return result.next; | ||
| 28 | + } | ||
| 29 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,28 @@ | |||
| 1 | + /** | ||
| 2 | + * Definition for singly-linked list. | ||
| 3 | + * public class ListNode { | ||
| 4 | + * int val; | ||
| 5 | + * ListNode next; | ||
| 6 | + * ListNode(int x) { val = x; } | ||
| 7 | + * } | ||
| 8 | + */ | ||
| 9 | + class Solution { | ||
| 10 | + public ListNode deleteDuplicates(ListNode head) { | ||
| 11 | + ListNode pre = head; | ||
| 12 | + ListNode pre_next = pre.next; | ||
| 13 | + if(pre == null || pre_next == null){ | ||
| 14 | + return head; | ||
| 15 | + } | ||
| 16 | + while(pre_next != null){ | ||
| 17 | + if(pre.val == pre_next.val){ | ||
| 18 | + pre_next = pre_next.next; | ||
| 19 | + pre = pre_next; | ||
| 20 | + }else{ | ||
| 21 | + pre = pre_next; | ||
| 22 | + pre_next = pre_next.next; | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + return head; | ||
| 26 | + | ||
| 27 | + } | ||
| 28 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,77 @@ | |||
| 1 | + /** | ||
| 2 | + * @author zhangruihao.zhang | ||
| 3 | + * @version v1.0.0 | ||
| 4 | + * @since 2019/04/24 | ||
| 5 | + */ | ||
| 6 | + public class LeetCode_153_108 { | ||
| 7 | + | ||
| 8 | + class Solution1 { | ||
| 9 | + public int findMin(int[] nums) { | ||
| 10 | + if(nums.length == 1){ | ||
| 11 | + return nums[0]; | ||
| 12 | + } | ||
| 13 | + if(nums.length == 2){ | ||
| 14 | + return nums[0] > nums[1] ? nums[1] : nums[0]; | ||
| 15 | + } | ||
| 16 | + if(nums[0] < nums[nums.length-1]){ | ||
| 17 | + return nums[0]; | ||
| 18 | + } | ||
| 19 | + int min = 0; | ||
| 20 | + int high = nums.length - 1; | ||
| 21 | + int mid = 0; | ||
| 22 | + while(min <= high){ | ||
| 23 | + //针对1、2、3这种情况 | ||
| 24 | + if(min == high){ | ||
| 25 | + return nums[min]; | ||
| 26 | + } | ||
| 27 | + mid = min + ((high-min) >> 1); | ||
| 28 | + if(nums[mid] > nums[high]){ | ||
| 29 | + if(nums[mid+1] < nums[mid]){ | ||
| 30 | + return nums[mid+1]; | ||
| 31 | + } | ||
| 32 | + min = mid + 1; | ||
| 33 | + continue; | ||
| 34 | + } | ||
| 35 | + if(nums[mid] < nums[min]){ | ||
| 36 | + if(nums[mid-1] > nums[mid]){ | ||
| 37 | + return nums[mid]; | ||
| 38 | + } | ||
| 39 | + high = mid - 1; | ||
| 40 | + } | ||
| 41 | + } | ||
| 42 | + return -1; | ||
| 43 | + } | ||
| 44 | + | ||
| 45 | + //二分查找 | ||
| 46 | + //分为两种情况 | ||
| 47 | + //mid比最后一个元素大,判断它右边的值是否小于它,如果小于的话就返回右边的值,否则的话,min取mid+1 | ||
| 48 | + //mid比第一个元素小,判断它左边的值是否比它大,如果大的话,直接返回它自己,否则的话high取mid-1 | ||
| 49 | + } | ||
| 50 | + | ||
| 51 | + class Solution2 { | ||
| 52 | + public int findMin(int[] nums) { | ||
| 53 | + if(nums.length == 1){ | ||
| 54 | + return nums[0]; | ||
| 55 | + } | ||
| 56 | + int min = 0; | ||
| 57 | + int high = nums.length - 1; | ||
| 58 | + int mid = 0; | ||
| 59 | + while(min < high){ | ||
| 60 | + mid = min + ((high-min) >> 1); | ||
| 61 | + if(nums[mid] > nums[high]){ | ||
| 62 | + min = mid + 1; | ||
| 63 | + } else { | ||
| 64 | + high = mid; | ||
| 65 | + } | ||
| 66 | + } | ||
| 67 | + return nums[min]; | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | + //二分查找 | ||
| 71 | + //分为两种情况 | ||
| 72 | + //mid比最后一个元素大,判断它右边的值是否小于它,如果小于的话就返回右边的值,否则的话,min取mid+1 | ||
| 73 | + //mid比第一个元素小,判断它左边的值是否比它大,如果大的话,直接返回它自己,否则的话high取mid-1 | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + } | ||
| 77 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,49 @@ | |||
| 1 | + import org.junit.Test; | ||
| 2 | + | ||
| 3 | + import java.util.Stack; | ||
| 4 | + | ||
| 5 | + /** | ||
| 6 | + * @author zhangruihao.zhang | ||
| 7 | + * @version v1.0.0 | ||
| 8 | + * @since 2019/04/29 | ||
| 9 | + */ | ||
| 10 | + public class LeetCode_20_108 { | ||
| 11 | + public static void main(String[] args) { | ||
| 12 | + String str = "()[]{}"; | ||
| 13 | + System.out.println(isValid(str)); | ||
| 14 | + } | ||
| 15 | + public static boolean isValid(String s) { | ||
| 16 | + Stack stack = new Stack(); | ||
| 17 | + for(int i=0;i<s.length();i++){ | ||
| 18 | + char c = s.charAt(i); | ||
| 19 | + if(stack.peek().equals(anotherChar(c))){ | ||
| 20 | + stack.pop(); | ||
| 21 | + }else{ | ||
| 22 | + stack.push(c); | ||
| 23 | + } | ||
| 24 | + } | ||
| 25 | + return stack.isEmpty(); | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + private static char anotherChar(char c){ | ||
| 29 | + if(c == '('){ | ||
| 30 | + return ')'; | ||
| 31 | + } | ||
| 32 | + if(c == ')'){ | ||
| 33 | + return '('; | ||
| 34 | + } | ||
| 35 | + if(c == '{'){ | ||
| 36 | + return '}'; | ||
| 37 | + } | ||
| 38 | + if(c == '}'){ | ||
| 39 | + return '{'; | ||
| 40 | + } | ||
| 41 | + if(c == '['){ | ||
| 42 | + return ']'; | ||
| 43 | + } | ||
| 44 | + if(c == ']'){ | ||
| 45 | + return '['; | ||
| 46 | + } | ||
| 47 | + return '0'; | ||
| 48 | + } | ||
| 49 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,75 @@ | |||
| 1 | + /** | ||
| 2 | + * @author zhangruihao.zhang | ||
| 3 | + * @version v1.0.0 | ||
| 4 | + * @since 2019/04/29 | ||
| 5 | + */ | ||
| 6 | + public class LeetCode_33_108 { | ||
| 7 | + class Solution { | ||
| 8 | + public int search(int[] nums, int target) { | ||
| 9 | + if(nums == null || nums.length == 0){ | ||
| 10 | + return -1; | ||
| 11 | + } | ||
| 12 | + | ||
| 13 | + if(nums[0] == target){ | ||
| 14 | + return 0; | ||
| 15 | + } | ||
| 16 | + if(nums[nums.length - 1] == target){ | ||
| 17 | + return nums.length - 1; | ||
| 18 | + } | ||
| 19 | + | ||
| 20 | + int end = nums.length - 1; | ||
| 21 | + int min = min(nums); | ||
| 22 | + //如果没有旋转,并且最大的数小于target | ||
| 23 | + if(min == 0 && nums[end]<target){ | ||
| 24 | + return -1; | ||
| 25 | + } | ||
| 26 | + | ||
| 27 | + if(nums[end] > target){ | ||
| 28 | + //target处于旋转的后半段 | ||
| 29 | + return binarySerach(nums,min,end,target); | ||
| 30 | + }else{ | ||
| 31 | + //target有可能处于旋转的前半段 | ||
| 32 | + return binarySerach(nums,0,min-1,target); | ||
| 33 | + } | ||
| 34 | + } | ||
| 35 | + | ||
| 36 | + //查找旋转点 | ||
| 37 | + private int min(int[] nums){ | ||
| 38 | + int low = 0; | ||
| 39 | + int high = nums.length - 1; | ||
| 40 | + int mid; | ||
| 41 | + while(low < high){ | ||
| 42 | + mid = low + ((high-low)>>1); | ||
| 43 | + if(nums[mid]>nums[high]){ | ||
| 44 | + low = mid + 1; | ||
| 45 | + }else{ | ||
| 46 | + high = mid; | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | + return low; | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + //二分查找 | ||
| 53 | + private int binarySerach(int[] nums,int left,int right,int target){ | ||
| 54 | + int mid = 0; | ||
| 55 | + while(left<right){ | ||
| 56 | + mid = left + ((right - left)>>1); | ||
| 57 | + if(nums[mid]==target){ | ||
| 58 | + return mid; | ||
| 59 | + } | ||
| 60 | + if(nums[mid]<target){ | ||
| 61 | + left = mid + 1; | ||
| 62 | + continue; | ||
| 63 | + } | ||
| 64 | + if(nums[mid]>target){ | ||
| 65 | + right = mid - 1; | ||
| 66 | + continue; | ||
| 67 | + } | ||
| 68 | + } | ||
| 69 | + if(nums[left] == target){ | ||
| 70 | + return left; | ||
| 71 | + } | ||
| 72 | + return -1; | ||
| 73 | + } | ||
| 74 | + } | ||
| 75 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,33 @@ | |||
| 1 | + import java.util.Stack; | ||
| 2 | + | ||
| 3 | + /** | ||
| 4 | + * @author zhangruihao.zhang | ||
| 5 | + * @version v1.0.0 | ||
| 6 | + * @since 2019/04/29 | ||
| 7 | + */ | ||
| 8 | + public class LeetCode_503_108 { | ||
| 9 | + class Solution { | ||
| 10 | + public int[] nextGreaterElements(int[] nums) { | ||
| 11 | + | ||
| 12 | + Stack<Integer> stack = new Stack<>(); | ||
| 13 | + int index[] = new int[nums.length]; | ||
| 14 | + for (int i = 0; i < index.length; i++) { | ||
| 15 | + index[i] = -1; | ||
| 16 | + } | ||
| 17 | + if (nums.length == 1) { | ||
| 18 | + return index; | ||
| 19 | + } | ||
| 20 | + int count = 0; | ||
| 21 | + while (count < 2) { | ||
| 22 | + for (int i = 0; i < nums.length; i++) { | ||
| 23 | + while (!stack.isEmpty() && nums[i] > nums[stack.peek()]) { | ||
| 24 | + index[stack.pop()] = nums[i]; | ||
| 25 | + } | ||
| 26 | + stack.push(i); | ||
| 27 | + } | ||
| 28 | + count++; | ||
| 29 | + } | ||
| 30 | + return index; | ||
| 31 | + } | ||
| 32 | + } | ||
| 33 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,50 @@ | |||
| 1 | + /** | ||
| 2 | + * @author zhangruihao.zhang | ||
| 3 | + * @version v1.0.0 | ||
| 4 | + * @since 2019/05/03 | ||
| 5 | + */ | ||
| 6 | + public class LeetCode_687_108 { | ||
| 7 | + /** | ||
| 8 | + * Definition for a binary tree node. | ||
| 9 | + * public class TreeNode { | ||
| 10 | + * int val; | ||
| 11 | + * TreeNode left; | ||
| 12 | + * TreeNode right; | ||
| 13 | + * TreeNode(int x) { val = x; } | ||
| 14 | + * } | ||
| 15 | + */ | ||
| 16 | + class Solution { | ||
| 17 | + int maxLen = 0; | ||
| 18 | + public int longestUnivaluePath(TreeNode root) { | ||
| 19 | + if(root == null){ | ||
| 20 | + return 0; | ||
| 21 | + } | ||
| 22 | + longest(root); | ||
| 23 | + return maxLen; | ||
| 24 | + } | ||
| 25 | + | ||
| 26 | + private int longest(TreeNode root){ | ||
| 27 | + if(root == null){ | ||
| 28 | + return 0; | ||
| 29 | + } | ||
| 30 | + int leftLen=longest( root.left); | ||
| 31 | + int rightLen=longest( root.right); | ||
| 32 | + int leftUnivalueLen = 0; | ||
| 33 | + int rightUnivalue = 0; | ||
| 34 | + if(root.left != null && root.left.val == root.val){ | ||
| 35 | + leftUnivalueLen = leftLen + 1; | ||
| 36 | + } | ||
| 37 | + | ||
| 38 | + if(root.right != null && root.right.val == root.val){ | ||
| 39 | + rightUnivalue = rightLen + 1; | ||
| 40 | + } | ||
| 41 | + | ||
| 42 | + maxLen = max(maxLen , leftUnivalueLen + rightUnivalue ); | ||
| 43 | + return max(leftUnivalueLen,rightUnivalue); | ||
| 44 | + } | ||
| 45 | + | ||
| 46 | + private int max(int x,int y){ | ||
| 47 | + return x>y ? x : y; | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments