class KthLargest { // 排序队列PQ 解决TOPK问题的最好方法 PriorityQueue<Integer> pq; int k; public KthLargest(int k, int[] nums) { this.k = k; pq = new PriorityQueue<Integer>(); for (int x : nums) { add(x); } } public int add(int val) { pq.offer(val); if (pq.size() > k) { pq.poll(); } return pq.peek(); }}/** * Your KthLargest object will be instantiated and called as such: * KthLargest obj = new KthLargest(k, nums); * int param_1 = obj.add(val); */
class Solution { public int search(int[] nums, int target) { int left = 0; int right = nums.length - 1; int pivot; while (left <= right){ pivot = (left + right) / 2; if (nums[pivot] == target) return pivot; if (target < nums[pivot]) right = pivot - 1; else left = pivot + 1; } return -1; }}
class Solution {
public String toLowerCase(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
if (64 < str.charAt(i) && str.charAt(i) < 90){
result.append((char)(str.charAt(i) + 32));
}else {
result.append(str.charAt(i));
}
}
return result.toString();
}
}
class Solution {
public boolean isOneBitCharacter(int[] bits) {
int i = 0;
// 遍历到的值为1则跳过一位
while (i < bits.length - 1) {
i += bits[i] + 1;
}
return i == bits.length - 1;
}
}
class Solution {
public String longestWord(String[] words) {
String result = "";
Set<String> sets = new HashSet<>(Arrays.asList(words));
for (String word : words) {
int worldLength = word.length();
int resultLength = result.length();
if (worldLength > resultLength || worldLength == resultLength && word.compareTo(result) < 0){
boolean flag = true;
for (int i = 1; i < word.length(); i++) {
if (!sets.contains(word.substring(0,i))){
flag = false;
break;
}
}
if (flag){
result = word;
}
}
}
return result;
}
}
class Solution {
public int pivotIndex(int[] nums) {
// 用i来标记中心下标的位置
for (int i = 0; i < nums.length; i++) {
int left = i,right = i;
int sumLeft = 0;
int sumRight = 0;
while (left > 0){
left --;
sumLeft += nums[left];
}
while (right < nums.length - 1){
right ++;
sumRight += nums[right];
}
if (sumLeft == sumRight){
return i;
}
}
return -1;
}
}
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> result = new ArrayList<>();
// 从left到right逐一判断是否为自除数
while (left <= right){
if (isSelfDividingNumbers(left)) result.add(left);
left++;
}
return result;
}
// 用于判断是否为自除数
public boolean isSelfDividingNumbers(int number){
// 将数字转换为字符串
String s = String.valueOf(number);
// 如果字符串中包含0则直接不是自除数
if (s.contains("0")) return false;
List<Integer> save = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
// 取出字符串中的每一位,转为整型放入到sava队列中
save.add(Integer.parseInt(s.substring(i,i + 1)));
}
boolean flag = true;
// 逐位判断是否能够整除
for (int i = 0; i < s.length(); i++) {
if (number % save.get(i) != 0) {
flag = false;
break;
}
}
return flag;
}
}
// DFS
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
helper(image, sr, sc, newColor, image[sr][sc]);
return image;
}
void helper(int[][] image, int sr, int sc, int newColor, int oldColor) {
if (sr < 0 || sc < 0 || sr >= image.length || sc >= image[0].length
|| image[sr][sc] != oldColor || newColor == oldColor){
return;
}
image[sr][sc] = newColor;
helper(image, sr - 1, sc, newColor, oldColor);
helper(image, sr + 1, sc, newColor, oldColor);
helper(image, sr, sc - 1, newColor, oldColor);
helper(image, sr, sc + 1, newColor, oldColor);
}
}
// BFS
class Solution {
public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
int[] dx = {1, 0, 0, -1};
int[] dy = {0, 1, -1, 0};
// 定义初始位置的颜色
int currColor = image[sr][sc];
// 特殊条件
if (currColor == newColor) {
return image;
}
int n = image.length, m = image[0].length;
Queue<int[]> queue = new LinkedList<>();
queue.offer(new int[]{sr, sc});
image[sr][sc] = newColor;
while (!queue.isEmpty()) {
int[] cell = queue.poll();
int x = cell[0], y = cell[1];
for (int i = 0; i < 4; i++) {
int mx = x + dx[i], my = y + dy[i];
if (mx >= 0 && mx < n && my >= 0 && my < m && image[mx][my] == currColor) {
queue.offer(new int[]{mx, my});
image[mx][my] = newColor;
}
}
}
return image;
}
}
class Solution {
public int minCostClimbingStairs(int[] cost) {
int size = cost.length;
int[] minCost = new int[size];
minCost[0] = 0;
minCost[1] = Math.min(cost[0], cost[1]);
for (int i = 2; i < size; i++) {
minCost[i] = Math.min(minCost[i - 1] + cost[i], minCost[i - 2] + cost[i - 1]);
}
return minCost[size - 1];
}
}
class Solution {
public int dominantIndex(int[] nums) {
int maxIsDoubleOrNot = findMaxIsDoubleOrNot(nums);
if (maxIsDoubleOrNot == -1){
return -1;
}
for (int i = 0; i < nums.length; i++) {
if (nums[i] == maxIsDoubleOrNot){
return i;
}
}
return -1;
}
public int findMaxIsDoubleOrNot(int[] nums){
if (nums.length == 1){
return nums[0];
}
int[] copy = Arrays.copyOf(nums, nums.length);
Arrays.sort(copy);
if (copy[copy.length - 1] >= (copy[copy.length - 2] * 2)){
return copy[copy.length - 1];
}
return -1;
}
}
class Solution {
public String shortestCompletingWord(String licensePlate, String[] words) {
String[] copies = Arrays.copyOf(words,words.length);
String lowerCase = licensePlate.toLowerCase();
List<String> result = new ArrayList<>();
for (int i = 0;i < copies.length;i++) {
int count = 0;
int cut = 0;
for (int j = 0; j < lowerCase.length(); j++) {
if (!('a' <= lowerCase.charAt(j) && lowerCase.charAt(j) <= 'z')) {
cut++;
continue;
}
String substring = lowerCase.substring(j, j + 1);
if (copies[i].contains(substring)) {
copies[i] = copies[i].replaceFirst(substring, "!");
count++;
}
}
if (count == lowerCase.length()-cut){
result.add(words[i]);
}
}
int index = 0;
int min = Integer.MAX_VALUE;
for (int i = 0; i < result.size(); i++) {
if (result.get(i).length() < min){
min = result.get(i).length();
index = i;
}
}
return result.get(index);
}
}
class Solution {
public int countPrimeSetBits(int L, int R) {
int result = 0;
while (L <= R){
int count = 0;
String s = Integer.toBinaryString(L);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '1'){
count++;
}
}
L++;
if (isZhiShu(count)){
result++;
}
}
return result;
}
public boolean isZhiShu(int number){
if (number == 1){
return false;
}
for (int i = 2; i <= Math.sqrt(number); i++) {
if (number % i == 0){
return false;
}
}
return true;
}
}
class Solution {
public int numJewelsInStones(String jewels, String stones) {
int result = 0;
for (int i = 0; i < stones.length(); i++) {
if (jewels.contains(stones.substring(i,i + 1))){
result ++;
}
}
return result;
}
}
class Solution {
public int rotatedDigits(int N) {
// Count how many n in [1, N] are good.
int ans = 0;
for (int n = 1; n <= N; ++n)
if (good(n, false)) ans++;
return ans;
}
// Return true if n is good.
// The flag is true iff we have an occurrence of 2, 5, 6, 9.
public boolean good(int n, boolean flag) {
if (n == 0) return flag;
int d = n % 10;
if (d == 3 || d == 4 || d == 7) return false;
if (d == 0 || d == 1 || d == 8) return good(n / 10, flag);
return good(n / 10, true);
}
}
class Solution {
public boolean rotateString(String A, String B) {
if (A.equals(B)){
return true;
}
if (A.equals("")){
return false;
}
StringBuilder stringBuilder = new StringBuilder();
int controller = 0;
stringBuilder.append(A);
while (controller <= A.length()){
if (stringBuilder.toString().equals(B)) return true;
String substring = stringBuilder.substring(0, 1);
stringBuilder.delete(0,1);
stringBuilder.append(substring);
controller++;
}
return false;
}
}