Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Week_01/id_33/LeetCode_101_033.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* @lc app=leetcode id=101 lang=java
*
* [101] Symmetric Tree
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isSymmetric(TreeNode root) {
return isSym(root, root);
}

public boolean isSym(TreeNode leftNode, TreeNode rightNode) {
if(leftNode == null && rightNode == null)
return true;
if(leftNode == null || rightNode == null || leftNode.val != rightNode.val)
return false;
return isSym(leftNode.left, rightNode.right) && isSym(leftNode.right, rightNode.left);
}
}

24 changes: 24 additions & 0 deletions Week_01/id_33/LeetCode_104_033.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* @lc app=leetcode id=104 lang=java
*
* [104] Maximum Depth of Binary Tree
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int maxDepth(TreeNode root) {
if(root == null)
return 0;
int leftMaxDepth = maxDepth(root.left);
int rightMaxDepth = maxDepth(root.right);
return Math.max(leftMaxDepth, rightMaxDepth) + 1;
}
}

34 changes: 34 additions & 0 deletions Week_01/id_33/LeetCode_24_033.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* @lc app=leetcode id=24 lang=java
*
* [24] Swap Nodes in Pairs
*/
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode swapPairs(ListNode head) {
ListNode pre = new ListNode(0);
ListNode cur = head;
pre.next = head;
ListNode tmp = pre;
while(cur != null){
ListNode next = cur.next;
if(next != null){
cur.next = next.next;
next.next = cur;
pre.next = next;
pre = cur;
cur = cur.next;
} else
break;
}
return tmp.next;
}
}

21 changes: 21 additions & 0 deletions Week_01/id_33/LeetCode_26_033.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* @lc app=leetcode id=26 lang=java
*
* [26] Remove Duplicates from Sorted Array
*/
class Solution {
public int removeDuplicates(int[] nums) {
int len = nums.length;
if(len == 0)
return 0;
int cur = 0;
for(int i = 0; i < len; i++){
if(nums[i] != nums[cur]){
cur++;
nums[cur] = nums[i];
}
}
return cur + 1;
}
}

23 changes: 23 additions & 0 deletions Week_01/id_33/LeetCode_49_033.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

/*
* @lc app=leetcode id=49 lang=java
*
* [49] Group Anagrams
*/
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> sortedMap = new HashMap<>();
for (String word : strs) {
char[] charWord = word.toCharArray();
Arrays.sort(charWord);
String newStr = new String(charWord);
sortedMap.putIfAbsent(newStr,new ArrayList<>());
sortedMap.get(newStr).add(word);
}
return new ArrayList<>(sortedMap.values());
}
}

19 changes: 19 additions & 0 deletions Week_01/id_33/LeetCode_50_033.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* @lc app=leetcode id=50 lang=java
*
* [50] Pow(x, n)
*/
class Solution {
public double myPow(double x, int n) {
if(n == 0)
return 1;
if(n == 1)
return x;
double half = myPow(x, n/2);
if(n % 2 == 0)
return half * half;
else
return n > 0 ? half * half * x : half * half * (1/x);
}
}

Binary file added Week_01/id_33/数据结构与算法脑图.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.