forked from sduonline/sc-resources
增加淘宝群内容,修改部分文件组织
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<classpath>
|
||||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
|
||||
<classpathentry kind="src" path="src"/>
|
||||
<classpathentry kind="output" path="bin"/>
|
||||
</classpath>
|
||||
@@ -0,0 +1,17 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>00-leetcode</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -0,0 +1,11 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
|
||||
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
|
||||
org.eclipse.jdt.core.compiler.compliance=1.8
|
||||
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
|
||||
org.eclipse.jdt.core.compiler.debug.localVariable=generate
|
||||
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
|
||||
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
|
||||
org.eclipse.jdt.core.compiler.source=1.8
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
package 二叉树;
|
||||
|
||||
public class TreeNode {
|
||||
int val;
|
||||
TreeNode left;
|
||||
TreeNode right;
|
||||
TreeNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package 二叉树;
|
||||
|
||||
import java.util.LinkedList;
|
||||
import java.util.Queue;
|
||||
|
||||
/**
|
||||
* https://leetcode-cn.com/problems/invert-binary-tree/
|
||||
* @author MJ Lee
|
||||
*
|
||||
*/
|
||||
public class _226_翻转二叉树 {
|
||||
|
||||
// public TreeNode invertTree(TreeNode root) {
|
||||
// if (root == null) return root;
|
||||
//
|
||||
// TreeNode tmp = root.left;
|
||||
// root.left = root.right;
|
||||
// root.right = tmp;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
// invertTree(root.right);
|
||||
//
|
||||
// return root;
|
||||
// }
|
||||
|
||||
// public TreeNode invertTree(TreeNode root) {
|
||||
// if (root == null) return root;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
// invertTree(root.right);
|
||||
//
|
||||
// TreeNode tmp = root.left;
|
||||
// root.left = root.right;
|
||||
// root.right = tmp;
|
||||
//
|
||||
// return root;
|
||||
// }
|
||||
|
||||
// public TreeNode invertTree(TreeNode root) {
|
||||
// if (root == null) return root;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
//
|
||||
// TreeNode tmp = root.left;
|
||||
// root.left = root.right;
|
||||
// root.right = tmp;
|
||||
//
|
||||
// invertTree(root.left);
|
||||
//
|
||||
// return root;
|
||||
// }
|
||||
public TreeNode invertTree(TreeNode root) {
|
||||
if (root == null) return root;
|
||||
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.offer(root);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
TreeNode node = queue.poll();
|
||||
TreeNode tmp = node.left;
|
||||
node.left = node.right;
|
||||
node.right = tmp;
|
||||
|
||||
if (node.left != null) {
|
||||
queue.offer(node.left);
|
||||
}
|
||||
|
||||
if (node.right != null) {
|
||||
queue.offer(node.right);
|
||||
}
|
||||
}
|
||||
return root;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package 栈;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Stack;
|
||||
|
||||
public class _20_有效的括号 {
|
||||
private static HashMap<Character, Character> map = new HashMap<>();
|
||||
static {
|
||||
// key - value
|
||||
map.put('(', ')');
|
||||
map.put('{', '}');
|
||||
map.put('[', ']');
|
||||
}
|
||||
|
||||
public boolean isValid(String s) {
|
||||
Stack<Character> stack = new Stack<>();
|
||||
|
||||
int len = s.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = s.charAt(i);
|
||||
if (map.containsKey(c)) { // 左括号
|
||||
stack.push(c);
|
||||
} else { // 右括号
|
||||
if (stack.isEmpty()) return false;
|
||||
|
||||
if (c != map.get(stack.pop())) return false;
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isValid1(String s) {
|
||||
Stack<Character> stack = new Stack<>();
|
||||
|
||||
int len = s.length();
|
||||
for (int i = 0; i < len; i++) {
|
||||
char c = s.charAt(i);
|
||||
if (c == '(' || c == '{' || c == '[') { // 左括号
|
||||
stack.push(c);
|
||||
} else { // 右括号
|
||||
if (stack.isEmpty()) return false;
|
||||
|
||||
char left = stack.pop();
|
||||
if (left == '(' && c != ')') return false;
|
||||
if (left == '{' && c != '}') return false;
|
||||
if (left == '[' && c != ']') return false;
|
||||
}
|
||||
}
|
||||
return stack.isEmpty();
|
||||
}
|
||||
|
||||
public boolean isValid2(String s) {
|
||||
while (s.contains("{}")
|
||||
|| s.contains("[]")
|
||||
|| s.contains("()")) {
|
||||
s = s.replace("{}", "");
|
||||
s = s.replace("()", "");
|
||||
s = s.replace("[]", "");
|
||||
}
|
||||
return s.isEmpty();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package 栈;
|
||||
|
||||
import java.util.Stack;
|
||||
|
||||
public class _232_用栈实现队列 {
|
||||
private Stack<Integer> inStack;
|
||||
private Stack<Integer> outStack;
|
||||
|
||||
/** Initialize your data structure here. */
|
||||
public _232_用栈实现队列() {
|
||||
inStack = new Stack<>();
|
||||
outStack = new Stack<>();
|
||||
}
|
||||
|
||||
/** 入队 */
|
||||
public void push(int x) {
|
||||
inStack.push(x);
|
||||
}
|
||||
|
||||
/** 出队 */
|
||||
public int pop() {
|
||||
checkOutStack();
|
||||
return outStack.pop();
|
||||
}
|
||||
|
||||
/** 获取队头元素 */
|
||||
public int peek() {
|
||||
checkOutStack();
|
||||
return outStack.peek();
|
||||
}
|
||||
|
||||
/** 是否为空 */
|
||||
public boolean empty() {
|
||||
return inStack.isEmpty() && outStack.isEmpty();
|
||||
}
|
||||
|
||||
private void checkOutStack() {
|
||||
if (outStack.isEmpty()) {
|
||||
while (!inStack.isEmpty()) {
|
||||
outStack.push(inStack.pop());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package 链表;
|
||||
|
||||
public class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
ListNode(int x) {
|
||||
val = x;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package 链表;
|
||||
|
||||
public class _141_环形链表 {
|
||||
|
||||
public boolean hasCycle(ListNode head) {
|
||||
if (head == null || head.next == null) return false;
|
||||
|
||||
ListNode slow = head;
|
||||
ListNode fast = head.next;
|
||||
while (fast != null && fast.next != null) {
|
||||
slow = slow.next;
|
||||
fast = fast.next.next;
|
||||
|
||||
if (slow == fast) return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package 链表;
|
||||
|
||||
public class _206_反转链表 {
|
||||
|
||||
public ListNode reverseList(ListNode head) {
|
||||
if (head == null || head.next == null) return head;
|
||||
|
||||
ListNode newHead = reverseList(head.next);
|
||||
head.next.next = head;
|
||||
head.next = null;
|
||||
return newHead;
|
||||
}
|
||||
|
||||
|
||||
public ListNode reverseList2(ListNode head) {
|
||||
if (head == null || head.next == null) return head;
|
||||
|
||||
ListNode newHead = null;
|
||||
while (head != null) {
|
||||
ListNode tmp = head.next;
|
||||
head.next = newHead;
|
||||
newHead = head;
|
||||
head = tmp;
|
||||
}
|
||||
|
||||
return newHead;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package 链表;
|
||||
|
||||
/**
|
||||
* https://leetcode-cn.com/problems/delete-node-in-a-linked-list/
|
||||
* @author MJ Lee
|
||||
*
|
||||
*/
|
||||
public class _237_删除链表中的节点 {
|
||||
|
||||
public void deleteNode(ListNode node) {
|
||||
node.val = node.next.val;
|
||||
node.next = node.next.next;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user