数学公式
常用公式
变形
\begin{aligned}&a ^ 3 - b ^ 3 = (a - b)(a ^ 2 + a b + b ^ 2) \\&a ^ 3 + b ^ 3 = (a + b)(a ^ 2 - ab + b ^ 2) \\\end{aligned}
三角函数
泰勒展开
\begin{aligned}&f(x) = f(x_0) + f'(x_0)(x - x_0) + \frac{f''(x_0)}{2}(x - x_0) ^ 2 + .... + \frac{f^{(n)}(x_0)}{n!}(x - x_0)^n \\&\sin x = x -\frac{x^3}{6} +o(x^3)\\\\ &\cos x=1-\frac{x^2}{2}+\frac{x^4}{24}+o(x^4)\\\\&\tan x = x +\frac{x^3}{3}+o(x^3)\\\\ &\sec x = 1+\frac{1}{2}x^2 + \frac{5}{24}x^4 + o(x^4) \\\\ &\csc x = \frac{1}{x} + \frac{1}{6}x + o(x) \\\\ &\cot x = \frac{1}{x} - \frac{1}{3}x + o(x) \\\\ &\arcsin x=x+\frac{x^3}{6}+o(x^3)\\\\&\arccos x=\frac{\pi}{2} - \arcsin x\\\\ &\arctan x=x-\frac{x^3}{3}+o(x^3)\\\\&e^x = 1+x+\frac{x^2}{2!}+\frac{x^3}{3!}+o(x^3)\\\\&ln(1+x)=x-\frac{x^2}{2}+\frac{x^3}{3}+o(x^3)\\\\&(1+x)^{\alpha}=1+\alpha x+\frac{\alpha(\alpha-1)}{2}x^2+o(x^2) \\\\&(1+x)^{\frac{1}{x}} = e - \frac{e}{2}x + \frac{11e}{24}X^2 + O(X^2) \\\\ &e^{\sin x} = 1+x+\frac{1}{2^1}x^2 + 0(x^2)\\\\&e^{\tan x} = 1 + x + \frac{1}{2}x^2 + \frac{1}{2}x^3 + o(x^3) \\\\&\ln(\frac{1+x}{1-x}) = 2x + \frac{2}{3}x^3 + \frac{2}{5}x^5 + o(x^5)\\\\&\frac{1}{1-x} = 1 + x + x ^ 2 + o(x ^ 2) \\\\ &\ln(\sin x) = \ln x-\frac{1}{6}x^2+o(x^2) \\\\ &\ln(\cos x) = -\frac{1}{2}x^2 + \frac{1}{12}x^4 + o(x^4) \\\\ &\ln(\tan x) = \ln x + \frac{1}{3}x^2 + o(x^2) \\\\ &\sinh x = x + \frac{1}{6}x ^ 3 + o(x ^ 3) \\\\ &\cosh x = 1 + \frac{1}{2}x ^ 2 + o(x ^ 2) \\\\ &\tanh x = x - \frac{1}{3}x ^ 3 + o(x ^ 3) \\\\ &{\rm sech}x = 1 - \frac{1}{2}x ^ 2 + o(x ^ 2) \\\\ &{\rm arsinhx} = \ln (x + \sqrt{1 + x ^ 2}) = x - \frac{1}{6}x ^ 3 + o(x ^ 3) \\\\&{\rm artanhx} = x + \frac{1}{3} x ^ 3 + \frac{1}{5} x ^ 5 + \frac{1}{7} x ^ 7 + o(x ^ 7) \\\\ &\tan(\tan x) = x + \frac{2}{3} x ^ 3 + \frac{3}{5} x ^ 5 + o(x ^ 5) \\\\ &\sin(\sin x) = x - \frac{1}{3} x ^ 3 + o(x ^ 3) \\\\&\tan(\sin x) = x + \frac{1}{6} x ^ 3 + o(x ^ 3) \\\\ &\sin(\tan x) = x + \frac{1}{6} x ^ 3 + o(x ^ 3) \\\\ \end{aligned}
积分、导数
不可积的不定积分
\begin{aligned}\int \sin(x^2)\, {\rm d}x \quad \int \cos(x^2)\, {\rm d}x \quad \int \frac{\sin x}{x}\, {\rm d}x \quad\int \frac{\cos x}{x}\, {\rm d}x \quad \int \frac{ {\rm d}x} {\ln x}\ \quad \int e^{x^2}\, {\rm d}x \quad\int \frac{e^x}{x}\, {\rm d}x \end{aligned}
常用积分
\begin{aligned}&\int k\, {\rm d}x = kx + C \quad \int x^\alpha\, {\rm d}x = \frac{x^{\alpha + 1}}{\alpha + 1} + C \quad \int \frac{1}{x}\, {\rm d}x = \left| x\right| + C \\\\&\int a^x\, {\rm d}x = \frac{1}{\ln a}a^x + C\, \left( a > 0, a \neq 1 \right) \quad \int e^x\, {\rm d}x = e^x + C \\\\&\int \sec^2x\, {\rm d}x = \int \frac{1}{\cos^2x}\, {\rm d}x = \tan x + C \quad\int \csc^2x\, {\rm d}x = \int \frac{1}{\sin^2x}\, {\rm d}x = -\cot x + C \\\\ &\int \tan x\sec x\, {\rm d}x = \sec x + C \quad \int \cot x\csc x\, {\rm d}x = -\csc x + C \\\\ &\int \tan x\, {\rm d}x = - \ln {\left|\cos x \right|}\, {\rm d}x + C \quad \int \cot x\, {\rm d}x = \ln {\left|\sin x \right|}\, {\rm d}x + C \\\\ &\int \sec x\, {\rm d}x = \ln{\left| \sec x + \tan x\right|} + C \quad \int \csc x\, {\rm d}x = \ln{\left| \csc x - \cot x\right|} + C \\\\ &\int \frac{ {\rm d}x}{\sqrt{a^2 - x^2} } = \arcsin \frac{x}{a} + C\, \left( a > 0\right) \\\\ &\int \frac{ {\rm d}x}{\sqrt{x^2 \pm a^2} } = \ln {\left|x + \sqrt{x^2 \pm a^2} \right|} + C\, \left( a > 0\right) \\\\ &\int \frac{ {\rm d}x}{a^2+x^2} = \frac{1}{a} \arctan \frac{x}{a} + C\, \left( a > 0\right) \\\\&\int \frac{ {\rm d}x}{x^2 - a^2} = \frac{1}{2a} \ln {\left|\frac{x - a}{x + a} \right|} + C\, \left( a > 0\right)\end{aligned}
导数(微分)
\begin{aligned}&(1) C' = 0\left( c为常数\right) \quad \left( x^\alpha \right)' = \alpha(x) ^ {\alpha - 1} \quad \left( a^x\right)' = a^x\ln a \\\\ & \quad \qquad \qquad \qquad \quad \left( e^x\right)' = e^x \quad (\ln \left| x\right|) = \frac{1}{x} \\\\ &(2)(\sin x)' = \cos x \quad (\cos x)' = -\sin x \quad (\tan x)' = \sec ^ 2x \\\\&\quad (\cot x)' = -\csc ^ 2x \quad (\sec x)' = \sec x\tan x \quad (\csc x)' = -\csc x\cot x \\\\ &(3) [\ln (x + \sqrt{x^2+a^2})]' = \frac{1}{\sqrt {x^2+a^2} } \quad [\ln (x + \sqrt{x^2 - a^2})]' = \frac{1}{\sqrt{x^2 - a^2} } \\\\ &(4) (\arcsin x)' = \frac{1}{\sqrt {1 - x^2} } \quad (\arccos x)' = -\frac{1}{\sqrt{1 - x^2} } \\\\& \quad (\arctan x)' = \frac{1}{x^2 + 1} \quad ({\rm arc}{\cot x})' = -\frac{1}{1 + x^2} \\\\\end{aligned} 变限函数求导
\begin{aligned}&\left[ \int_{\psi(x)}^{\varphi(x)} f(t)\, {\rm d}t\right]' = f\left[ \varphi(x) \right] \cdot \varphi(x)' - f\left[ \psi(x) \right] \cdot \psi(x)'\end{aligned}
曲率、曲率半径、曲率圆
曲率$K=\frac{|y’’|}{(1 + y’)^{3/2}}$; 曲率半径$R=\frac{1}{K}$;曲率圆与曲线在切点$M$有相同的切线、凹凸线和曲率。
渐近线
名称 | 定义 |
---|---|
铅直渐近线 | 若\lim_{x\to{x_0} ^ +}f(x) = \infty 或 \lim_{x\to{x_0} ^ -}f(x) = \infty ,则x = x_0 是曲线y = f(x)的铅直渐近线 |
水平渐近线 | 若\lim_{x \to {+ \infty} } = A或\lim_{x \to {- \infty} } = A,则 y = A 是曲线 y = f(x) 的水平渐近线 |
斜渐近线 | 若\lim_{x \to {+ \infty} } \frac{f(x)}{x} = a(a \ne 0)且\lim_{x \to {+\infty} }[f(x) - ax] = b则y = ax + b是曲线y = f(x)的斜渐近线 |
常见凑微分
\begin{aligned}&(1)\int \sin xf(\ln x)\, {\rm d} x = -\int f(\cos x)\, {\rm d}{cosx} \\\\&(2)\int \frac{1}{x}f(\ln x)\, {\rm d}x = \int f(\ln x)\, {\rm d}{lnx} \\\\&(3)\int \frac{1}{x^2}\, {\rm d}x = -\int f(\frac{1}{x})\, {\rm d}\frac{1}{x} \\\\&(4)\int \frac{1}{\sqrt x}f(\sqrt x)\, {\rm d}x = 2\int f(\sqrt x)\, {\rm d}\sqrt x \\\\ &(5)\int x^{n - 1}f(x^n)\, {\rm d}x = \frac{1}{n}\int f(x^n)\, {\rm d}x^n \\\\ \end{aligned}
全微分
2.全微分公式
hexo常用标签外挂
折叠框 folding
{% folding 参数(可选), 标题 %}

