全排列
public static void main(String[] args) {
dfs(0);
}
static int[] a = new int[] {1,2,3,4,5,7,8,9,10,12,13};
static int n=11;
static void dfs(int m) {
if(m>=n) {
// if....syso
return;
}
for(int i=m;i<n;i++) {
swap(i,m);
dfs(m+1);
swap(i,m);
}
}
static void swap(int i,int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
质因数分解
public static Map<Integer,Integer> fj(int num){
HashMap <Integer,Integer> m=new HashMap<>();
for(int i=2;i*i<=num;i++) {
while(num%i==0) {
Integer t=m.get(i);
if(t==null) {
m.put(i, 1);
}else {
m.put(i,t+1);
}
num/=i;
}
}
return m;
}
容器
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
map.isEmpty() //返回bool 查看map是否为空
map.containsKey(x) //返回bool 查看map中是否有x健
map.containsValue(x) //返回bool 查看map中是否有x值
map.put(x, k); // 给x初始值k 重复时为覆盖
map.putAll(mp); // 把mp的键值对复制给map
map.get(x); // 获取x的对应的值
map.remove(x) // 移除键为x的键值对
map.clear() // 清空map
for(int u : map.values() ) // 遍历map的元素集
for(int v : map.keySet() ) // 遍历map的键值对集
for(Entry<Integer, Integer> entry : map.entrySet()) // 遍历map
for(Map.Entry<Integer,Integer> x: list)
x.getValue() / x.getKey()
map.put(s, (map.get(s) == null ? 1 : map.get(s) + 1)); //累加
一些单词
false true
continue break
default(switch 语句)
(set<int>) :: iterator it = s.begin();it<s.end();
insert()// (set输入)
链表
public static void main(String[] args) {
Node n1=new Node(1);
Node p=n1;
for(int i=2;i<6;i++) {
p.next=new Node(i);
p=p.next;
}
p.next = n1;
p = n1;
int t=0;
while(t<10) {
System.out.println(p.index);
p=p.next;
t++;
}
}
}
class Node{
Node next;
int index;
public Node(int index) {
this.index=index;
}
最大公约数
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}