Skip to content

Instantly share code, notes, and snippets.

View forkercat's full-sized avatar
🉑
一杯咖啡、一只猫

Junhao Wang forkercat

🉑
一杯咖啡、一只猫
View GitHub Profile
@forkercat
forkercat / Solution.java
Created November 15, 2019 17:22
953. Verifying an Alien Dictionary
class Solution {
public boolean isAlienSorted(String[] words, String order) {
if (words == null || words.length == 0 || order == null || order.length() == 0) {
return false;
}
Map<Character, Integer> orderRule = new HashMap<>();
for (int i = 0; i < order.length(); ++i) {
char ch = order.charAt(i);
orderRule.put(ch, i);
}
@forkercat
forkercat / Solution.java
Created November 15, 2019 08:32
Alien Dictionary
class Solution {
public String alienOrder(String[] words) {
if (words == null || words.length == 0) {
return "";
}
Map<Character, Set<Character>> graph = new HashMap<>(); // List -> Set?
Map<Character, Integer> indegree = new HashMap<>();
@forkercat
forkercat / Solution.java
Created November 15, 2019 04:09
937. Reorder Data in Log Files
class Solution {
public String[] reorderLogFiles(String[] logs) {
if (logs == null) {
return null;
}
int n = logs.length;
Map<String, String[]> map = new HashMap<>();
for (String log : logs) {
map.put(log, log.split(" ", 2)); // only consider the first word
@forkercat
forkercat / ReverseLinkedList.java
Created October 19, 2019 08:48
ReverseLinkedList
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
@forkercat
forkercat / MaxStack.java
Created October 18, 2019 06:56
not correct~
class Solution {
class ListNode {
int val;
ListNode oldNode = null; // point to old max node
ListNode prev = null;
ListNode next = null;
ListNode(int _val) { val = _val; }
}
ListNode dummy = new ListNode(0);
public class BuyAndSellStock {
/**
* Brute-Force
* Time: O(N^2)
*/
public static double maxProfitBruteForce(List<Double> prices) {
double maxProfit = 0.0;
for (int i = 0; i < prices.size(); ++i) {
for (int j = i; j < prices.size(); ++j) {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> numList = new ArrayList<>();
result.add(new ArrayList<>()); // empty set
subsets(0, nums, numList, result);
return result;
}
private void subsets(int offset, int[] nums, List<Integer> numList, List<List<Integer>> result) {
@forkercat
forkercat / Solution.java
Created September 15, 2019 22:54
Permutaitons
class Solution {
public List<List<Integer>> permute(int[] nums) {
List<List<Integer>> result = new ArrayList<>();
permute(0, nums, result);
return result;
}
private void permute(int depth, int[] nums, List<List<Integer>> result) {
// base case
import java.util.ArrayList;
import java.util.List;
/**
* Created by JunhaoW on 05/06/2019
*/
public class MyBTree<Key extends Comparable<Key>, Value> {
/**
* 2-3 tree: Order-of-3, 3 children max
@forkercat
forkercat / SLList_Sentinel.java
Created April 1, 2019 02:31
Sentinel problem?
/**
* Created by JunhaoW on 03/31/2019
*/
public class SLList_Sentinel {
private int size;
private IntNode sentinel; /* The first item (if it exists) is at sentinel.next */
/* sentinel should not be changed later (after init) */
public SLList_Sentinel() {