{% endfolding %}
颜色:blue, cyan, green, yellow, red
状态:状态填写 open 代表默认打开。
{% folding %}
{% endfolding %}
分栏 tab
{% tabs Unique name, [index] %}
<!-- tab [Tab caption] [@icon] -->
Any content (support inline tags too).
<!-- endtab -->
{% endtabs %}
Unique name :
选项卡块标签的唯一名称,不带逗号。
将在 #id 中用作每个标签及其索引号的前缀。
如果名称中包含空格,则对于生成 #id,所有空格将由破折号代替。
仅当前帖子 / 页面的 URL 必须是唯一的!
[index]:
活动选项卡的索引号。
如果未指定,将选择第一个标签(1)。
如果 index 为 - 1,则不会选择任何选项卡。
可选参数。
[Tab caption]:
当前选项卡的标题。
如果未指定标题,则带有制表符索引后缀的唯一名称将用作制表符的标题。
如果未指定标题,但指定了图标,则标题将为空。
可选参数。
[@icon]:
FontAwesome 图标名称(全名,看起来像 “fas fa-font”)
可以指定带空格或不带空格;
例如’Tab caption @icon’ 和 ‘Tab caption@icon’.
可选参数。
{% tabs test1 %}
<!-- tab -->
<!-- endtab -->
<!-- tab -->
<!-- endtab -->
<!-- tab -->
<!-- endtab -->
{% endtabs %}
诗词标签 poem
{% poem [title],[author] %}
诗词内容
{% endpoem %}
title:诗词标题
author:作者,可以不写
{% poem %}
{% endpoem %}
单选列表 radio
{% radio 样式参数(可选), 文本(支持简单md) %}
颜色: red,yellow,green,cyan,blue,gray
选中状态: checked
{% radio %}
复选列表 checkbox
{% checkbox 样式参数(可选), 文本(支持简单md) %}
样式: plus, minus, times
颜色: red,yellow,green,cyan,blue,gray
选中状态: checked
{% checkbox %}
上标标签 tip
{% tip [参数,可选] %}文本内容{% endtip %}
样式: success,error,warning,bolt,ban,home,sync,cogs,key,bell
{% tip %}{% endtip %}
样式预览
{% tip %}默认情况{% endtip %}
{% tip success %}success{% endtip %}
{% tip error %}error{% endtip %}
{% tip warning %}warning{% endtip %}
{% tip bolt %}bolt{% endtip %}
{% tip ban %}ban{% endtip %}
{% tip home %}home{% endtip %}
{% tip sync %}sync{% endtip %}
{% tip cogs %}cogs{% endtip %}
{% tip key %}key{% endtip %}
{% tip bell %}bell{% endtip %}
{% tip fa-atom %}自定义font awesome图标{% endtip %}
默认情况
success
error
warning
bolt
ban
home
sync
cogs
key
bell
自定义font awesome图标
动态标签 anima
更多详情请参看 font-awesome-animation 文档
faa-parent animated-hover
。(详情见示例及示例源码)
You can regulate the speed of the animation by adding the CSS class or . faa-fastfaa-slowfaa-fast
或 faa-slow
来控制动画快慢。{% tip [参数,可选] %}文本内容{% endtip %}
{% tip %}
{% endtip %}
样式预览
Qt常用快捷键
一、快捷键配置方法: 进入“工具->选项->环境->键盘”即可配置快捷键。
二、常用默认快捷键: 编号 快捷键 功能 1 Esc 切换到代码编辑状态 2 F1 查看帮助(选中某一类或函数,按下F1,出现帮助文档) 3 F2 在光标选中对象的声明和定义之间切换(和Ctrl+鼠标左键一样的效果,选中某一类或函数,按下F2,迅速定位到该类或函数声明的地方或被调用的地方)
4 F3 查找下一个 5 F4 头文件和源文件之间切换 6 F5 开始调试/继续执行 7 F9 设置和取消断点 8 F10 单步跳过 9 F11 单步进入 10 Shift + F2 声明和定义之间切换 11 Shift + F3 查找上一个 12 Shift + F5 停止调试 13 Shift + F11 单步跳出 14 Ctrl + 0 编辑界面恢复默认字体 15 Ctrl + 1 激活欢迎模式 16 Ctrl + 2 激活编辑模式 17 Ctrl + 3 激活调试模式 18 Ctrl + 4 激活项目模式 19 Ctrl + 5 激活帮助模式 20 Ctrl + 6 激活输出模式 21 Ctrl + B 编译工程 22 Ctrl + E释放后按0 删除新建的分栏 23 Ctrl + E释放后按1 删除所有分栏 24 Ctrl + E释放后按2 上下分栏 25 Ctrl + E释放后按3 左右分栏 26 Ctrl + F 查找/替换当前选中的内容,对与所选内容一致的部分进行高亮显示 27 Ctrl + I 代码格式化 28 Ctrl + K 定位文件,可以找到对应的文件(符号说明:? 帮助、: 类和方法、1 数字 定位当前文件中行、m 类、 f 方法、a 任何工程中、p 当前工程中)
29 Ctrl + L 跳到某一行 30 Ctrl + M 添加/删除书签(书签也可以添加文字标注) 31 Ctrl + W 关闭当前编辑的文件 32 Ctrl + R 运行工程 33 Ctrl + . 下一个书签 34 Ctrl + , 上一个书签 35 Ctrl + / 多行注释/取消多行注释 36 Ctrl + [ 跳到代码块的头部 37 Ctrl + ] 跳到代码块的尾部 38 Ctrl + Tab 快速切换已打开的文件 39 Ctrl + Space 自动补全(Windows系统下与输入法切换快捷键冲突) 40 Ctrl + 转动鼠标滚轮 调整编辑区字体大小 41 Ctrl + Shift + F5 重启调试 42 Ctrl + Shift + F 在项目/文件夹下查找 43 Ctrl + Shift + R 局部变量统一修改(将光标移动到需要更改的变量上,按Ctrl+Shift+ R,当前变量名称外框为红色时,表示已经已激活全局修改功能,当修改此处变量名称时将一同修改代码中所有使用该变量的变量名。),全局变量开启全局替换窗口
44 Ctrl + Shift + U 查找所有使用该符号的地方 45 Ctrl + Shift + < 折叠代码块 46 Ctrl + Shift + > 展开代码块 47 Ctrl + Shift + Up 将当前行的代码向上移动一行 48 Ctrl + Shift + Down 将当前行的代码向下移动一行 49 Alt + 0 隐藏或显示边栏,编辑模式下起作用(有时写的函数太长,屏幕不够大,就用这个) 50 Alt + 1 切换至/关闭生成窗口 51 Alt + 2 切换至/关闭搜索结果窗口 52 Alt + 3 切换至/关闭应用输出窗口 53 Alt + 4 切换至/关闭编译输出窗口 54 Alt + 5 切换至/关闭QML/JS Console窗口 55 Alt + 6 切换至/关闭概要信息窗口 56 Alt + 7 切换至/关闭版本控制窗口 57 Alt + 9 最大化/最小化输出窗口 58 Alt + 左 光标返回到上一位置 59 Alt + 右 光标前进到下一位置 60 Alt + Enter 将光标移动到h文件中的方法声明,按Alt+Enter,再按回车键将在cpp中添加对应的方法实体
J-STL
/**
* String
* ArrayList
* Queue
* stk
* Deque 双端队列
* Set
* Map
* BitSet
* Pair
* 位运算
* 常用库函数:
* 翻转、去重、随机打乱、sort
* lower_bound/upper_bound、nth_element
*/
// String
@Test
public void test16() throws Exception {
String s1 = "HelloWorld";
// 输出字符串长度
System.out.println(s1.length()); // 10
// 获取字符串中某个字符
System.out.println(s1.charAt(0)); // H
// 判断字符串是否为空
System.out.println(s1.isEmpty()); // False
// 将字符串转化为全小写/全大写 后返回
System.out.println(s1.toLowerCase()); // helloworld
System.out.println(s1.toUpperCase()); // HELLOWORLD
System.out.println(s1); // HelloWorld
// 去除字符串前后的空格
System.out.println("--" + " he llo world ".trim() + "--"); // --he llo world--
// 比较字符串
String s2 = "helloWorld";
System.out.println(s1.equals(s2)); // false 直接比较字符串内容
System.out.println(s1.equalsIgnoreCase(s2)); // true 忽略大小写比较字符串内容
// 字符串连接
System.out.println(s1.concat("You")); // HelloWorldYou 等价于+
// 比较字符串的大小
System.out.println("abc".compareTo("abcd")); // -1 负数意味着左边小,正数左边大,0相等
// 截取字符串 substring(int beginIndex, int endIndex) 左闭右开
String s3 = "我爱你中国";
System.out.println(s3.substring(3)); // 中国
System.out.println(s3.substring(0, 3)); // 我爱你
System.out.println("-------------------------------------");
System.out.println(s1); // HelloWorld
// 测试此字符串是否以指定的后缀结束
System.out.println(s1.endsWith("world")); // false
// 测试此字符串是否以指定的前缀开始
System.out.println(s1.startsWith("He")); // true
// 测试此字符串从指定索引开始的子字符串是否以指定前缀开始
System.out.println(s1.startsWith("ll", 2)); // true
System.out.println("-------------------------------------");
System.out.println(s1); // HelloWorld
// 测试当前字符串是否包含指定的char值序列
System.out.println(s1.contains("ll")); // true
// 返回指定子字符串在此字符串中第一次出现处的索引,没有返回-1
System.out.println(s1.indexOf("ll")); // 2
// 返回指定子字符串在此字符串中第一次出现处的索引,从指定索引开始
System.out.println(s1.indexOf("ll", 5)); // -1
// 返回指定子字符串在此字符串最右边出现的索引
System.out.println(s1.lastIndexOf("o")); // 6
// 返回指定子字符串在此字符串最右边出现的索引,从指定索引开始反向搜索
System.out.println(s1.lastIndexOf("o", 5)); // 4
System.out.println("-------------------------------------");
String s4 = "北京欢迎您! 北京欢迎您!";
// String replace(char a, char b)
// 返回一个新的字符串,它是通过b替此字符串中出现的所有a得到的
System.out.println(s4.replace('北', '南')); // 南京欢迎您! 南京欢迎您!
// String replace(CharSequence a, CharSequence b)
// 返回一个新的字符串,它是通过b替此字符串中出现的所有a得到的
System.out.println(s4.replace("北京", "上海")); // 上海欢迎您! 上海欢迎您!
// String replaceAll(String regex, String b)
// 返回一个字符串,它是通过b替换此字符串所有匹配的正则表达式得到的
String s5 = "12hello34world5java7891mysql456";
// hello,world,java,mysql
System.out.println(s5.replaceAll("\\d+", ",").replaceAll("^,|,$", ""));
// String replaceFirst(String regex, String b)
// 返回一个字符串,它是通过b替换此字符串第一个匹配的正则表达式得到的
// hello,world,java,mysql,
System.out.println(s5.replaceAll("\\d+", ",").replaceFirst("^,|,$", ""));
// boolean matches(String regrex)
// 告知此字符串是否匹配给定的正则表达式
System.out.println("12345".matches("\\d+")); // true
System.out.println("0571-4534289".matches("0571-\\d{7,8}")); // true
// String[] split(String regrex)
// 根据给定正则表达式拆分此字符串
String s6 = " good good study, day day up ";
// 去掉前后空格后用空格将字符串分开, \\s+ 会将多个空格看做一个
String[] strs1 = s6.trim().split("\\s+");
System.out.println(strs1.length); // 6
// String[] split(String regrex, int limit)
// 根据给定正则表达式拆分此字符串, 最多不超过limit个, 如果超过了, 剩下的放到最后一个元素中
String[] strs2 = s6.trim().split("\\s+", 3);
System.out.println(strs2.length); // 3
System.out.println("-------------------------------------");
System.out.println(Integer.parseInt("123")); // String -> 基本数据类型/包装类
System.out.println(Double.parseDouble("1.2e9")); // String -> 基本数据类型/包装类
System.out.println(String.valueOf(123)); // 基本数据类型/包装类 -> String
System.out.println(123.1 + ""); // 基本数据类型/包装类 -> String
// String -> char[]
String s7 = "123abc";
char[] chs = s7.toCharArray();
System.out.println(Arrays.toString(chs)); // [1, 2, 3, a, b, c]
// char[] -> String
System.out.println(new String(chs)); // 123abc
// String -> byte[]
byte[] bs1 = s7.getBytes(); // 使用默认的字符集进行转化
System.out.println(Arrays.toString(bs1)); // [49, 50, 51, 97, 98, 99]
String s8 = "123abc中国";
byte[] bs2 = s8.getBytes("gbk"); // 使用gbk进行编码
System.out.println(Arrays.toString(bs2)); // [49, 50, 51, 97, 98, 99, -42, -48, -71, -6]
// byte[] -> String
System.out.println(new String(bs1)); // 123abc
System.out.println(new String(bs2, "gbk")); // 123abc中国
}
// StringBuilder/StringBuffer
@Test
public void test17() {
// StringBuilder/StringBuffer和String相同的方法这里不演示了
StringBuilder sb = new StringBuilder("abc");
sb.append(1);
sb.append('2');
sb.append("345");
System.out.println(sb); // abc12345
sb.delete(2, 4);
System.out.println(sb); // ab2345
sb.replace(2, sb.length(), "hello");
System.out.println(sb); // abhello
sb.insert(2, false);
System.out.println(sb); // abfalsehello
sb.reverse();
System.out.println(sb); // olleheslafba
String s = sb.substring(1, 3);
System.out.println(s); // ll
System.out.println(sb); // olleheslafba
sb.setCharAt(0, 'a');
System.out.println(sb); // alleheslafba
/**
* 总结:
* (1) 增:append(xxx)
* (2) 删: delete(int start, int end)
* (3) 改: setCharAt(int index, char ch) / replace
* (4) 查: charAt(int index)
* (5) 长度: length()
* (6) 遍历: for (char c : sb.toString().toCharArray()) ...
*/
}
@Test
public void test01() {
// 定义 ArrayList
// 定义一个空 ArrayList
List<Integer> a = new ArrayList<>();
// 定义一个空 ArrayList,初始容量为3
List<Integer> b = new ArrayList<>(3);
// 定义一个包含元素1,2,3 的 ArrayList
ArrayList<Integer> c = new ArrayList<>(Arrays.asList(1, 2, 3));
// 遍历 ArrayList
for (int i = 0; i < c.size(); i++) System.out.print(c.get(i) + " ");
System.out.println();
for (int x : c) System.out.print(x + " ");
System.out.println();
// 获取 ArrayList 第一个和最后一个元素
System.out.println(c.get(0)); // 第一个元素:c[0]常用
System.out.println(c.get(c.size() - 1)); // 最后一个元素
// 在 ArrayList 尾部添加、删除元素
c.add(4);
c.remove(c.size() - 1);
c.remove(c.size() - 1);
System.out.println(c.size()); // 2
// 清空 ArrayList
c.clear();
System.out.println(c.size()); // 0
// ArrayList 之间的比较需要自己实现
}
@Test
public void test02() {
// 定义 queue
Queue<Integer> q = new LinkedList<>();
// 入队、出队
q.add(4); // 4
q.add(3); // 4,3
q.add(7); // 4,3,7
q.remove(); // 3,7
// 获取 queue 队首和队尾元素(无法获取队尾元素)
System.out.println(q.peek()); // 返回队首元素: 3
// 清空 queue
q.clear();
System.out.println(q.size()); // 0
System.out.println(q.isEmpty()); // true
// 定义PriorityQueue
System.out.println("=======================");
Queue<Integer> a = new PriorityQueue<>(); // 小根堆
Queue<Integer> b = new PriorityQueue<>(((o1, o2) -> o2 - o1)); // 大根堆
class Rec implements Comparable<Rec> {
int x, y;
Rec(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Rec o) { return this.x - o.x; }
@Override
public String toString() { return "Rec{" + "x=" + x + ", y=" + y + '}'; }
}
Queue<Rec> c = new PriorityQueue<>(); // 此时为小根堆
// PriorityQueue 中插入元素,删除元素
c.add(new Rec(1, 2));
c.add(new Rec(6, 7));
c.add(new Rec(5, 4));
c.add(new Rec(3, 9));
c.remove();
// 返回堆顶元素
System.out.println(c.peek()); // Rec{x=3, y=9}
PriorityQueue类下面给出了重要的方法,你应该知道。
boolean add(object):将指定的元素插入此优先级队列。
boolean offer(object):将指定的元素插入此优先级队列。
boolean remove(object):从此队列中删除指定元素的单个实例(如果存在)。
Object poll():检索并删除此队列的头部,如果此队列为空,则返回null。
Object element():检索但不删除此队列的头部,如果此队列为空,则返回null。
Object peek():检索但不删除此队列的头部,如果此队列为空,则返回null。
void clear():从此优先级队列中删除所有元素。
Comparator comparator():返回用于对此队列中的元素进行排序的比较器,如果此队列根据其元素的自然顺序排序,则返回null。
boolean contains(Object o):如果此队列包含指定的元素,则返回true。
Iterator iterator():返回此队列中元素的迭代器。
int size():返回此队列中的元素数。
Object [] toArray():返回包含此队列中所有元素的数组。
}
@Test
public void test03() {
// 定义 stk
Deque<Integer> stk = new ArrayDeque<>();
// 入栈,出栈
stk.push(20); stk.push(10); stk.push(30);
stk.pop(); // 弹出30
// 返回栈顶元素
System.out.println(stk.peek()); // 10
// 清空 stk
stk.clear();
System.out.println(stk.size()); // 0
System.out.println(stk.isEmpty()); // true
}
@Test
public void test04() {
// 定义 deque
Deque<Integer> d = new ArrayDeque<>();
// 双端队列中插入、删除元素
d.addLast(20); d.addLast(10); // 尾部插入数据: 20,10
d.removeLast(); // 弹出一个尾部数据10: 20
d.addFirst(40); d.addFirst(30); // 头部插入数据: 30,40,20
d.removeFirst(); // 弹出一个头部数据30: 40,20
// 遍历 deuqe,不能使用fori进行遍历
for (int x : d) System.out.print(x + " ");
System.out.println();
// 返回头部和尾部元素
System.out.println(d.getFirst()); // 40
System.out.println(d.getLast()); // 20
// 清空 deque
d.clear();
System.out.println(d.size()); // 0
System.out.println(d.isEmpty()); // true
}
@Test
public void test05() {
/*
* 有序的set: TreeSet
*/
// 定义 set,元素不能重复,TreeSet内部实现是红黑树,可以保持有序
TreeSet<Integer> a = new TreeSet<>();
class Rec implements Comparable<Rec> {
int x, y;
Rec(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Rec o) { return this.x - o.x; }
}
TreeSet<Rec> b = new TreeSet<>();
// set 中插入、删除元素
a.add(20); a.add(3); a.add(1);
a.remove(3);
// 返回第一个和最后一个元素
System.out.println(a.first()); // 1
System.out.println(a.last()); // 20
// 遍历 set,不能使用fori进行遍历
for (int x : a) System.out.print(x + " ");
System.out.println();
// set 中判断某元素是否存在
System.out.println(a.contains(20));
// ceiling/higher
a.add(4); a.add(12); // a: 1, 4, 12, 20
System.out.println(a.ceiling(4)); // 返回大于等于4的最小元素: 4
System.out.println(a.higher(4)); // 返回大于4的最小元素: 12
// 补充:floor/lower
System.out.println(a.floor(4)); // 返回小于等于4的最大元素: 4
System.out.println(a.lower(4)); // 返回小于4的最大元素: 1
// 清空 set
a.clear();
System.out.println(a.size()); // 0
System.out.println(a.isEmpty()); // true
// Java中没有类似于multiset的集合,可以使用TreeMap实现multiset的各项功能
/*
* 无序的set: HashSet
*/
System.out.println("=======================");
// 定义 set,元素不能重复,HashSet内部实现是哈希表,不可以保持有序
HashSet<Integer> hash = new HashSet<>();
// set 中插入、删除元素
hash.add(1); hash.add(2); hash.add(3); hash.add(4);
hash.remove(2);
// 遍历 set,不能使用fori进行遍历
for (int x : hash) System.out.print(x + " ");
System.out.println();
// 随机返回HashSet中的一个数据
System.out.println(hash.iterator().next());
// set 中判断某元素是否存在
System.out.println(hash.contains(3)); // true
// 清空 set
hash.clear();
System.out.println(hash.size()); // 0
System.out.println(hash.isEmpty()); // true
}
@Test
public void test06() {
/*
* 有序的map: TreeMap
*/
// 定义 map,元素不能重复,内部实现是红黑树,可以保持有序
TreeMap<String, Integer> a = new TreeMap<>();
// map 中插入、删除元素
a.put("wxx", 21); a.put("hh", 18); a.put("other", 20);
a.remove("other");
// 返回第一个和最后一个元素
System.out.println(a.firstEntry()); // hh=18
System.out.println(a.lastEntry()); // wxx=21
System.out.println(a.firstKey()); // 没有 firstValue() !
System.out.println(a.firstEntry().getKey());
System.out.println(a.firstEntry().getValue());
// 遍历 map
for (Map.Entry<String, Integer> p : a.entrySet()) System.out.print(p + "\t");
System.out.println();
for (String k : a.keySet()) System.out.print(k + ":" + a.get(k) + "\t");
System.out.println();
Iterator<Map.Entry<String, Integer>> it = a.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, Integer> e = it.next();
System.out.print(e.getKey() + "~" + e.getValue() + "\t");
}
System.out.println();
for (int v : a.values()) System.out.print(v + "\t"); // 只遍历值
System.out.println();
// 随机返回Map中的一个键
System.out.println(a.keySet().iterator().next());
// 随机返回Map中的一个值
System.out.println(a.values().iterator().next());
// map 中判断某元素是否存在
System.out.println(a.containsKey("wxx")); // true
// ceilingKey/higherKey
a.put("c1", 2); a.put("c2", 4); // a: {c1=2, c2=4, hh=18, wxx=21}
System.out.println(a.ceilingKey("c1")); // 返回大于等于"c1"的最小元素: c1
System.out.println(a.higherKey("c1")); // 返回大于"c1"的最小元素: c2
// 补充:floorKey/lowerKey
System.out.println(a.floorKey("c1")); // 返回小于等于"c1"的最大元素: c1
System.out.println(a.lowerKey("c1")); // 返回小于"c1"的最大元素: null
// 清空 map
a.clear();
System.out.println(a.size()); // 0
System.out.println(a.isEmpty()); // true
// Java中没有类似于multimap的集合
/*
* 无序的map: HashMap
*/
System.out.println("=======================");
// 定义 map,元素不能重复,HashSet内部实现是哈希表,不可以保持有序
HashMap<String, Integer> hash = new HashMap<>();
// map 中插入、删除元素
hash.put("wxx", 21); hash.put("hh", 18); hash.put("other", 20);
hash.remove("other");
// 遍历 map,不能使用fori进行遍历
for (Map.Entry<String, Integer> p : hash.entrySet()) System.out.print(p + "\t");
System.out.println();
for (String k : hash.keySet()) System.out.print(k + ":" + hash.get(k) + "\t");
System.out.println();
for (int v : hash.values()) System.out.print(v + "\t"); // 只遍历值
System.out.println();
// map 中判断某元素是否存在
System.out.println(hash.containsKey("wxx")); // true
// 清空 map
hash.clear();
System.out.println(hash.size()); // 0
System.out.println(hash.isEmpty()); // true
}
// https://www.runoob.com/java/java-bitset-class.html
@Test
public void test07() {
// 定义BitSet
BitSet a = new BitSet(1000), b = new BitSet(1000);
// 赋值
a.set(0, true); // a[0] = true
a.set(2, 8, true); // 将b[2~8)全部设为true
b.flip(1, 9); // b[1~9)全部翻转
// 获取某一位的值
System.out.println(a.get(3)); // true
// 位运算
a.and(b); // & 与
a.or(b); // | 或
a.xor(b); // ^ 异或
// 返回此 BitSet 表示位值时实际使用空间的位数。
System.out.println(a.size());
}
@Test
public void test08() {
// 系统库中的Pair(javafx.util.Pair)
Pair<Integer, Integer> pair = new Pair<>(1, 1);
pair.getKey();
pair.getValue();
// 有些jdk中不包含Pair,比如zulu(https://www.azul.com/)家的jdk,因此推荐自己写一个
class MyPair implements Comparable<MyPair> {
int x, y;
public MyPair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(MyPair o) {
if (x != o.x) return x - o.x;
else return y - o.y;
}
}
MyPair a = new MyPair(1, 2), b = new MyPair(3, 1);
System.out.println(a.compareTo(b)); // -2
// 输出MyPair
System.out.println("(" + a.x + ", " + a.y + ")");
}
/*
& 与
| 或
~ 非
^ 异或
>> 右移
<< 左移
常用操作:
(1)求x的第k位数字 x >> k & 1
(2)lowbit(x) = x & -x,返回x的最后一位1
*/
@Test
public void test09() {
// 翻转 ArrayList
ArrayList<Integer> a = new ArrayList<>(Arrays.asList(1, 2, 3));
Collections.reverse(a);
System.out.println(a);
// 翻转 String
String s = "abcd";
s = new StringBuilder(s).reverse().toString();
System.out.println(s);
// 翻转数组:不存在库函数
int[] b = {1, 2, 3, 4, 5};
for (int l = 0, r = b.length - 1; l < r; l++, r--) {
int t = b[l]; b[l] = b[r]; b[r] = t;
}
System.out.println(Arrays.toString(b));
}
@Test
public void test10() {
// 数组去重
int[] a = {1, 1, 2, 2, 3, 3, 4};
int m = 0;
for (int i = 0; i < a.length; i++) {
if (i == 0 || a[i] != a[i - 1])
a[m++] = a[i];
}
for (int i = 0; i < m; i++) System.out.print(a[i] + " ");
System.out.println();
// ArrayList 去重
ArrayList<Integer> b = new ArrayList<>(Arrays.asList(1, 1, 2, 2, 3, 3, 4));
ArrayList<Integer> res = new ArrayList<>();
for (int i = 0; i < b.size(); i++) {
if (i == 0 || !b.get(i).equals(b.get(i - 1)))
res.add(b.get(i));
}
System.out.println(res);
}
@Test
public void test11() {
// 打乱数组: 实现比较麻烦,思路是先将数组转为List,然后打乱,然后再转为数组
int[] a = {1, 2 , 3, 4, 5};
/*
* int[] 转 List<Integer>
* 1.使用 Arrays.stream 将 int[] 转换成 IntStream。
* 2.使用 IntStream中 的 boxed() 装箱。将 IntStream 转换成 Stream<Integer>。
* 3.使用 Stream 的 collect(),将 Stream<T> 转换成 List<T>,因此正是 List<Integer>。
*/
List<Integer> list = Arrays.stream(a).boxed().collect(Collectors.toList());
Collections.shuffle(list);
/*
* List<Integer> 转 int[]
* 1.想要转换成 int[] 类型,就得先转成 IntStream。
* 这里就通过 mapToInt() 把 Stream<Integer> 调用 Integer::valueOf 来转成 IntStream
* 2.而 IntStream 中默认 toArray() 转成 int[]。
*/
a = list.stream().mapToInt(Integer::valueOf).toArray();
System.out.println(Arrays.toString(a));
// 打乱 ArrayList
ArrayList<Integer> b = new ArrayList<>(Arrays.asList(1, 2 , 3, 4, 5));
Collections.shuffle(b);
System.out.println(b);
}
@Test
public void test12() {
// 对数组进行排序
int[] a = {3, 1, 4, 5, 2};
Arrays.sort(a);
System.out.println(Arrays.toString(a)); // [1, 2, 3, 4, 5]
Integer[] arr = new Integer[a.length]; // 对数组自定义比较运算需要先转为包装类
for (int i = 0; i < a.length; i++) arr[i] = a[i];
// Arrays.sort(arr, new Comparator<Integer>() {
// @Override
// public int compare(Integer o1, Integer o2) {
// return o2 - o1;
// }
// });
Arrays.sort(arr, (o1, o2) -> o2 - o1);
System.out.println(Arrays.toString(arr)); // [5, 4, 3, 2, 1]
// 对 ArrayList 排序
System.out.println("=======================");
ArrayList<Integer> t = new ArrayList<>(Arrays.asList(3, 1, 4, 5, 2));
Collections.sort(t);
System.out.println(t); // [1, 2, 3, 4, 5]
// 对String进行排序
String s = "dcab43210";
char[] cs = s.toCharArray();
Arrays.sort(cs);
String res = new String(cs);
System.out.println(res); // 01234abcd
// 对自定义的类排序
// 两种方式:(1) 传入比较函数; (2) 继承Comparable接口,重写compareTo方法
System.out.println("=======================");
class Rec implements Comparable<Rec> {
int x, y;
public Rec(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Rec o) { return this.x - o.x; } // this在前:升序
@Override
public String toString() { return "Rec{" + "x=" + x + ", y=" + y + '}'; }
}
ArrayList<Rec> r = new ArrayList<>();
for (int i = 0; i < 4; i++) r.add(new Rec(-i, i));
// 方式(1)
Collections.sort(r, (o1, o2) -> o1.x - o2.x); // o1在前:升序
System.out.println(r); // [Rec{x=-3, y=3}, Rec{x=-2, y=2}, Rec{x=-1, y=1}, Rec{x=0, y=0}]
// 方式(2)
Collections.sort(r);
System.out.println(r); // [Rec{x=-3, y=3}, Rec{x=-2, y=2}, Rec{x=-1, y=1}, Rec{x=0, y=0}]
}
@Test
public void test13() {
// java 中不存在这两个函数,可以自己实现
int[] a = {1, 2, 4, 5, 6};
System.out.println(lower_bound(a, 4)); // 4
System.out.println(lower_bound(a, 8)); // null
System.out.println(upper_bound(a, 4)); // 5
System.out.println(upper_bound(a, 6)); // null
}
// 返回 a 中大于等于 x 的最小元素
public Integer lower_bound(int[] a, int x) {
int l = 0, r = a.length;
while (l < r) {
int mid = l + r >> 1;
if (a[mid] >= x) r = mid;
else l = mid + 1;
}
if (r == a.length) return null;
return a[r];
}
// 返回 a 中大于 x 的最小元素
public Integer upper_bound(int[] a, int x) {
int l = 0, r = a.length;
while (l < r) {
int mid = l + r >> 1;
if (a[mid] > x) r = mid;
else l = mid + 1;
}
if (r == a.length) return null;
return a[r];
}
@Test
public void test14() {
// java 中不存在nth_element,可以自己实现
int[] a = {6, 2, 1, 4, 3, 5};
int k = 2; // a[1]已经被排到正确的位置上(递增序),第二小的数
System.out.println(quick_sort(a, 0, a.length - 1, k)); // 2
for (int x : a) System.out.print(x + " ");
System.out.println();
}
// 返回 q[l...r]中第k小的数据
public int quick_sort(int[] q, int l, int r, int k) {
if (l == r) return q[l];
int x = q[l], i = l - 1, j = r + 1;
while (i < j) {
while (q[++i] < x) ;
while (q[--j] > x) ;
if (i < j) {
int t = q[i]; q[i] = q[j]; q[j] = t;
}
}
int sl = j - l + 1;
if (k <= sl) return quick_sort(q, l, j, k);
return quick_sort(q, j + 1, r, k - sl);
}
int[] nums = {4, 2, 6, 7};
// 输出数组中元素
System.out.println(Arrays.toString(nums));
// 求数组最值、求和
int min = Arrays.stream(nums).max().getAsInt();
int max = Arrays.stream(nums).min().getAsInt();
int s = Arrays.stream(nums).sum();
// List<Integer> 转 int[]
List<Integer> a = Arrays.asList(2, 1, 5, 4);
int[] b = a.stream().mapToInt(Integer::valueOf).toArray();
// List<int[]> 转 int[][]
List<int[]> c = Arrays.asList(new int[]{1, 3}, new int[]{4, 2});
int[][] d = c.toArray(new int[0][0]);
// int[] 转 List<Integer>
int[] e = {4, 2, 1, 6};
List<Integer> f = Arrays.stream(e).boxed().collect(Collectors.toList());
@Test
public void test15() {
// 设置随机种子的话可以保证每次运行程序生成的序列时一致的
// 不同于C++, 如果不设置随机种子,每次运行得到的序列时不同的
Random r = new Random();
System.out.println(r.nextInt());
System.out.println(r.nextInt());
// 生成[0, x)范围的随机数据
int x = 15;
System.out.println(r.nextInt(x));
System.out.println("-------------------------------------");
int a = 5, b = 6;
// 生成[a, b)范围的随机数据
System.out.println(r.nextInt(b - a) + a);
// 生成(a, b]范围的随机数据
System.out.println(r.nextInt(b - a) + a + 1);
// 生成[a, b]范围的随机数据
System.out.println(r.nextInt(b - a + 1) + a);
System.out.println("-------------------------------------");
// 生成[a, b)范围的随机数据
System.out.println((int) (Math.random() * (b - a) + a));
// 生成(a, b]范围的随机数据
System.out.println((int) (Math.random() * (b - a) + a + 1));
// 生成[a, b]范围的随机数据
System.out.println((int) (Math.random() * (b - a + 1) + a));
System.out.println("-------------------------------------");
// 生成[0.0, 1,0)范围的随机数据
System.out.println(Math.random());
}