This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package junlin.util; | |
import java.util.Comparator; | |
import static junlin.util.Print.*; | |
public class SortUtil { | |
/** | |
* 交换两个变量的值 | |
* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
题目:找出字符串中第一个出现一次的字符 | |
Sample: 'google' => 'l' | |
*/ | |
// 解法一:使用正则匹配字符 | |
function find(str) { | |
for (const i of str) { | |
const char = str[i] | |
const reg = new RegExp(char, 'g') | |
if (str.match(reg).length === 1) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 大整数乘法 | |
* 功能:大整数乘法精确运算,同时解决浮点数误差问题 | |
* 问题:超过 Number.MAX_SAGE_INTEGER 的数不能准确表示,同时运算结果也不准确 | |
* 原理:通过字符串表示大数,并且从字符串末尾开始把每个字符转成数值进行运算,然后再处理进位和借位,最后结果也是用字符串表示 | |
* 说明: | |
* 支持正负整数; | |
* 不支持科学计数法表示的字符串,如 1.2e+10; | |
* 进行运算的字符串假设数值格式尽量标准 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 浮点数精确运算,解决浮点数运算误差问题。比如: | |
* 0.1 + 0.2 = 0.30000000000000004、 | |
* 0.3 - 0.2 = 0.09999999999999998、 | |
* 0.7 * 0.1 = 0.06999999999999999、 | |
* 0.3 / 0.1 = 2.9999999999999996 | |
* 不支持科学计数法表示的浮点数 | |
* 原理:浮点数由于有限的存储位数,无法精确表示超出存储位数的浮点数,所以浮点数运算结果会有误差; | |
* 而整数没有浮点数运算误差的问题,所以在浮点数运算过程中先转成整数再计算,计算结果再转换为浮点数 | |
**/ |