class Solution { public int uniqueMorseRepresentations(String[] words) { String[] morse = new String[]{ ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-", ".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-", ".--","-..-","-.--","--.." }; Set<String> set = new HashSet<>(); Map<Character,Integer> map = new HashMap<>(); for (int i = 0; i < 26; i++) { map.put((char)(i +97),i); } for (String word : words) { StringBuilder getMorse = new StringBuilder(); for (int i = 0; i < word.length(); i++) { getMorse.append(morse[map.get(word.charAt(i))]); } set.add(getMorse.toString()); } return set.size(); }}
class Solution { public int[] numberOfLines(int[] widths, String s) { Map<Character,Integer> map = new HashMap<>(); for (int i = 0; i < widths.length; i++) { map.put((char)(i + 97) ,widths[i]); } int[] result = new int[2]; int line = 1; int count = 0; for (int i = 0; i < s.length(); i++) { count += map.get(s.charAt(i)); if (count > 100){ count = 0; count += map.get(s.charAt(i)); line++; } } result[0] = line; result[1] = count; return result; }}
class Solution { public List<String> subdomainVisits(String[] cpdomains) { Map<String, Integer> counts = new HashMap(); for (String domain: cpdomains) { // 按空格进行split String[] cpinfo = domain.split("\\s+"); // 按空格split之后的后半部分按.split String[] frags = cpinfo[1].split("\\."); int count = Integer.parseInt(cpinfo[0]); String cur = ""; for (int i = frags.length - 1; i >= 0; --i) { cur = frags[i] + (i < frags.length - 1 ? "." : "") + cur; counts.put(cur, counts.getOrDefault(cur, 0) + count); } } List<String> ans = new ArrayList<>(); for (String dom: counts.keySet()) ans.add("" + counts.get(dom) + " " + dom); return ans; }}
class Solution { public double largestTriangleArea(int[][] points) { double S = 0; double x1 , x2 , x3, y1 , y2 , y3; for(int i = 0;i < points.length;i++){ x1 = points[i][0]; y1 = points[i][1]; for(int j = i+1;j < points.length;j++){ x2 = points[j][0]; y2 = points[j][1]; for(int k = j+1;k < points.length;k++){ x3 = points[k][0]; y3 = points[k][1]; //坐标公式 S = Math.max(S , Math.abs(x1*y2+x2*y3+x3*y1-x2*y1-x3*y2-x1*y3)/2.0); } } } return S; }}
class Solution { public String mostCommonWord(String paragraph, String[] banned) { paragraph = paragraph.toLowerCase(); for (String s : banned) { paragraph = paragraph.replaceAll(s,"1"); } // 匹配所有标点符号 paragraph = paragraph.replaceAll("[^0-9A-z\\u4e00-\\u9fa5\\s]", " "); String[] split = paragraph.split("\\s+"); int max = 0; String result = ""; for (int i = 0; i < split.length; i++) { if (split[i].contains("1")){ continue; } int count = 1; for (int j = i + 1; j < split.length; j++) { if (split[i].contains("1")){ continue; } if (split[j].equals(split[i])){ count ++; } } if (max < count){ max = count; result = split[i]; } } return result; }}
class Solution { public int[] shortestToChar(String s, char c) { int[] result = new int[s.length()]; Arrays.fill(result,Integer.MAX_VALUE); List<Integer> index = findIndex(s, c); for (int i = 0; i < s.length(); i++) { for (Integer integer : index) { result[i] = Math.abs(Math.min(integer - i,result[i])); } } return result; } public List<Integer> findIndex(String s,char c){ List<Integer> list = new ArrayList<>(); for (int i = 0; i < s.length(); i++) { if (s.charAt(i) == c){ list.add(i); } } return list; }}
class Solution { public String toGoatLatin(String S) { // 特殊条件排除 if (S.length() == 0){ return ""; } // 元音字母字符串,用作匹配 String vowel = "aeiouAEIOU"; // 按空格拆分成数组 String[] words = S.split("\\s+"); StringBuilder result = new StringBuilder(); for (int i = 0; i < words.length; i++) { // 元音字母开头直接添加原字符再拼接ma if (vowel.contains(words[i].substring(0,1))){ result.append(words[i]); }else { // 辅音字母开头换头至尾再添加ma result.append(words[i].substring(1)); result.append(words[i].charAt(0)); } result.append("ma"); int k = i; // 根据字母顺序添加a do { result.append("a"); k --; }while (k >= 0); // 每拼接完一个单词添加一个空格 if (i != words.length - 1){ result.append(" "); } } return result.toString(); }}
class Solution {
public int[][] flipAndInvertImage(int[][] image) {
for (int i = 0; i < image.length; i++) {
for (int j = 0; j <= image[0].length / 2; j++) {
int k = image[0].length - 1;
while (k >= image[0].length / 2) {
int temp = image[i][j];
image[i][j] = image[i][k];
image[i][k] = temp;
k--;
j++;
}
}
}
for (int i = 0; i < image.length; i++) {
for (int j = 0; j < image[0].length; j++) {
if (image[i][j] == 1){
image[i][j] = 0;
}else {
image[i][j] = 1;
}
}
}
return image;
}
}
class Solution {
public boolean isRectangleOverlap(int[] rec1, int[] rec2) {
if (rec1[0] == rec1[2] || rec1[1] == rec1[3] || rec2[0] == rec2[2] || rec2[1] == rec2[3]) {
return false;
}
return (rec1[2] > rec2[0] && // left
rec1[3] > rec2[1] && // bottom
rec1[0] < rec2[2] && // right
rec1[1] < rec2[3]); // top
}
}
class Solution {
public boolean backspaceCompare(String S, String T) {
StringBuilder s = new StringBuilder();
StringBuilder t = new StringBuilder();
for (int i = 0; i < S.length(); i++) {
if (S.charAt(i) != '#'){
s.append(S.charAt(i));
}else {
if (s.length() != 0) {
s.delete(s.length() - 1,s.length());
}
}
}
for (int i = 0; i < T.length(); i++) {
if (T.charAt(i) != '#'){
t.append(T.charAt(i));
}else {
if (t.length() != 0) {
t.delete(t.length() - 1,t.length());
}
}
}
return s.compareTo(t) == 0;
}
}
class Solution {
public int peakIndexInMountainArray(int[] arr) {
// 初步理解为找山峰位置(最大值)
int max = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] > arr[max]){
max = i;
}
}
return max;
}
}
class Solution {
public boolean buddyStrings(String a, String b) {
if (a.length() == 0 || b.length() == 0){
return false;
}
char[] chars = a.toCharArray();
char[] chars1 = b.toCharArray();
List<Integer> difference = new ArrayList<>();
for (int i = 0; i < chars.length; i++) {
if (chars[i] != chars1[i]){
difference.add(i);
}
}
// 如果没有不同 则判断A中是否有两个或两个以上相同元素
if (difference.size() == 0){
for (int i = 0; i < chars.length; i++) {
int count = 1;
for (int j = i + 1; j < chars.length; j++) {
if (chars[i] == chars[j]){
count++;
}
}
if (count >= 2){
return true;
}
}
return false;
}
// 如果有两个以上的不同,则对数组进行交换,判断交换后的数组是否相同
if (difference.size() == 2){
char temp = chars[difference.get(0)];
chars[difference.get(0)] = chars[difference.get(1)];
chars[difference.get(1)] = temp;
}
return Arrays.equals(chars, chars1);
}
}
class Solution {
public boolean lemonadeChange(int[] bills) {
int five = 0, ten = 0;
for (int bill : bills) {
if (bill == 5) {
five ++;
} else if (bill == 10) {
if (five == 0) {
return false;
}
five --;
ten ++;
} else {
if (five > 0 && ten > 0) {
five --;
ten --;
} else if (five >= 3) {
five -= 3;
} else {
return false;
}
}
}
return true;
}
}
class Solution {
public int[][] transpose(int[][] matrix) {
int[][] transpose = new int[matrix[0].length][matrix.length];
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
transpose[j][i] = matrix[i][j];
}
}
return transpose;
}
}
class Solution {
public int binaryGap(int n) {
// 将n转换为二进制字符串
String binaryString = Integer.toBinaryString(n);
int result = 0;
List<Integer> list = new ArrayList<>();
// 将所有的1下标的数放入list中
for (int i = 0; i < binaryString.length(); i++) {
if (binaryString.charAt(i) == '1'){
list.add(i);
}
}
// 如果list中只有0个或1个数,则说明没有相邻的1,返回0
if (list.size() == 0 || list.size() == 1){
return 0;
}
// 如果有相邻的1,距离最近的两个1一定是互相邻的1,找最大值
for (int i = 0; i < list.size() - 1; i++) {
result = Math.max(result,list.get(i + 1) - list.get(i));
}
return result;
}
}
class Solution {
public int robotSim(int[] commands, int[][] obstacles) {
int[] dx = new int[]{0, 1, 0, -1};
int[] dy = new int[]{1, 0, -1, 0};
int x = 0, y = 0, di = 0;
// Encode obstacles (x, y) as (x+30000) * (2^16) + (y+30000)
Set<Long> obstacleSet = new HashSet();
for (int[] obstacle: obstacles) {
long ox = (long) obstacle[0] + 30000;
long oy = (long) obstacle[1] + 30000;
obstacleSet.add((ox << 16) + oy);
}
int ans = 0;
for (int cmd: commands) {
if (cmd == -2) //left
di = (di + 3) % 4;
else if (cmd == -1) //right
di = (di + 1) % 4;
else {
for (int k = 0; k < cmd; ++k) {
int nx = x + dx[di];
int ny = y + dy[di];
long code = (((long) nx + 30000) << 16) + ((long) ny + 30000);
if (!obstacleSet.contains(code)) {
x = nx;
y = ny;
ans = Math.max(ans, x*x + y*y);
}
}
}
}
return ans;
}
}
class Solution {
public int projectionArea(int[][] grid) {
int N = grid.length;
int ans = 0;
for (int i = 0; i < N; ++i) {
int bestRow = 0; // largest of grid[i][j]
int bestCol = 0; // largest of grid[j][i]
for (int j = 0; j < N; ++j) {
if (grid[i][j] > 0) ans++; // top shadow
bestRow = Math.max(bestRow, grid[i][j]);
bestCol = Math.max(bestCol, grid[j][i]);
}
ans += bestRow + bestCol;
}
return ans;
}
}
class Solution {
public String[] uncommonFromSentences(String A, String B) {
String[] splitA = A.split("\\s+");
String[] splitB = B.split("\\s+");
List<String> arr = new ArrayList<>(Arrays.asList(splitA));
arr.addAll(Arrays.asList(splitB));
List<String> result = new ArrayList<>();
for (int i = 0; i < arr.size(); i++) {
int count = 1;
for (int j = 0; j < arr.size(); j++) {
if (j == i) continue;
if (arr.get(i).equals(arr.get(j))) {
count++;
break;
}
}
if (count == 1){
result.add(arr.get(i));
}
}
return result.toArray(new String[0]);
}
}
class Solution {
public int[] fairCandySwap(int[] A, int[] B) {
// A中的元素总和减去一个自己的元素加上一个B中的元素如果等于B中的元素总和减去一个自己的元素加上一个A中的元素则说这是一个可用的方案
// 定义一个接收返回的数组result
int[] result = new int[2];
int sumA = 0, sumB = 0;
for (int i : A) {
sumA += i;
}
for (int i : B) {
sumB += i;
}
for (int j : A) {
for (int k : B) {
if (sumA - j + k == sumB - k + j) {
result[0] = j;
result[1] = k;
return result;
}
}
}
return result;
}
}
class Solution {
public int surfaceArea(int[][] grid) {
if (grid == null || grid.length < 1 || grid[0].length < 1) return 0;
//统计所有的立方体数量
int blocks = 0;
//统计有多少个面被其他面盖住,那么就在所有的立方体的表面积上减去被盖住的面数×2(因为盖住一个面需要另一个面来盖,所以会损失2个面);
int cover = 0;
for (int i = 0; i < grid.length; ++i) {
for (int j = 0; j < grid[0].length; ++j) {
blocks += grid[i][j];
//这个是统计当前格子中因为堆叠而盖住了几个面
cover += grid[i][j] > 1 ? grid[i][j] - 1 : 0;
if (i > 0) {
//看看上一行同一列盖住了多少个面
cover += Math.min(grid[i - 1][j], grid[i][j]);
}
if (j > 0) {
//看看同一行前一列盖住了几个面
cover += Math.min(grid[i][j - 1], grid[i][j]);
}
}
}
return blocks * 6 - cover * 2;
}
}
class Solution {
public int numSpecialEquivGroups(String[] A) {
Set<String> seen = new HashSet<>();
for (String S: A) {
int[] count = new int[52];
for (int i = 0; i < S.length(); ++i)
count[S.charAt(i) - 'a' + 26 * (i % 2)]++;
seen.add(Arrays.toString(count));
}
return seen.size();
}
}
class Solution {
public boolean isMonotonic(int[] A) {
return isSorted(A, true) || isSorted(A, false);
}
public boolean isSorted(int[] A, boolean increasing) {
int n = A.length;
if (increasing) {
for (int i = 0; i < n - 1; ++i) {
if (A[i] > A[i + 1]) {
return false;
}
}
} else {
for (int i = 0; i < n - 1; ++i) {
if (A[i] < A[i + 1]) {
return false;
}
}
}
return true;
}
}