# 轩辕互动面试总结

2015-07-02 06:30:01 阅读( 54 )

1.对于一个给定的整数数组,”支配者”是在这个数组中出现的频率超过一半的整数,例如:
[3,4,3,2,3,-1,3,3]

0,2,4,6,7

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

public class test {

public static int find(int[] v) {
Map map = new TreeMap();
Integer n = -1;
double size = v.length;
for (Integer i : v) {
if (map.get(i) == null) {
Integer count = 1;
map.put(i, count);
} else {
Integer count = map.get(i);
count = count + 1;
map.put(i, count);
}
}
Set set = map.keySet();
Iterator it = set.iterator();
while (it.hasNext()) {
Integer temp = it.next();
Iterator it2 = map.keySet().iterator();
while (it2.hasNext()) {
Integer temp2 = it2.next();
if (temp2 != temp) {

int nn = map.get(temp);
double d = nn / size;
if (d > 0.5) {
n = temp;
}
}
}
if (n != -1) {
break;
}
}
return n;
}

public static void main(String args[]) {
int[] v = { 3, 4, 3, 2, 3, -1, 3, 3 };
int n = find(v);
System.out.println(n);
}
}
2.“有序数组中绝对值不同的数的个数”指的是,一个已经排好序的整数数组中绝对值不相同的数字的个数;

[-5,-3,-1,0,3,6]

3.一个序列S的子序列是指在该序列S中拥有连续的数组下标的元素所组成的序列.例如,对于给定的序列
S[0]=3S[1]=2S[2]=-6S[3]=4S[4]=0

[2,-6,4,0][3,2,-6,4,0][2][3,2,-6][-6,4][](空的子序列)

[3,-6,0],[1],[3,2,-6,10]

Max(S[p]+S[p+1]+…..+S[q])

5=S[0]+S[1]