时间好快
面试题目一:海量无序数据中,得到最小的K个数
考察知识点:大顶堆,堆排序建堆,调整过程
package learning;
public class minTopK {
/**
* 利用大根堆求解无序数组的最小的k个数
*/
public static void swap(int[] arr,int index1,int index2){
int tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
public static void heapInsert(int[] arr,int value,int index){
arr[index] = value;//index为0的话一定为根节点
while(index!=0){
int parent = (index -1)/2;
if(arr[parent]arr[index]){
largest = left;
}
if(rightarr[largest]){
largest = right;
}
if(largest!=index){
swap(arr,largest,index);
}else{
break;
}
index = largest;
left = index*2+1;
right = index*2+2;
}
}
public static int[] getMinKNumsByHeap(int[] arr,int k){
if(k<1||k>arr.length){
return arr;
}
int[] kHeap = new int[k];
for(int i=0;i!=k;i++){
heapInsert(kHeap,arr[i],i);
}
for(int i=k;i!=arr.length;i++){
if(arr[i]
考察知识点:动态规划,背包问题
package qidian;
public class Test2 {
public static int KnapSack(int num, int weight[], int value[], int x[], int C){
int V[][] = new int[C+1][C+1];
for(int i = 0 ; i <= num-1 ; i++ ){
V[i][0] = 0; //第一列都为0;
}
for(int j = 0 ; j <=C ; j++){
V[0][j]=0; //第一行都为0
}
for(int i = 1 ; i <= num-1 ; i++){
for(int j = 1 ; j <=C ; j++){
if(j
面试题目3:判断一个数是否为回文数
考察知识点:数制
package learning;
public class palindrome {
/**
* 判断一个数是否为回文数
*/
public static int get_help(int num){
int help = 1;
while(num/10!=0){
num = num/10;
help = help*10;
}
System.out.print(help);
return help;
}
public static boolean ispalindrome(int num){
num = Math.abs(num);
int help = get_help(num);
while(help>1){
int left = num/help;
int right = num%10;
if(left!=right){
return false;
}
num = num%help/10;
help = help/100;
}
return true;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int a = -1211111;
int b = 123321;
int c = 2;
System.out.print(ispalindrome(a));
}
}
面试题目4:将无序数组分成四块,不好切割点分别求和,使得和相同
缺点:正负整型符合不能正确求解,有大神可以解决么?
package qidian;
import java.util.HashMap;
import java.util.Map;
public class searchPoints_2 {
/**
* @param args
*/
public static void main(String[] args){
//int[] input = {2,0,2,0,2,0,2};
//int[] input = {1,3,1,4,2,2,1,1,2,4};
//int[] input = {2,-1,5,3,-2,-1,4,-3,2,1};
int[] input = {2,0,1,2,0,2,0,2};
int[] sums = new int[input.length];
Map hashMap = new HashMap();
int tmp = 0;
for (int i = 0; i < input.length; ++i) {
tmp += input[i];
sums[i] = tmp;
if(!hashMap.containsKey(tmp)){
hashMap.put(tmp, i);
}
}
for (int pos1 = 1; pos1 < input.length; ++pos1) {
int sum = sums[pos1] - input[pos1];
if (hashMap.containsKey(sum + sums[pos1])) {
int pos2 = hashMap.get(sum + sums[pos1]) + 1;
if (pos2 < input.length && hashMap.containsKey(sum + sums[pos2])) {
int pos3 = hashMap.get(sum + sums[pos2]) + 1;
if (pos3 < input.length && sums[sums.length - 1] - sums[pos3] == sum) {
System.out.println("result:"+pos1 + "---"+pos2+"----"+pos3);
return;
}
}
}
}
return;
}
}