LeetCode刷题日记01

1. 2469. 温度转换

class Solution {
    public double[] convertTemperature(double celsius) {
        return new double[]{celsius + 273.15, celsius * 1.8 + 32.00};
    }
}

2.2235. 两整数相加

class Solution {
    public int sum(int num1, int num2) {
        return num1 + num2;
    }
}

3.2413. 最小偶倍数

class Solution {
    public int smallestEvenMultiple(int n) {
        return n % 2 == 0 ? n : n * 2;
    }
}

4.2160. 拆分数位后四位数字的最小和

这道题用贪心策略,我们易知最好的策略是两个两位数字相加,并且对于每个数字将最小的数字放在十位,较大的数字放在个位,这样得出的结果是最小的。

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());
    }
}

5.2520. 统计能整除数字的位数

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;
    }
}

6.1688. 比赛中的配对次数

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;
    }
}

7.1281. 整数的各位积和之差

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;

    }
}

8.2427. 公因子的数目

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;
    }
}

10.2119. 反转两次的数字

class Solution {
    public boolean isSameAfterReversals(int num) {
        return num < 10 || num % 10 != 0;
    }
}

  • 9
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值