diff --git a/Week_01/id_42/LeeCode_83_042.java b/Week_01/id_42/LeeCode_83_042.java new file mode 100644 index 00000000..d212686d --- /dev/null +++ b/Week_01/id_42/LeeCode_83_042.java @@ -0,0 +1,38 @@ +package list; + +public class LeeCode_83_042 { + + /* + * Definition for singly-linked list. + * */ + + public ListNode deleteDuplicates(ListNode head) { + ListNode stubNode = head; + if(head ==null){ + return null; + }else{ + ListNode ptemp= head.next; + int stub = head.val; + while(ptemp!=null){ + if(stub == ptemp.val){ + head.next = ptemp.next; + }else{ + stub = ptemp.val; + head = head.next; + } + ptemp = ptemp.next; + if(ptemp!=null){ + System.out.println(ptemp.val); + } + + + } + return stubNode; + } + } + + public static void main(String[] args){ + + } +} + diff --git a/Week_01/id_42/LeeCode_905_042.java b/Week_01/id_42/LeeCode_905_042.java new file mode 100644 index 00000000..7ed0a2dd --- /dev/null +++ b/Week_01/id_42/LeeCode_905_042.java @@ -0,0 +1,43 @@ +package array; + +public class LeeCode_905_042 { + public static int[] sortArrayByParity(int[] A) { + + int oddSize = 0; + + for(int i =0;i< A.length; i++){ + if(A[i]%2 !=0){ + oddSize++; + } + } + int start =0; + int end = A.length-1; + + if(oddSize > 0 && oddSize < A.length){ + while(oddSize >0 || start< end){ + if(A[start]%2 !=0){ + oddSize--; + for(int j =end; j> start; j--){ + if((A[j]%2) == 0){ + int temp = A[start]; + A[start] = A[j]; + A[j]= temp; + end = j-1; + break; + } + } + } + start ++; + } + + } + return A; + } + public static void main(String[] args){ + int[] A ={1,2,3,4,5,6,7,8,9,10}; + LeeCode_905_042.sortArrayByParity(A); + for(int i =0; i< A.length ;i++){ + System.out.println(A[i]); + } + } +} diff --git a/Week_02/id_42/NOTE.md b/Week_02/id_42/NOTE.md index c684e62f..c606e380 100644 --- a/Week_02/id_42/NOTE.md +++ b/Week_02/id_42/NOTE.md @@ -1 +1,4 @@ -# 学习笔记 \ No newline at end of file +# 学习笔记 +无感想,想学习套路,发现基础不牢,把自己套进去了。接着做题吧。 +面试别人的时候,用了链表的题目,候选人没有做出来, +自己调试代码的时候,死循环了。 diff --git a/Week_02/id_42/leecoce_692_042.java b/Week_02/id_42/leecoce_692_042.java new file mode 100644 index 00000000..5d737dc6 --- /dev/null +++ b/Week_02/id_42/leecoce_692_042.java @@ -0,0 +1,17 @@ +class leecode_692_042{ + List list = new ArrayList(); + public int findSecondMinimumValue(TreeNode root) { + if (root == null) return -1; + if (!list.contains(root.val)) list.add(root.val); + findSecondMinimumValue(root.left); + findSecondMinimumValue(root.right); + Collections.sort(list); + if (list.size() <= 1) { + return -1; + } else { + return list.get(1); + } + + } + +} \ No newline at end of file diff --git a/Week_02/id_42/leecode_671_042.java b/Week_02/id_42/leecode_671_042.java new file mode 100644 index 00000000..f27e0869 --- /dev/null +++ b/Week_02/id_42/leecode_671_042.java @@ -0,0 +1,44 @@ +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.TreeMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + +class leecode_671_042 { + public List topKFrequent(String[] words, int k) { + TreeMap wordsCount = new TreeMap(); + // statistic + for(String word:words){ + if(wordsCount.containsKey(word)){ + int count = wordsCount.get(word); + count++; + wordsCount.put(word, count); + }else{ + wordsCount.put(word, 1); + } + + } + + //sort + + List> list = new ArrayList>(wordsCount.entrySet()); + Collections.sort(list, new Comparator>() { + public int compare(Entry o1, + Entry o2) { + return o2.getValue().compareTo(o1.getValue()); + } + + }); + + List result = new ArrayList(); + + for(int i=0;i q; + final int k; + + public KthLargest(int k, int[] nums) { + this.k = k; + q = new PriorityQueue(k); + for (int i : nums) { + add(i); + } + } + + public int add(int val) { + if (q.size() < k) { + q.offer(val); + + } else if (q.peek() < val) { + q.poll(); + q.offer(val); + } + return q.peek(); + } + } +} \ No newline at end of file diff --git a/Week_03/id_42/leecode_997_042.java b/Week_03/id_42/leecode_997_042.java new file mode 100644 index 00000000..1fbae7e1 --- /dev/null +++ b/Week_03/id_42/leecode_997_042.java @@ -0,0 +1,14 @@ +class leecode_997_042 { + public int findJudge(int N, int[][] trust) { + int[] index = new int[N+1]; + for(int[] t : trust){ + index[t[0]] = -1; + if(index[t[1]] != -1) index[t[1]]++; + } + for(int i = 1; i <= N; i++){ + if(index[i] == N-1) return i; + } + + return -1; + } +} \ No newline at end of file