Skip to content

Instantly share code, notes, and snippets.

View wushbin's full-sized avatar

Shengbin Wu wushbin

  • San Francisco Bay Area
View GitHub Profile
class Solution {
public boolean canTransform(String start, String end) {
int pt1 = 0, pt2 = 0;
int len1 = start.length();
int len2 = end.length();
while(pt1 < len1 || pt2 < len2) {
// find not X
while(pt1 < len1 && start.charAt(pt1) == 'X') {
class Solution {
public int maxRotateFunction(int[] A) {
int sum = 0;
int F = 0;
int len = A.length;
for (int i = 0; i < len; i++) {
sum += A[i];
F += i * A[i];
}
class Solution {
Map<Integer, Integer> shuffle;
int n;
Random rand;
int n_rows;
int n_cols;
public Solution(int n_rows, int n_cols) {
this.n = n_rows * n_cols;
this.shuffle = new HashMap<>();
this.rand = new Random();
class Solution {
public int findNthDigit(int n) {
long base = 1;
int cnt = 1;// cnt -> number of digits
while(n > 9 * base * cnt) {
n -= (9 * base * cnt);
cnt += 1;
base *= 10;
}
class Solution {
public boolean checkSubarraySum(int[] nums, int k) {
Map<Integer, Integer> map = new HashMap<>();
int sum = 0;
map.put(0, -1);
for (int i = 0; i < nums.length; i++) {
sum += nums[i];
if (k != 0) {
sum %= k;
}
class Solution {
public int subarrayBitwiseORs(int[] A) {
Set<Integer> result = new HashSet<>();
Set<Integer> current = new HashSet<>();
for (int num : A) {
Set<Integer> temp = new HashSet<>();
temp.add(num);
for (int pre : current) {
temp.add(num | pre);
class Solution {
public int rangeBitwiseAnd(int m, int n) {
int i = 0;
while(m != n) {
m >>= 1;
n >>= 1;
i++;
}
return m << i;
}
class Solution {
public int[] addNegabinary(int[] arr1, int[] arr2) {
List<Integer> list = new ArrayList<>();
int pt1 = arr1.length - 1;
int pt2 = arr2.length - 1;
int carrier = 0;
while(pt1 >= 0 || pt2 >= 0) {
class MajorityChecker {
class SegmentTreeNode {
int major;
int freq;
int start;
int end;
SegmentTreeNode left;
SegmentTreeNode right;
public SegmentTreeNode (int start, int end) {
class Solution {
class TrieNode {
char c;
String word;
TrieNode[] children;
boolean isWord;
public TrieNode(char c) {
this.c = c;
this.children = new TrieNode[26];