Skip to content

Commit ad47330

Browse files
committed
add lintcode
1 parent 29a8533 commit ad47330

File tree

4 files changed

+130
-30
lines changed

4 files changed

+130
-30
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.duwei.lintcode;
2+
3+
import java.util.PriorityQueue;
4+
5+
/**
6+
* LintCode:791
7+
* 给出n个数,现在要将这n个数合并成一个数,每次只能选择两个数a,b合并,每次合并需要消耗a+b的能量,输出将这n个数合并成一个数后消耗的最小能量。
8+
*/
9+
public class MergeNum {
10+
11+
public int mergeNum(int[] nums) {
12+
PriorityQueue<Integer> mHeap = new PriorityQueue<>();//小根堆
13+
int value = 0;
14+
15+
for (int num : nums) {//全部add进去构造堆
16+
mHeap.add(num);
17+
}
18+
19+
while (mHeap.size() > 1) {
20+
int num1 = mHeap.remove();
21+
int num2 = mHeap.remove();
22+
int afterAdd = num1 + num2;
23+
value += afterAdd;
24+
mHeap.add(afterAdd);
25+
}
26+
27+
return value;
28+
}
29+
30+
public static void main(String[] args) {
31+
int[] a = {1, 2, 3, 4};
32+
33+
MergeNum mergeNum = new MergeNum();
34+
System.out.print(mergeNum.mergeNum(a));
35+
36+
}
37+
38+
39+
}

src/com/duwei/lintcode/PlusAb.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.duwei.lintcode;
2+
3+
/**
4+
* 不使用+运算符计算和
5+
*/
6+
public class PlusAb {
7+
8+
public int add(int a, int b) {
9+
if ((a & b) == 0) {//无进位情况
10+
return a | b;
11+
}
12+
13+
int d = a ^ b;
14+
int c = (a & b) << 1;//进位
15+
16+
while ((d & c) != 0){
17+
c = c<<1;
18+
}
19+
20+
return c;
21+
}
22+
23+
24+
public static void main(String[] args) {
25+
System.out.print(new PlusAb().add(1,1));
26+
}
27+
28+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.duwei.lintcode;
2+
3+
4+
/**
5+
* 翻转一个三位数的整数
6+
*/
7+
public class ReversNum {
8+
public static void main(String[] args) {
9+
10+
}
11+
12+
13+
public int reversNum(int num){
14+
15+
16+
17+
return 0;
18+
}
19+
20+
}
Lines changed: 43 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,50 @@
11
package com.duweri.interview;
2+
23
/**
3-
* 位运算Demo
4-
* @author 杜伟
5-
*
4+
* 位运算Demo
5+
*
6+
* @author 杜伟
67
*/
78
public class BitCompute {
89

9-
public static void main(String[] args) {
10-
//异或
11-
int a = 8;//1000
12-
int b = 9;//1001
13-
System.out.println("a^b = "+(a^b));
14-
15-
int x = 15;//01111=1+2+4+8
16-
int y = 23;//10111=1+2+4+16
17-
System.out.println("x^y = "+(x^y));
18-
19-
//与运算
20-
System.out.println("x&y = "+(x&y));
21-
22-
//或运算
23-
System.out.println("x|y = "+ (x|y));
24-
25-
//左右移运算
26-
System.out.println("8>>2 = "+(8>>2));
27-
System.out.println("8<<2 = "+(8<<2));
28-
29-
//~ 原码01000放进计算机用补码:01000,
30-
//取反计算:10111(这是补码),输出源码(取反加1,符号位不变):11001
31-
System.out.println("~ = "+(~8));
32-
33-
System.out.println("8>>>2="+(8>>>2));
34-
35-
}
10+
public static void main(String[] args) {
11+
//异或
12+
int a = 8;//1000
13+
int b = 9;//1001
14+
////////////0001
15+
System.out.println("a^b = " + (a ^ b));
16+
17+
int x = 15;//01111=1+2+4+8
18+
int y = 23;//10111=1+2+4+16
19+
/////////////11000=16+8=24
20+
System.out.println("x^y = " + (x ^ y));
21+
22+
//与运算
23+
//int x = 15;//01111=1+2+4+8
24+
//int y = 23;//10111=1+2+4+16
25+
///////////////00111=4+2+1=7
26+
System.out.println("x&y = " + (x & y));
27+
28+
//或运算
29+
//int x = 15;//01111=1+2+4+8
30+
//int y = 23;//10111=1+2+4+16
31+
///////////////11111=16+8+4+2+1=31
32+
System.out.println("x|y = " + (x | y));
33+
34+
//左右移运算
35+
//0000 1000>>0000 0010
36+
//0000 1000<<0010 0000
37+
System.out.println("8>>2 = " + (8 >> 2));//2
38+
System.out.println("8<<2 = " + (8 << 2));//32
39+
System.out.println("8<<2 = " + (8 << 5));//256
40+
System.out.println("1<<32 = " + (1 << 32));//1
41+
42+
//~ 原码01000放进计算机用补码:01000,
43+
//取反计算:10111(这是补码),输出源码(取反加1,符号位不变):11001
44+
System.out.println("~ = " + (~8));
45+
46+
System.out.println("8>>>2=" + (8 >>> 2));
47+
48+
}
3649

3750
}

0 commit comments

Comments
 (0)