Skip to content

Instantly share code, notes, and snippets.

/**
* 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<>();
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();
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];
}
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) {
/* public class Vote {
int time;
String name;
Vote(int time, String name) {
this.time = time;
this.name = name;
}
}*/
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) {
class MagicDictionary {
class TrieNode {
boolean isWord;
TrieNode[] next = new TrieNode[26];
}
private TrieNode root;
/** Initialize your data structure here. */
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;
}
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]) {
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]) {