Skip to content
Open
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
2 changes: 2 additions & 0 deletions Week_01/id_1/NOTE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# 学习笔记
=======
### 一. 第一周学习总结

#### 1. 关于链表题目的做题总结
Expand Down
39 changes: 39 additions & 0 deletions Week_01/id_80/LeetCode_20_80.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
*
* https://leetcode-cn.com/problems/valid-parentheses/
*/
class Solution {
private HashMap<Character, Character> mappings;
public Solution(){
this.mappings = new HashMap<Character, Character>();
this.mappings.put('(', ')');
this.mappings.put('{', '}');
this.mappings.put('[', ']');

}
public boolean isValid(String s) {
Stack<Character> stack=new Stack<>();
char[] chars = s.toCharArray();
for(char c:chars){
if(stack.size()==0){
stack.push(c);
}else if(isSym(stack.peek(),c)){
stack.pop();
}else{
stack.push(c);
}
}
return stack.isEmpty();
}

private boolean isSym(char c1, char c2) {
if(mappings.containsKey(c1)){
if(mappings.get(c1)!=c2){
return false;
}else{
return true;
}
}
return false;
}
}
42 changes: 42 additions & 0 deletions Week_01/id_80/LeetCode_21_80.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* https://leetcode-cn.com/problems/merge-two-sorted-lists/
*
* Definition for singly-linked list.
* public class ListNode { int val; ListNode
* next; ListNode(int x) { val = x; } }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if (l2 == null)
return l1;
if (l1 == null)
return l2;
ListNode p1 = l1;
ListNode p2 = l2;
ListNode start;
if (l1.val < l2.val) {
start = l1;
} else {
start = l2;
}
do {
if (p2.val <= p1.val) {
while (p2.next != null && p2.next.val <= p1.val) {
p2 = p2.next;
}
l2 = p2.next;
p2.next = p1;
} else {
while (p1.next != null && p1.next.val < p2.val) {
p1 = p1.next;
}
l2 = p2.next;
p2.next = p1.next;
p1.next = p2;
}

p2 = l2;
} while (p2 != null);
return start;
}
}
23 changes: 23 additions & 0 deletions Week_01/id_80/LeetCode_83_80.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list/
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
ListNode current=head;
while(current!=null&&current.next!=null){
ListNode next=current.next;
if(current.val==next.val){
current.next=next.next;
}else{
current=current.next;
}
}
return head;
}
}
25 changes: 25 additions & 0 deletions Week_01/id_80/LeetCode_905_80.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
public int[] sortArrayByParity(int[] A) {
int length=A.length;
if(length>=1&&length<=5000){
int[] B=new int[length];
int l=0;
int r=0;
for(int i=0;i<length;i++){
int value=A[i];
//如果为奇数 放到后面
if(value%2==1){
B[length-1-l]=value;
l++;

}else{
B[r]=value;
r++;

}
}
return B;
}
return null;
}
}
8 changes: 7 additions & 1 deletion Week_01/id_80/NOTE.md
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
# 学习笔记
# 学习笔记

# 作业总结
1. leetcode 由于是第一次刷,有些题目的套路还有点儿生疏,有些题目看后没有思路。目前只做了一些简单有些思路的题目,希望自己后面几周,能把题目的难度提升上去。
2. 有些题目查看其他同学提供的解题方案的时候,能发现的bug集中出现在对边界情况考虑,所以自己要在这方面特别注意。
3. leetcode 21题(合并有序链表),针对空间复杂度和时间复杂度,可以提交两种解决思路的答案。但在现实场景中如何更高效的运用和取舍,自己这点要逐渐的积累和加强相关意识。

39 changes: 39 additions & 0 deletions Week_02/id_80/LeetCode_242_80.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
class Solution {
public boolean isAnagram(String s, String t) {
if (s == null || t == null) {
return false;
}
if (s.length() == t.length()) {
HashMap<Character, Integer> sMap = new HashMap<>();
//HashMap<Character, Integer> tMap = new HashMap<>();
char[] chars = s.toCharArray();
char[] chart = t.toCharArray();
for (char cs:chars){
if (sMap.containsKey(cs)) {
int count = sMap.get(cs);
count++;
sMap.put(cs, count);
} else {
sMap.put(cs, 1);
}
}
for (char ct:chart){
if(sMap.containsKey(ct)){
int count=sMap.get(ct);
count--;
if(count==0){
sMap.remove(ct);
}else{
sMap.put(ct,count);
}
}else{
return false;
}
}
return sMap.keySet().size() == 0;
} else {
return false;
}

}
}
Empty file.
58 changes: 58 additions & 0 deletions Week_02/id_80/LeetCode_692_80.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Solution {


public List<String> topKFrequent1(String[] words, int k) {
Map<String, Integer> count = new HashMap();
for (String word: words) {
count.put(word, count.getOrDefault(word, 0) + 1);
}
List<String> candidates = new ArrayList(count.keySet());
Collections.sort(candidates, (w1, w2) -> count.get(w1).equals(count.get(w2)) ?
w1.compareTo(w2) : count.get(w2) - count.get(w1));

return candidates.subList(0, k);
}

public List<String> topKFrequent(String[] words, int k) {
Map<String, Integer> countMap = new HashMap<>();
for (String str : words) {
Integer count = countMap.get(str);
if (count != null) {
count++;
} else {
count = 1;
}
countMap.put(str, count);
}
Map<Integer, List<String>> listMap = new HashMap<>();

for (String key : countMap.keySet()) {
Integer count = countMap.get(key);
List<String> stringList = listMap.get(count);
if (stringList == null) {
stringList = new ArrayList<>();
}
stringList.add(key);
listMap.put(count, stringList);
}

Set<Integer> keySet = listMap.keySet();
Object[] keyArray = keySet.toArray();
Arrays.sort(keyArray);
List<String> resultList = new ArrayList<>();
for (int i = keyArray.length - 1; i >= 0; i--) {
Object count = keyArray[i];
List<String> sl = listMap.get(count);
Object[] ss = sl.toArray();
Arrays.sort(ss);
for (Object s : ss) {
if (resultList.size() < k) {
resultList.add(String.valueOf(s));
} else {
return resultList;
}
}
}
return resultList;
}
}
48 changes: 48 additions & 0 deletions Week_02/id_80/LeetCode_783_80.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {

int last = -1000000;
int res = 1000000;
public int minDiffInBST(TreeNode root) {
if (root == null) {
return 0;
}
minDiffInBST(root.left);
res = Math.min(root.val - last, res);
last = root.val;
minDiffInBST(root.right);
return res;
}


//此答案对应题目:返回相邻两节点差的最小值。
public int minDiffInBST1(TreeNode root) {
int minDiff=getDiff(root,0);
return minDiff;
}

public int getDiff(TreeNode root,int val){
if(root!=null){
int currentMin=Math.abs(root.val-val);
if(root.left!=null){
currentMin=Math.min(getDiff(root.left,root.val),currentMin);
}
if(root.right!=null){
currentMin=Math.min(getDiff(root.right,root.val),currentMin);
}

return currentMin;
}else{
return Integer.MAX_VALUE;
}

}
}
Loading