diff --git a/code/src/main/java/com/raorao/java/A.java b/code/src/main/java/com/raorao/java/BigEnd.java similarity index 93% rename from code/src/main/java/com/raorao/java/A.java rename to code/src/main/java/com/raorao/java/BigEnd.java index 1c0f279d..f310780a 100644 --- a/code/src/main/java/com/raorao/java/A.java +++ b/code/src/main/java/com/raorao/java/BigEnd.java @@ -1,6 +1,5 @@ package com.raorao.java; -import io.netty.buffer.ByteBuf; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.util.Arrays; @@ -11,7 +10,7 @@ import java.util.Arrays; * @author Xiong Raorao * @since 2018-08-10-23:27 */ -public class A { +public class BigEnd { public static void main(String[] args) { ByteBuffer bb = ByteBuffer.wrap(new byte[12]); diff --git a/code/src/main/java/com/raorao/leetcode/TreeNode.java b/code/src/main/java/com/raorao/leetcode/TreeNode.java new file mode 100644 index 00000000..e23dd03f --- /dev/null +++ b/code/src/main/java/com/raorao/leetcode/TreeNode.java @@ -0,0 +1,18 @@ +package com.raorao.leetcode; + +/** + * . + * + * @author Xiong Raorao + * @since 2018-09-02-19:31 + */ +public class TreeNode { + + public int val; + public TreeNode left; + public TreeNode right; + + TreeNode(int x) { + val = x; + } +} diff --git a/code/src/main/java/com/raorao/leetcode/q104/MaxDepth.java b/code/src/main/java/com/raorao/leetcode/q104/MaxDepth.java new file mode 100644 index 00000000..a2975443 --- /dev/null +++ b/code/src/main/java/com/raorao/leetcode/q104/MaxDepth.java @@ -0,0 +1,21 @@ +package com.raorao.leetcode.q104; + + +import com.raorao.leetcode.TreeNode; + +/** + * 求树的最大深度. + * + * @author Xiong Raorao + * @since 2018-09-02-19:30 + */ +public class MaxDepth { + + public int maxDepth(TreeNode root) { + if (root == null) { + return 0; + } + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } + +} diff --git a/code/src/main/java/com/raorao/leetcode/q111/BTreeDepth.java b/code/src/main/java/com/raorao/leetcode/q111/BTreeDepth.java index c68fd0e9..b20fa03f 100644 --- a/code/src/main/java/com/raorao/leetcode/q111/BTreeDepth.java +++ b/code/src/main/java/com/raorao/leetcode/q111/BTreeDepth.java @@ -1,6 +1,7 @@ package com.raorao.leetcode.q111; +import com.raorao.leetcode.TreeNode; import java.util.LinkedList; import java.util.Queue; @@ -43,15 +44,4 @@ public class BTreeDepth { } return level; } - - public class TreeNode { - - int val; - TreeNode left; - TreeNode right; - - TreeNode(int x) { - val = x; - } - } } diff --git a/code/src/main/java/com/raorao/leetcode/q160/LInkIntersection.java b/code/src/main/java/com/raorao/leetcode/q160/LInkIntersection.java new file mode 100644 index 00000000..bd965911 --- /dev/null +++ b/code/src/main/java/com/raorao/leetcode/q160/LInkIntersection.java @@ -0,0 +1,68 @@ +package com.raorao.leetcode.q160; + +/** + * 寻找链表相交的点. + * + * @author Xiong Raorao + * @since 2018-09-02-17:42 + */ +public class LInkIntersection { + + /** + * 第一种方法,两个链表逐个遍历 + */ + public ListNode getIntersectionNode(ListNode headA, ListNode headB) { + ListNode listA = headA; + ListNode listB = headB; + while (listA != listB) { + listA = listA == null ? headB : listA.next; + listB = listB == null ? headA : listB.next; + } + return listA; + } + + /** + * 第二种方法:将其中的一个链表的尾巴连接到另外一个头,寻找链表的环入口。 + */ + + public ListNode solution2(ListNode headA, ListNode headB) { + if(headA == null || headB == null){ + return null; + } + ListNode tailA = headA; + while (tailA.next != null) { + tailA = tailA.next; + } + tailA.next = headB; // A的结尾接上B,接下来寻找环节点 + ListNode slow = headA; + ListNode fast = headA; + while (fast != null && fast.next != null){ + slow = slow.next; + fast = fast.next.next; + if(slow == fast){ + break; + } + } + // 得到相遇点,slow置位链表头,fast和slow速度一致 + if(fast == null || fast.next == null){ + return null; + } + slow = headA; + while (slow != fast){ + slow = slow.next; + fast = fast.next; + } + return slow; + } + + static class ListNode { + + int val; + ListNode next; + + ListNode(int x) { + val = x; + next = null; + } + } +} diff --git a/code/src/main/java/com/raorao/leetcode/q628/MaximumProduct.java b/code/src/main/java/com/raorao/leetcode/q628/MaximumProduct.java new file mode 100644 index 00000000..7af37b82 --- /dev/null +++ b/code/src/main/java/com/raorao/leetcode/q628/MaximumProduct.java @@ -0,0 +1,45 @@ +package com.raorao.leetcode.q628; + +/** + * 求数组最大的三个数的乘积. + * + * @author Xiong Raorao + * @since 2018-09-02-17:20 + */ +public class MaximumProduct { + + public static void main(String[] args) { + + } + + public int maximumProduct(int[] nums) { + int m1 = Integer.MIN_VALUE; + int m2 = Integer.MIN_VALUE; + int m3 = Integer.MIN_VALUE; + int min1 = Integer.MAX_VALUE; + int min2 = Integer.MAX_VALUE; + for (int num : nums) { + if (num > m1) { + // 后移一位 + m3 = m2; + m2 = m1; + m1 = num; + } else if (num > m2) { + m3 = m2; + m2 = num; + } else if (num > m3) { + m3 = num; + } + + // 考虑到负数的可能 + if(num < min1){ + min2 = min1; + min1 = num; + }else if(num < min2){ + min2 = num; + } + } + return Math.max(m1*m2*m3, m1*min1*min2); + } + +}