This commit is contained in:
xiongraorao
2018-09-02 21:25:44 +08:00
parent 2c7460783c
commit 75f2e9c31e
6 changed files with 154 additions and 13 deletions

View File

@ -1,6 +1,5 @@
package com.raorao.java; package com.raorao.java;
import io.netty.buffer.ByteBuf;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
import java.util.Arrays; import java.util.Arrays;
@ -11,7 +10,7 @@ import java.util.Arrays;
* @author Xiong Raorao * @author Xiong Raorao
* @since 2018-08-10-23:27 * @since 2018-08-10-23:27
*/ */
public class A { public class BigEnd {
public static void main(String[] args) { public static void main(String[] args) {
ByteBuffer bb = ByteBuffer.wrap(new byte[12]); ByteBuffer bb = ByteBuffer.wrap(new byte[12]);

View File

@ -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;
}
}

View File

@ -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;
}
}

View File

@ -1,6 +1,7 @@
package com.raorao.leetcode.q111; package com.raorao.leetcode.q111;
import com.raorao.leetcode.TreeNode;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Queue; import java.util.Queue;
@ -43,15 +44,4 @@ public class BTreeDepth {
} }
return level; return level;
} }
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
} }

View File

@ -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;
}
}
}

View File

@ -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);
}
}