1. 判断素数

    import java.util.Scanner;
    public class LC {
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         long num = sc.nextLong();
         if(num > 0) {
             //k为num的正平方根
             int k = (int)Math.sqrt(num);
             //从2开始,到k,如果num能够整除i,说明num能被除了1和它本身的数整除,不是素数
             for (int i = 2; i <= k; i++) {
                 if (num % i == 0) {
                     System.out.println(false);
                     break;
                 }
             }
         }
         System.out.println(true);
     }
    }
  2. 素数的个数

    import java.util.Scanner;
    class Solution {
     public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         int len = sc.nextInt();
         int[] nums = new int[len];
    
         long result = 0;
         //不断除以2就是素数的个数
         for (int i = 0; i < nums.length; i++) {
             nums[i] = sc.nextInt();
             result += nums[i] / 2;
         }
         System.out.println(result);
     }
    }
  1. 字符串加法:https://leetcode-cn.com/problems/add-binary/

    class Solution {
     public String addBinary(String a, String b) {
         //指向字符串尾部
         int len1 = a.length()-1;
         int len2 = b.length()-1;
    
         StringBuilder result = new StringBuilder();
         int carry = 0;
    
         //从尾部开始相加
         while(len1 >=0 || len2 >= 0 || carry != 0){
             if(len1 >= 0){
                 carry += a.charAt(len1--) - '0';
             }
             if(len2 >= 0){
                 carry += b.charAt(len2--) - '0';
             }
             //二进制相加
             result.append(carry%2);
             carry /= 2;
         }
         return result.reverse().toString();
     }
    }
  1. 出现次数为len/2的元素(摩尔投票):https://leetcode-cn.com/problems/majority-element/
    class Solution {
     public int majorityElement(int[] nums) {
         //摩尔投票法:相同就相消
         int count = 0;
         int result = 0;
         for(int i : nums){
             if(count == 0){
                 result = i;
                 count = 1;
             }else if(result == i){
                 count++;
             }else{
                 count--;
             }
         }
         return result;
     }
    }
  1. 阶乘后的0:https://leetcode-cn.com/problems/factorial-trailing-zeroes/
    class Solution {
     public int trailingZeroes(int n) {
         //只有2和5相乘的结果才会有0,只需要考虑5即可
         int result = 0;
         //大于等于5时,不断除以5并叠加
         while(n >= 5){
             result += n/5;
             n /= 5;
         }
         return result;
     }
    }