This commit is contained in:
kkunkka
2025-03-18 13:57:41 +08:00
parent 51e6fff72c
commit 70116de2d6
7 changed files with 166 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
package com.dota.design._382;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class ListNode {
int val;
ListNode next;
ListNode() {}
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
List<Integer> list ;
Random r;
public Solution(ListNode head) {
r = new Random();
list = new ArrayList<Integer>();
while (head != null) {
list.add(head.val);
head = head.next;
}
}
public int getRandom() {
return list.get(r.nextInt(list.size()));
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(head);
* int param_1 = obj.getRandom();
*/

View File

@@ -0,0 +1,22 @@
package com.dota.design._398;
import java.util.*;
class Solution {
Map<Integer, List<Integer>> map = new HashMap<>();
Random r;
public Solution(int[] nums) {
r = new Random();
for (int i = 0; i < nums.length; i++) {
map.computeIfAbsent(nums[i], k -> new ArrayList<>());
map.get(nums[i]).add(i);
}
}
public int pick(int target) {
List<Integer> list = map.get(target);
return list.get(r.nextInt(list.size()));
}
}

View File

@@ -0,0 +1,40 @@
package com.dota.design._497;
import java.util.Random;
class Solution {
Random rand = new Random();
int[][] rects;
int[] dp;
public Solution(int[][] rects) {
this.rects = rects;
dp = new int[rects.length];
dp[0] = (1 + rects[0][2] - rects[0][0]) * (1 + rects[0][3] - rects[0][1]);
for (int i = 1; i < rects.length; i++) {
dp[i] = dp[i - 1] + ((rects[i][2] - rects[i][0] + 1) * (rects[i][3] - rects[i][1] + 1));
}
}
public int[] pick() {
var ranInt = rand.nextInt(dp[dp.length - 1]) + 1;
int idx = 0;
int left = 0, right = rects.length - 1;
while(left < right) {
int mid = left + (right - left) / 2;
if (dp[mid] >= ranInt) {
right = mid;
} else left = mid+1;
}
idx = right;
int x = rand.nextInt(rects[idx][2] - rects[idx][0] + 1);
int y = rand.nextInt(rects[idx][3] - rects[idx][1] + 1);
return new int[]{x + rects[idx][0], y + rects[idx][1]};
}
}
/**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(rects);
* int[] param_1 = obj.pick();
*/

View File

@@ -0,0 +1,13 @@
package com.dota.greedy._3227;
class Solution {
public boolean doesAliceWin(String s) {
int c = 0;
for (int i = 0; i < s.length(); i++) {
if ("aeiou".indexOf(s.charAt(i)) != -1) {
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,31 @@
package com.dota.math._2614;
class Solution {
public int diagonalPrime(int[][] nums) {
int max = 0;
int n = nums.length;
for (int i = 0; i < nums.length; i++) {
if (isSu(nums[i][n-1-i])) {
max = Math.max(max, nums[i][n-1-i]);
}
if (isSu(nums[i][i])) {
max = Math.max(max, nums[i][i]);
}
}
return max;
}
boolean isSu(int num) {
if (num == 1) {
return false;
}
for (int i=2;i*i<=num;i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}

View File

@@ -0,0 +1,12 @@
package com.dota.str._3110;
class Solution {
public int scoreOfString(String s) {
int n = s.length();
int res = 0;
for (int i = 1; i < n; i++) {
res += Math.abs(s.charAt(i) - s.charAt(i - 1));
}
return res;
}
}

View File

@@ -0,0 +1,13 @@
package com.dota.str._3340;
class Solution {
public boolean isBalanced(String num) {
int res = 0;
boolean flag = true;
for (char c : num.toCharArray()) {
res = flag ? res + (c - '0') : res - (c - '0');
flag = !flag;
}
return res == 0;
}
}