1. 2469. 温度转换
class Solution {
public double[] convertTemperature(double celsius) {
return new double[]{celsius + 273.15, celsius * 1.8 + 32.00};
}
}
class Solution {
public int sum(int num1, int num2) {
return num1 + num2;
}
}
class Solution {
public int smallestEvenMultiple(int n) {
return n % 2 == 0 ? n : n * 2;
}
}
这道题用贪心策略,我们易知最好的策略是两个两位数字相加,并且对于每个数字将最小的数字放在十位,较大的数字放在个位,这样得出的结果是最小的。
class Solution {
public int minimumSum(int num) {
int[] arr = new int[10];
while (num > 0) {
arr[num % 10]++;
num /= 10;
}
StringBuilder num1 = new StringBuilder();
StringBuilder num2 = new StringBuilder();
for (int i = 0; i < 4; i++) {
… }
}
return Integer.parseInt(num1.toString()) + Integer.parseInt(num2.toString());
}
}
class Solution {
public int countDigits(int num) {
int copyNum = num;
int ans = 0;
while (copyNum > 0) {
int tmp = copyNum % 10;
if (num % tmp == 0) ans++;
copyNum /= 10;
}
return ans;
}
}
class Solution {
public int numberOfMatches(int n) {
int ans = 0;
while (n != 1) {
ans += n / 2;
n = (int)Math.ceil((double)n / 2.0);
}
return ans;
}
}
class Solution {
public int subtractProductAndSum(int n) {
int sum = 0, product = 1;
while (n > 0) {
int tmp = n % 10;
sum += tmp;
product *= tmp;
n /= 10;
}
return product - sum;
}
}
class Solution {
public int commonFactors(int a, int b) {
int ans = 0;
int n = Math.min(a, b);
for (int i = 1; i <= n; i++) {
if (a % i == 0 && b % i == 0) ans++;
}
return ans;
}
}
9.728. 自除数
class Solution {
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++) {
int tmpNum, tmpCopyNum = i;
boolean flagNum = true;
while (tmpCopyNum > 0) {
tmpNum = tmpCopyNum % 10;
if (tmpNum == 0 || i % tmpNum != 0) { flagNum = false; break; }
tmpCopyNum /= 10;
}
if (flagNum) list.add(i);
}
return list;
}
}
class Solution {
public boolean isSameAfterReversals(int num) {
return num < 10 || num % 10 != 0;
}
}