1、计数质数(力扣204)
public static int countPrimes(int n) {
int count = 0;
for (int i = 2; i < n; i++) {
if(isPrimes(i)){
count ++;
}
}
return count;
}
public static boolean isPrimes(int num){
for (int i = 2; i * i <= num; i++) {
if (num % i == 0){
return false;
}
}
return true;
}
public static int countPrimes2(int n) {
int[] isPrime = new int[n];
Arrays.fill(isPrime,1);
int count = 0;
for (int i = 2; i < n; i++) {
if (isPrime[i] == 1){
count ++;
if ((long)i * i < n){
for (int j = i * i; j < n; j+=i) {
isPrime[j] = 0;
}
}
}
}
return count;
}
2、七进制数(力扣504)
public String convertToBase7(int num) {
if(num == 0) return "0";
StringBuilder sb = new StringBuilder();
int cur = num < 0 ? -num : num;
while(cur > 0){
sb.append(cur % 7);
cur /= 7;
}
if(num < 0) sb.append("-");
return sb.reverse().toString();
}
3、数字转换为十六进制数(力扣405)
public static String toHex(int num) {
StringBuilder sb = new StringBuilder();
char[] arr = "0123456789abcdef".toCharArray();
if (num ==0) return "0";
while (num != 0){
int temp = num & 15;
sb.append(arr[temp]);
num = num >>> 4;
}
return sb.reverse().toString();
}
4、Excel表列名称(力扣168)
public String convertToTitle(int columnNumber) {
StringBuilder sb = new StringBuilder();
while(columnNumber > 0){
int len = columnNumber % 26;
if(len == 0){
len = 26;
columnNumber -=1;
}
sb.append((char)('A' + len - 1));
columnNumber /= 26;
}
return sb.reverse().toString();
}
public static String convertToTitle(int columnNumber) {
StringBuilder sb = new StringBuilder();
while (columnNumber > 0){
columnNumber --;
sb.append((char)('A' + columnNumber % 26));
columnNumber /= 26;
}
return sb.reverse().toString();
}
5、阶乘后的零(力扣172)
public static int trailingZeroes(int n) {
int count = 0;
while (n >= 5){
count += (n / 5);
n /= 5;
}
return count;
}
6、二进制求和(力扣67)
public static String addBinary(String a, String b) {
int m = a.length() - 1, n = b.length() - 1;
StringBuilder sb = new StringBuilder();
int count = 0;
while (m >= 0 && n >= 0) {
int sum = count;
sum += a.charAt(m--) - '0';
sum += b.charAt(n--) - '0';
count = sum / 2;
sb.append(sum % 2);
}
while (m >= 0){
int sum = count + a.charAt(m--) - '0';
count = sum / 2;
sb.append(sum % 2);
}
while (n >= 0){
int sum = count + b.charAt(n--) - '0';
count = sum / 2;
sb.append(sum % 2);
}
if (count == 1){
sb.append(count);
}
return sb.reverse().toString();
}
7、字符串相加(力扣415)
public static String addStrings(String num1, String num2) {
StringBuilder sb = new StringBuilder();
int m = num1.length() - 1,n = num2.length() - 1;
int count = 0;
while (m >= 0 && n >= 0){
int sum = count;
sum += num1.charAt(m--) - '0';
sum += num2.charAt(n--) - '0';
count = sum / 10;
sb.append(sum % 10);
}
while (m >= 0) {
int sum = count + num1.charAt(m--) - '0';
count = sum / 10;
sb.append(sum % 10);
}
while (n >= 0) {
int sum = count + num2.charAt(n--) - '0';
count = sum / 10;
sb.append(sum % 10);
}
if (count == 1){
sb.append(1);
}
return sb.reverse().toString();
}
public static String addStrings2(String num1, String num2) {
int m = num1.length() - 1,n = num2.length() - 1;
StringBuilder sb = new StringBuilder();
int count = 0;
while (m >= 0 || n >= 0 || count != 0){
int sum1 = m >= 0 ? num1.charAt(m--) - '0': 0;
int sum2 = n >= 0 ? num2.charAt(n--) - '0': 0;
int sum = sum1 + sum2 + count;
count = sum / 10;
sb.append(sum % 10);
}
return sb.reverse().toString();
}
8、最少移动次数使数组元素相等 II(力扣462)
|