43. 字符串相乘
class Solution {
public String multiply(String num1, String num2) {
if (num1.equals("0")||num2.equals("0")){
return "0";
}
//num[i]* num2[j]: res[i+j+1] res[i+j]
int n = num1.length();
int m = num2.length();
int[] res = new int[n + m];
for (int i = n - 1; i >= 0; i--) {
int a = num1.charAt(i) - '0';
for (int j = m - 1; j >= 0; j--) {
int b = num2.charAt(j) - '0';
res[i + j + 1] += a * b;
res[i + j] += res[i + j + 1] / 10;
res[i + j + 1] %= 10;
}
}
StringBuilder result=new StringBuilder();
int i=0;
while (res[i]==0){
i++;
}
for (;i<res.length;i++){
result.append(res[i]);
}
return result.toString();
}
}
415. 字符串相加
/**
* Copyright (C), 2018-2020
* FileName: addStrings415
* Author: xjl
* Date: 2020/8/3 9:30
* Description: 给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。
*/
package String;
import java.util.Scanner;
public class addStrings415 {
/**
* 从前开始设置为低的计算
*
* @param num1
* @param num2
* @return
*/
public static String addStrings(String num1, String num2) {
//从尾部开始计算
int i = num1.length() - 1, j = num2.length() - 1, add = 0;
//存储结果
StringBuffer ans = new StringBuffer();
//遍历
while (i >= 0 || j >= 0 || add != 0) {
int x = i >= 0 ? num1.charAt(i) - '0' : 0;
int y = j >= 0 ? num2.charAt(j) - '0' : 0;
int result = x + y + add;
ans.append(result % 10);
add = result / 10;
i--;
j--;
}
// 计算完以后的答案需要翻转过来
ans.reverse();
return ans.toString();
}
/**
* 表示的从后面设置为低位
* @param num1
* @param num2
* @return
*/
public static String addStrings2(String num1, String num2) {
//将翻转
String Nnum1 = new StringBuilder(num1).reverse().toString();
String Nuum2 = new StringBuilder(num2).reverse().toString();
//从尾部开始计算
int i = num1.length() - 1, j = num2.length() - 1, add = 0;
//存储结果
StringBuffer ans = new StringBuffer();
//遍历
while (i >= 0 || j >= 0 || add != 0) {
int x = i >= 0 ? num1.charAt(i) - '0' : 0;
int y = j >= 0 ? num2.charAt(j) - '0' : 0;
int result = x + y + add;
ans.append(result % 10);
add = result / 10;
i--;
j--;
}
// 计算完以后的答案需要翻转过来
ans.reverse();
return ans.toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s1 = sc.nextLine();
String s2 = sc.nextLine();
String result = addStrings2(s1, s2);
System.out.println(result);
}
}
50. Pow(x, n)
/**
* Copyright (C), 2018-2020
* FileName: myPow50
* Author: xjl
* Date: 2020/8/14 13:50
* Description:
*/
package Math;
import org.junit.Test;
public class myPow50 {
double quickMul(double x, long N) {
double ans = 1.0;
// 贡献的初始值为 x
double x_contribute = x;
// 在对 N 进行二进制拆分的同时计算答案
while (N > 0) {
if (N % 2 == 1) {
// 如果 N 二进制表示的最低位为 1,那么需要计入贡献
ans *= x_contribute;
}
// 将贡献不断地平方
x_contribute *= x_contribute;
// 舍弃 N 二进制表示的最低位,这样我们每次只要判断最低位即可
N /= 2;
}
return ans;
}
public double myPow(double x, int n) {
long N = n;
return N >= 0 ? quickMul(x, N) : 1.0 / quickMul(x, -N);
}
double mypowhand(double x, long n) {
//递归的出口
if (n == 1) {
return x;
}
//分为奇和偶数的情况去递归的实现
if (n % 2 != 0) {
double half = mypowhand(x, n / 2);
return half * half * x;
} else {
double half = mypowhand(x, n / 2);
return half * half;
}
}
//主要的函数
double mypow(double x, int n) {
if (n == 0 || x == 1) {
return 1;
}
if (n < 0) {
return 1 / mypowhand(x, Math.abs(n));
}
return mypowhand(x, n);
}
@Test
public void test() {
mypow(2, 5);
}
}
69. x 的平方根
/**
* Copyright (C), 2018-2020
* FileName: Square
* Author: xjl
* Date: 2020/8/9 21:57
* Description: 平方根的实现
*/
package Math;
public class Square {
public static void main(String[] args) {
double sqrt = test(10, 0.02);
System.out.println(sqrt);
}
public static double sqrt(double t, Double precise) {
//采用的是二分法的思想来实现的
double low = 0, high = t, middle, squre;
double prec = precise != null ? precise : 1e-7;
//小于的的时候是不能采取的
if (t < 0) {
throw new RuntimeException("Negetive number cannot have a sqrt root.");
}
//判断的什么时候会达到精度要求。
while (high - low > prec) {
//中间值
middle = (low + high) / 2;
//中间值的平方
squre = middle * middle;
//如果是中间数字大于的话
if (squre > t) {
high = middle;
} else {
low = middle;
}
}
return (low + high) / 2;
}
public static double sqrt2(double t, Double precise) {
double x0 = t, x1, differ;
double prec = precise != null ? precise : 1e-7;
while (true) {
x1 = (x0 * x0 + t) / (2 * x0);
differ = x1 * x1 - t;
if (differ <= prec && differ >= -prec) {
return x1;
}
x0 = x1;
}
}
public static float sqrtRoot(float m) {
if (m == 0) {
return 0;
}
float i = 0;
float x1, x2 = 0;
while ((i * i) <= m) {
i += 0.1;
}
x1 = i;
for (int j = 0; j < 10; j++) {
x2 = m;
x2 /= x1;
x2 += x1;
x2 /= 2;
x1 = x2;
}
return x2;
}
/**
* 这个为什么是的Double呢? 因为是的如果是等空的时候
*
* @param t
* @param precise
*/
public static double test(double t, Double precise) {
double low = 0, high = t, mid, square;
double pre = precise != null ? precise : 1e-7;
if (t < 0) {
throw new RuntimeException("Negetive number cannot have a sqrt root.");
}
while ((high - low) > pre) {
mid = (high + low) / 2;
square = mid * mid;
if (square > t) {
high = mid;
} else {
low = mid;
}
}
return (high + low) / 2;
}
}
29. 两数相除
/**
* Copyright (C), 2018-2020
* FileName: divide
* Author: xjl
* Date: 2020/8/14 13:20
* Description: 相除
*/
package Math;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
public class divide {
public int divide(int dividend, int divisor) {
long d1 = dividend;
long d2 = divisor;
//首先将这个值全部变成正数
if (d1 < 0) d1 = -d1;
if (d2 < 0) d2 = -d2;
//设置为被除数
long cur = d2;
List<Long> list1 = new ArrayList<>();
List<Long> list2 = new ArrayList<>();
long bei = 1;
while (cur <= d1) {
list1.add(cur);
list2.add(bei);
cur += cur;
bei += bei;
}
long res = 0;
for (int i = list1.size() - 1; i >= 0; i--) {
if (list1.get(i) <= d1) {
res += list2.get(i);
d1 -= list1.get(i);
}
}
if ((dividend < 0 && divisor > 0) || (dividend > 0 && divisor < 0)) res = -res;
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) return Integer.MAX_VALUE;
return (int) res;
}
@Test
public void test(){
int divide = divide(10, 3);
System.out.println(divide);
}
}