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 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') { |
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 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]; | |
} | |
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 { | |
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(); |
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 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; | |
} |
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 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; | |
} |
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 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); |
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 rangeBitwiseAnd(int m, int n) { | |
int i = 0; | |
while(m != n) { | |
m >>= 1; | |
n >>= 1; | |
i++; | |
} | |
return m << 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 { | |
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) { |
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 MajorityChecker { | |
class SegmentTreeNode { | |
int major; | |
int freq; | |
int start; | |
int end; | |
SegmentTreeNode left; | |
SegmentTreeNode right; | |
public SegmentTreeNode (int start, int end) { |
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 { | |
class TrieNode { | |
char c; | |
String word; | |
TrieNode[] children; | |
boolean isWord; | |
public TrieNode(char c) { | |
this.c = c; | |
this.children = new TrieNode[26]; |