周赛258

This commit is contained in:
huangge1199@hotmail.com 2021-09-12 11:41:44 +08:00
parent 21f35cebf4
commit 1fe5e6b470

View File

@ -0,0 +1,40 @@
package com.code.leet.week;
import javafx.util.Pair;
import java.util.*;
/**
* @description:
* @author: Administrator
* @date: 2021/8/22-10:29
*/
public class Solution258 {
public static void main(String[] args) {
Solution258 solution = new Solution258();
}
public String reversePrefix(String word, char ch) {
int index = word.indexOf(ch);
if (index > 0) {
StringBuilder str = new StringBuilder(word.substring(0, index + 1));
word = str.reverse() + word.substring(index + 1);
}
return word;
}
public long interchangeableRectangles(int[][] rectangles) {
Map<Double, Integer> map = new HashMap<>();
for (int[] rectangle : rectangles) {
double key = (double) rectangle[1] / rectangle[0];
map.put(key, map.getOrDefault(key, 0) + 1);
}
long sum = 0;
for (double key : map.keySet()) {
if (map.get(key) > 1) {
sum += (long) map.get(key) * (map.get(key) - 1) / 2;
}
}
return sum;
}
}