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 { | |
int count = 0; | |
int N = 0; | |
public boolean canVisitAllRooms(List<List<Integer>> rooms) { | |
if (rooms == null || rooms.size() < 1) return true; | |
int n = rooms.size(); | |
count = n; | |
N = n; | |
boolean[] visited = new boolean[n]; | |
Set<Integer> canReach = new HashSet<>(); |
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 List<Integer> splitIntoFibonacci(String S) { | |
List<Integer> res = new ArrayList<>(); | |
int len = S.length(); | |
for (int i = 1; i < len && i < 11; i++) { | |
if (S.charAt(0) == '0' && i > 1) return res; | |
for (int j = 1; j < len - i && j < 11; j++) { | |
if (S.charAt(i) == '0' && j > 1) continue; | |
long a = Long.valueOf(S.substring(0, i)); | |
long b = Long.valueOf(S.substring(i, i + j)); |
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 numMagicSquaresInside(int[][] grid) { | |
if (grid == null || grid.length < 3 || grid[0].length < 3) return 0; | |
int m = grid.length, n = grid[0].length; | |
boolean[] visited = new boolean[9]; | |
int[] rows = new int[3]; | |
int[] cols = new int[3]; | |
int[] diags = new int[2]; | |
boolean flag = false; |
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
/** | |
* // This is the Master's API interface. | |
* // You should not implement it, or speculate about its implementation | |
* interface Master { | |
* public int guess(String word) {} | |
* } | |
*/ | |
class Solution { | |
private int match(String guess, String w) { | |
int match = 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 Solution { | |
public int[][] flipAndInvertImage(int[][] A) { | |
if (A == null || A.length < 1 || A[0].length < 1) return A; | |
int m = A.length, n = A[0].length; | |
for (int i = 0; i < m; i++) { | |
for (int j = 0; j < n / 2; j++) { | |
int temp = A[i][j]; | |
A[i][j] = A[i][n - 1 - j] ^ 1; | |
A[i][n - 1 - j] = temp ^ 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 countSubstrings(String s) { | |
if (s == null || s.length() < 0) return 0; | |
int count = 0, len = s.length(); | |
boolean[][] isPalindrome = new boolean[len][len]; | |
for (int i = len - 1; i >= 0; i--) { | |
for (int j = i; j < len; j++) { | |
if (s.charAt(i) == s.charAt(j) && (j - i < 2 || isPalindrome[i + 1][j - 1])) { | |
isPalindrome[i][j] = true; | |
count++; |
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 List<List<Integer>> largeGroupPositions(String S) { | |
List<List<Integer>> res = new ArrayList<>(); | |
int len = S.length(); | |
if (len < 3) return res; | |
int start = 0, end = 1; | |
while (end < len) { | |
while (end < len && S.charAt(end) == S.charAt(end - 1)) { | |
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 { | |
public String maskPII(String S) { | |
StringBuilder res = new StringBuilder(); | |
int idx = S.indexOf('@'); | |
int len = S.length(); | |
if (idx > 0) { | |
res.append(Character.toLowerCase(S.charAt(0))); | |
res.append("*****"); | |
res.append(S.substring(idx - 1).toLowerCase()); | |
} else { |
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 uniqueLetterString(String S) { | |
char[] s = S.toCharArray(); | |
int n = s.length; | |
int mod = 1000000007; | |
long ret = 0; | |
for(int i = 0;i < s.length;i++){ | |
int l = i-1; | |
for(;l >= 0 && s[l] != s[i];l--); | |
int r = i+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
public class Solution { | |
public List<String> wordBreak(String s, List<String> wordDict) { | |
List<String> res = new ArrayList<String>(); | |
if (s == null || s.length() == 0) { | |
return res; | |
} | |
int max = 0; | |
for (String str : wordDict) { | |
max = Math.max(str.length(), max); | |
} |
NewerOlder