您的位置:首页 > 职场人生

链家笔试题整理

2017-08-27 09:59 316 查看

1.撤销操作如何实现?

撤销功能的实现备忘录模式

2.1000万个数找出两数之和为K的数

用HashMap实现,key为 具体的数字,value为数字出现的次数

3.一个含有n个元素的数组,找出m个数使其和为K

public class Test {
public static void main(String[] args) {
int[] nums ={ 1, 2, 3, 4, 5, 6 };
int sum=10;
Test test = new Test();
test.findNums(nums,sum);
}

public void findNums(int[] nums,int sum){
int length = nums.length;
int count = 1<< length;
for(int i=1;i<count;i++){
int temp =i;
int index = length-1;
int temp_sum=0;
while (temp>0) {
if ((temp & 1) == 1) {
temp_sum += nums[index];
}
temp=temp>>1;
index--;
}
if(temp_sum==sum){
printNums(nums,i);
System.out.println();
}
}
}

public void printNums(int[] nums,int i){
int index = nums.length-1;
while (i>0){
if((i&1)==1){
System.out.print(nums[index]+"  ");
}
i=i>>1;
index--;
}
}
}


4.二叉树中两个节点的最近公共祖先

public int query(BinaryTreeNode node1, BinaryTreeNode node2, BinaryTreeNode root) {
int left = node1.value;
int right = node2.value;
BinaryTreeNode parent = null;

if (left > right) {
int temp = left;
left = right;
right = temp;
}

while (true) {
if (root.value < left) {
parent = root;
root = root.right;
} else if (root.value > right) {
parent = root;
root = root.left;
} else if (root.value == left || root.value == right) {
return parent.value;
} else {
return root.value;
}
}
}


5.IP 与城市的映射

HashMap实现
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  面试