This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for Directed graph. class DirectedGraphNode { int label; ArrayList | |
* <DirectedGraphNode> neighbors; DirectedGraphNode(int x) { label = x; | |
* neighbors = new ArrayList<DirectedGraphNode>(); } }; | |
*/ | |
public class Solution { | |
private Set<DirectedGraphNode> visited = new HashSet<>(); | |
private Stack<DirectedGraphNode> stack = new Stack<>(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Random; | |
/** | |
* Definition for singly-linked list. public class ListNode { int val; ListNode | |
* next; ListNode(int x) { val = x; } } | |
*/ | |
class Solution { | |
private ListNode head; | |
private Random rand = new Random(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public boolean PredictTheWinner(int[] nums) { | |
if (nums == null || nums.length == 0) return false; | |
int len = nums.length; | |
int sum = 0; | |
int[][] T = new int[len][len]; | |
for (int i = 0; i < len; i++) { | |
T[i][i] = nums[i]; | |
sum += nums[i]; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
// [10,5,2,6] | |
// 100 | |
public int numSubarrayProductLessThanK(int[] nums, int k) { | |
int cnt = 0; // The result | |
int prod = 1; // current product | |
int start = 0; // Starting index of current subarray | |
for (int i = 0; i < nums.length; i++) { | |
if (nums[i] < k) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* public class Vote { | |
int time; | |
String name; | |
Vote(int time, String name) { | |
this.time = time; | |
this.name = name; | |
} | |
}*/ | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public class Solution { | |
/** | |
* @param n: | |
* a integer | |
* @return: return a 2D array | |
*/ | |
public List<List<Integer>> getFactors(int n) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MagicDictionary { | |
class TrieNode { | |
boolean isWord; | |
TrieNode[] next = new TrieNode[26]; | |
} | |
private TrieNode root; | |
/** Initialize your data structure here. */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MovingAverage { | |
private int k; | |
private long sum; | |
private Queue<Integer> queue = new LinkedList<>(); | |
/* | |
* @param size: An integer | |
*/public MovingAverage(int size) { | |
this.k = size; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int singleNonDuplicate(int[] nums) { | |
int l = 0, r = nums.length - 1; | |
while (l < r) { | |
// mid suppose to point to first element of a pair | |
int mid = (l + r) / 2; | |
if (mid % 2 == 1) mid--; | |
if (nums[mid] == nums[mid + 1]) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public int singleNonDuplicate(int[] nums) { | |
int l = 0, r = nums.length - 1; | |
while (l <= r) { | |
System.out.println("l: " + l + " r: " + r); | |
int mid = (l + r) / 2; | |
if (l == r || mid == l || mid == r || nums[mid] != nums[mid + 1] && nums[mid] != nums[mid - 1]) | |
return mid; | |
if (nums[mid] == nums[mid - 1]) { |