class Solution { public int[] sortArrayByParity(int[] A) { int[] result = new int[A.length]; int first = 0; int last = A.length - 1; for (int j : A) { // 如果是偶数,放在数据最前面 if (j % 2 == 0) { result[first] = j; first++; // 奇数放于数组最后面 } else { result[last] = j; last--; } } return result; }}
class Solution { public int smallestRangeI(int[] A, int K) { // 特殊情况排除 if (A.length <= 1){ return 0; } Arrays.sort(A); return Math.max((A[A.length - 1] - K) - (A[0] + K), 0); }}
class Solution { public boolean hasGroupsSizeX(int[] deck) { int[] count = new int[10000]; for (int c: deck) { count[c]++; } int g = -1; for (int i = 0; i < 10000; ++i) { if (count[i] > 0) { if (g == -1) { g = count[i]; } else { g = gcd(g, count[i]); } } } return g >= 2; } public int gcd(int x, int y) { return x == 0 ? y : gcd(y % x, x); }}
class Solution { public String reverseOnlyLetters(String S) { Stack<Character> letters = new Stack(); for (char c: S.toCharArray()) if (Character.isLetter(c)) letters.push(c); StringBuilder ans = new StringBuilder(); for (char c: S.toCharArray()) { if (Character.isLetter(c)) ans.append(letters.pop()); else ans.append(c); } return ans.toString(); }}
class Solution { public int[] sortArrayByParityII(int[] nums) { int[] result = new int[nums.length]; int j = 0; int k = 1; for (int num : nums) { if (isOrNot(num)) { result[j] = num; j += 2; } else { result[k] = num; k += 2; } } return result; } public static boolean isOrNot(int number){ return number % 2 == 0; }}
class Solution { public boolean isLongPressedName(String name, String typed) { int i = 0, j = 0; while (j < typed.length()) { if (i < name.length() && name.charAt(i) == typed.charAt(j)) { i++; j++; } else if (j > 0 && typed.charAt(j) == typed.charAt(j - 1)) { j++; } else { return false; } } return i == name.length(); }}
class Solution { public int numUniqueEmails(String[] emails) { Set<String> set = new HashSet<>(); for (String email : emails) { StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < email.length(); i++) { if (email.charAt(i) == '.'){ continue; }else if (email.charAt(i) == '+'){ do { i++; }while (email.charAt(i) != '@'); } if (email.charAt(i) == '@'){ stringBuilder.append(email.substring(i)); break; } stringBuilder.append(email.charAt(i)); } set.add(stringBuilder.toString()); } return set.size(); }}
class Solution {
public String[] reorderLogFiles(String[] logs) {
Arrays.sort(logs, (log1, log2) -> {
// 将数组中的元素分成两部分(标识符和内容)
String[] split1 = log1.split("\\s+", 2);
String[] split2 = log2.split("\\s+", 2);
// 判断内容部分的第一个,如果是数字就是数字日志,如果是字母就是字母日志
boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
if (!isDigit1 && !isDigit2) {
// 如果两个都不是数字日志判断字典顺序
int cmp = split1[1].compareTo(split2[1]);
// 如果字典顺序不相同,直接返回
if (cmp != 0) return cmp;
// 字典顺序相同就比较标识符
return split1[0].compareTo(split2[0]);
}
// 返回(字母在前 —— 1,数字在后 —— -1)
return isDigit1 ? (isDigit2 ? 0 : 1) : -1;
});
return logs;
/*
Arrays.sort(logs, new Comparator<String>() {
@Override
public int compare(String log1, String log2) {
String[] split1 = log1.split("\\s+", 2);
String[] split2 = log2.split("\\s+", 2);
boolean isDigit1 = Character.isDigit(split1[1].charAt(0));
boolean isDigit2 = Character.isDigit(split2[1].charAt(0));
if (!isDigit1 && !isDigit2) {
int cmp = split1[1].compareTo(split2[1]);
if (cmp != 0) return cmp;
return split1[0].compareTo(split2[0]);
}
return isDigit1 ? (isDigit2 ? 0 : 1) : -1;
}
});
*/
return logs;
}
}
class Solution {
public boolean validMountainArray(int[] arr) {
int maxIndex = findMaxIndex(arr);
if (arr.length < 3 || maxIndex == 0 || maxIndex == arr.length - 1){
return false;
}
boolean flag = true;
for (int i = 0; i < arr.length - 1; i++) {
if (i < maxIndex){
if (arr[i] >= arr[i + 1]) {
flag = false;
}
}else {
if (arr[i] <= arr[i + 1]) {
flag = false;
}
}
}
return flag;
}
public static int findMaxIndex(int[] arr){
int max = Integer.MIN_VALUE;
int index = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] > max){
max = arr[i];
index = i;
}
}
return index;
}
}
class Solution {
public int[] diStringMatch(String s) {
int N = s.length();
int[] arr = new int[N + 1];
for (int i = 0; i < N + 1; i++) {
arr[i] = i;
}
int[] result = new int[N + 1];
int first = 0;
int index = N;
for (int i = 0; i < N; i++) {
if (s.charAt(i) == 'I'){
result[i] = arr[first++];
}else {
result[i] = arr[index--];
}
}
result[N] = arr[index];
return result;
}
}
class Solution {
public int minDeletionSize(String[] strs) {
int length = strs[0].length();
int result = 0;
for (int i = 0; i < length; i++) {
for (int j = 0; j < strs.length - 1; j++) {
if (strs[j].charAt(i) > strs[j + 1].charAt(i)){
result++;
break;
}
}
}
return result;
}
}
class Solution {
public boolean isAlienSorted(String[] words, String order) {
int[] index = new int[26];
for (int i = 0; i < order.length(); ++i)
index[order.charAt(i) - 'a'] = i;
search: for (int i = 0; i < words.length - 1; ++i) {
String word1 = words[i];
String word2 = words[i+1];
// Find the first difference word1[k] != word2[k].
for (int k = 0; k < Math.min(word1.length(), word2.length()); ++k) {
if (word1.charAt(k) != word2.charAt(k)) {
// If they compare badly, it's not sorted.
if (index[word1.charAt(k) - 'a'] > index[word2.charAt(k) - 'a'])
return false;
continue search;
}
}
// If we didn't find a first difference, the
// words are like ("app", "apple").
if (word1.length() > word2.length())
return false;
}
return true;
}
}
class Solution {
public int repeatedNTimes(int[] A) {
int len = A.length;
int repeat = len / 2;
for (int i : A) {
if (count(A,i) == repeat) {
return i;
}
}
return 0;
}
public static int count(int[] A,int number){
int result = 0;
for (int i : A) {
if (i == number){
result++;
}
}
return result;
}
}
class Solution {
public int largestPerimeter(int[] A) {
Arrays.sort(A);
for (int i = A.length - 1; i >= 2; --i) {
if (A[i - 2] + A[i - 1] > A[i]) {
return A[i - 2] + A[i - 1] + A[i];
}
}
return 0;
}
}
class Solution {
public int[] sortedSquares(int[] nums) {
int[] result = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
result[i] = nums[i] * nums[i];
}
Arrays.sort(result);
return result;
}
}
class Solution {
// 先求和,如果操作的位置为偶数则和做同样操作,否则直接跳过
public int[] sumEvenAfterQueries(int[] A, int[][] queries) {
int S = 0;
for (int x: A)
if (x % 2 == 0)
S += x;
int[] ans = new int[queries.length];
for (int i = 0; i < queries.length; ++i) {
int val = queries[i][0], index = queries[i][1];
if (A[index] % 2 == 0) S -= A[index];
A[index] += val;
if (A[index] % 2 == 0) S += A[index];
ans[i] = S;
}
return ans;
}
}
class Solution {
public List<Integer> addToArrayForm(int[] num, int k) {
List<Integer> res = new ArrayList<Integer>();
int n = num.length;
for (int i = n - 1; i >= 0; --i) {
int sum = num[i] + k % 10;
k /= 10;
if (sum >= 10) {
k++;
sum -= 10;
}
res.add(sum);
}
for (; k > 0; k /= 10) {
res.add(k % 10);
}
Collections.reverse(res);
return res;
}
}
class Solution {
public int numRookCaptures(char[][] board) {
int cnt = 0, st = 0, ed = 0;
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
// 先找出R 车所在的位置
for (int i = 0; i < 8; ++i) {
for (int j = 0; j < 8; ++j) {
if (board[i][j] == 'R') {
st = i;
ed = j;
break;
}
}
}
// 只有四个方位,分别往四个方位走
for (int i = 0; i < 4; ++i) {
for (int step = 0;; ++step) {
int tx = st + step * dx[i];
int ty = ed + step * dy[i];
if (tx < 0 || tx >= 8 || ty < 0 || ty >= 8 || board[tx][ty] == 'B') {
break;
}
if (board[tx][ty] == 'p') {
cnt++;
break;
}
}
}
return cnt;
}
}