z准备时把全网的例题都找了,并做了记录和代码,供分享,求加米。需要原稿课私信。- /*
public static int alphanumbericCombination(String s) {
int n = s.length();
int[] dp = new int[s.length() + 1];
dp[1] = 1;
if (s.length() == 1) return 1;
int twoDigits = Integer.parseInt(s.substring(0, 2));
if (twoDigits >= 10 && twoDigits <= 26) {
dp[2] = 2;
} else {
dp[2] = 1;
}
for (int i = 3; i < n + 1 ; i++) {
int oneDigit = Integer.parseInt(s.substring(i - 1, i));
if ( oneDigit <= 9 && oneDigit >= 1) {
dp += dp[i - 1];
}
twoDigits = Integer.[i]parseInt(s.substring(i - 2, i));
if ( twoDigits <= 26 && twoDigits >= 10) {
dp += dp[i - 2];
}
}
return dp[n];
} - [i]/**
A compliance system monitors incoming and outbound calls.
It sends an alert whenever the average number of calls over a trailing number of minutes exceeds a threshold. If the number
of trailing minutes to consider, precedingMinutes = 5, at time T, average the call volumes for times T-(5-1), T-(5-2) ...T-(5-5).
Example
n = 8
numCalls = [2, 2, 2, 2, 5, 5, 5, 8]
alertThreshold = 4
precedingMinutes = 3
No alerts are sent until at least T = 3 because there are not enough values to consider. When T = 3, the average calls = (2 + 2 + 2)/3 = 2.
Average calls from T = 4 to T = 8 are 2, 3, 4, 5, and 6. A total of two alerts are sent during the last two periods.
*/
public static int calculateAlert(int[] numCalls, int alertThreshold, int precedingTime) {
int count = 0;
int sum = 0;
for (int i = 0; i < precedingTime && i < numCalls.length; i++) {
sum += numCalls;
double average = (double) sum / precedingTime;
if (average > alertThreshold) {
count++;
}
}
for (int i = precedingTime; i < numCalls.length; i++) {
sum -= numCalls[i - precedingTime] ;
sum += numCalls[i];
System.[i]out.println(" sum: " + sum);
double average = (double) sum / precedingTime;
System.out.println("avarage: " + average);
if (average > alertThreshold) {
count++;
}
}
return count;
} - /**
* calculate chair base on the ordersst<List<String>> list) {
String[][] res = new String[list.size()][];
for (int i = 0;i < list.size(); i++) {
res = new String[list.get(i).size()];
for (int j = 0; j < res[i].length; j++){
res[i][j] = list.get(i).get(j);
}
}
return res;
}
[i]/**
* calculate number of unique pairs in the given nums with a difference of k, k can be 0;
*/
public static int countPair(int[] numbers, int k) {
Set<Integer> set = new HashSet<>();
for (int num : numbers) {
set.add(num);
}
Set<String> uniqueSet = new HashSet<>();
for (int num : numbers) {
if (set.contains(num + k)) {
uniqueSet.add(num + ", " + num + k);
}
}
return uniqueSet.size();
}
/**
*li kou: liu si qi: Palindromic Substrings
*/
public int palindromeCount(String s) {
int n = s.length();
int ans = 0;
for (int i = 0; i < n; i++) {
int even = count(s, i, i + 1);
int odd = count(s, i, i);
ans += even + odd;
}
return ans;
}
public int count(String s, int left, int right){
int count = 0;
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
left--;
right++;
count++;
}
return count;
}
|