Skip to content
29 changes: 29 additions & 0 deletions Week_01/id_15/LeetCode_20_isValid_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.haotone.week_01;

import java.util.Stack;

class Solution {
public boolean isValid(String s) {

if(s == null || "".equals(s)) return true;
char[] charArray = s.toCharArray();
Stack sta = new Stack();
for(char a:charArray){

if(sta.isEmpty()){
sta.push(a);
continue;
}
char peek = (char)sta.peek();
if((peek=='(' && a==')')
|| (peek=='[' && a==']')
|| (peek=='{' && a=='}'))
sta.pop();
else
sta.push(a);
}
if(sta.isEmpty()) return true;
else return false;

}
}
40 changes: 40 additions & 0 deletions Week_01/id_15/LeetCode_21_mergeTwoLists_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.haotone.week_01;

/**
* 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) {

ListNode hummy = new ListNode(-1);
ListNode cur = hummy;
while(l1!=null || l2!=null){
if(l1 == null){
cur.next = l2;
cur = l2;
l2 = l2.next;
}else if(l2 == null){
cur.next = l1;
cur = l1;
l1 = l1.next;
}else{
if(l1.val>l2.val){
cur.next = l2;
cur = l2;
l2 = l2.next;
}else{
cur.next = l1;
cur = l1;
l1 = l1.next;
}
}

}
return hummy.next;
}
}
31 changes: 31 additions & 0 deletions Week_01/id_15/LeetCode_83_deleteDuplicates_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.haotone.week_01;

/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode deleteDuplicates(ListNode head) {
if(head == null) return null;
HashSet key = new HashSet();
key.add(head.val);
ListNode node=head.next;
ListNode pre = head;
while(node!=null){
if(key.contains(node.val)){
pre.next = pre.next.next;
}
else{
key.add(node.val);
pre = node;
}
node = node.next;
}
return head;

}
}
27 changes: 27 additions & 0 deletions Week_01/id_15/LeetCode_905_sortArrayByParity_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.haotone.week_01;

class Solution {
public int[] sortArrayByParity(int[] A) {
int befor = 0;
int after = A.length-1;
while(befor<after){

if(A[befor]%2 != 0 && A[after]%2 == 0){
int temp = A[befor];
A[befor] = A[after];
A[after] = temp;
befor++;
after--;
}
if(A[befor]%2 == 0){
befor++;
}
if(A[after]%2 != 0){
after--;
}

}
return A;

}
}
27 changes: 27 additions & 0 deletions Week_01/id_15/LeetCode_922_sortArrayByParityII_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.haotone.week_01;

class Solution {
public int[] sortArrayByParityII(int[] A) {

int ou = 0;
int ji = 1;
int n = A.length;
while(ji<=n-1 && ou<=n-2){


if(A[ji]%2!=0){
ji += 2;
}else if(A[ou]%2==0){
ou += 2;
}else if(A[ji]%2==0 && A[ou]%2!=0){
int temp = A[ji];
A[ji] = A[ou];
A[ou] = temp;
ji += 2;
ou += 2;
}
}
return A;

}
}
28 changes: 28 additions & 0 deletions Week_02/id_15/LeetCode_242_isAnagram_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.haotone.week_01;

import java.util.HashMap;

class Solution {
public boolean isAnagram(String s, String t) {
char[] sarr = s.toCharArray();
char[] tarr = t.toCharArray();
HashMap<Character,Integer> dic = new HashMap<Character,Integer>();
if(sarr.length != tarr.length) return false;
for(int i=0;i<sarr.length;i++){
char c = sarr[i];
if(dic.containsKey(c)) dic.put(c, dic.get(c)+1);
else dic.put(c, 1);
}
for(int i=0;i<tarr.length;i++){
char c = tarr[i];
if(dic.containsKey(c)){
dic.put(c, dic.get(c)-1);
if(dic.get(c)==0) dic.remove(c);
}
else return false;
}
if(dic.size()>0) return false;
return true;

}
}
54 changes: 54 additions & 0 deletions Week_02/id_15/LeetCode_783_minDiffInBST_15.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.haotone.week_01;

import java.util.HashMap;

/**
* Definition for a binary tree node.
* */
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}

class Solution {
public int minDiffInBST(TreeNode root) {
if(root == null) return Integer.MAX_VALUE;
if(root.left == null&&root.right == null) return Integer.MAX_VALUE;
if(root.left == null){
int ri = root.right.val-root.val;
int rimin = minDiffInBST(root.right);
int rile = Integer.MAX_VALUE;
if(root.right.left != null) {
rile = root.right.left.val-root.val;
}
return Math.min(Math.min(ri, rimin),rile);
}else if(root.right == null){
int le = root.val-root.left.val;
int lemin = minDiffInBST(root.left);
int leri = Integer.MAX_VALUE;
if(root.left.right != null) {
leri = root.val-root.left.right.val;
}
return Math.min(Math.min(le, lemin),leri);
}
int le = root.val-root.left.val;
int ri = root.right.val-root.val;
int cur = Math.min(le, ri);
int rimin = minDiffInBST(root.right);
int lemin = minDiffInBST(root.left);
int chimin = Math.min(rimin, lemin);
int leri = Integer.MAX_VALUE;
int rile = Integer.MAX_VALUE;
if(root.left.right != null) {
leri = root.val-root.left.right.val;
}
if(root.right.left != null) {
rile = root.right.left.val-root.val;
}
int aromin = Math.min(rile, leri);
return Math.min(Math.min(chimin,aromin),cur);

}
}