当前位置:编程学习 > JAVA >>

一道面试题。。感觉挺好玩的 有没有简单的解决方法

int[] a = {1,2,3,4,5,6};//要求数字
int[] b = {2,3,4,6,7,8,9};//要求数字

int[] c = {1,2,3,4,5,6,7};//所含数字

现在要写一个算法 要可以判断a,b俩个数组中的数字 是否在C中都有 即a满足 b不满足
ps 三个数组都是无序的
--------------------编程问答--------------------

public static void main(String[] args) {
int[] a = {1,2,3,6,4,5};
int[] b = {9,2,3,4,6,7,8};//要求数字
int[] c = {1,7,2,3,6,4,5};//所含数字
System.out.println(extsis(a,c)+" - "+ extsis(b,c));
}

/**
 * 返回true为全部包含
 * @return
 */
public static boolean extsis(int[] ab,int[] c){
if(ab.length<0||ab.length>c.length  ){
return false;
}
Arrays.sort(ab);
Arrays.sort(c);
//如果数组ab最大值大于c,最小值小于c
if(ab[ab.length-1]>c[c.length-1]||ab[0]<c[0]){
return false;
}
for (int i = 1; i < ab.length-2; i++) {
if(ab[i]!=c[i]){
return false;
}  
}
return true;
}


这样能过不? --------------------编程问答--------------------
引用 1 楼 hzw2312 的回复:

public static void main(String[] args) {
int[] a = {1,2,3,6,4,5};
int[] b = {9,2,3,4,6,7,8};//要求数字
int[] c = {1,7,2,3,6,4,5};//所含数字
System.out.println(extsis(a,c)+" - "+ extsis(b,c));
}

/**
 * 返回true为全部包含
 * @return
 */
public static boolean extsis(int[] ab,int[] c){
if(ab.length<0||ab.length>c.length  ){
return false;
}
Arrays.sort(ab);
Arrays.sort(c);
//如果数组ab最大值大于c,最小值小于c
if(ab[ab.length-1]>c[c.length-1]||ab[0]<c[0]){
return false;
}
for (int i = 1; i < ab.length-2; i++) {
if(ab[i]!=c[i]){
return false;
}  
}
return true;
}


这样能过不?
对于这个题你的算是侥幸能过,有个问题:
  当变成int[] a = {3,6,4,5};
        int[] c = {1,7,3,6,4,5};//所含数字,代码就错误了,原因是被包含的数组可能有断层,不会按照c对应索引对应相同的值。

这样的话应该就可以了:
//main:System.out.println(ext(c,a)+"  --  "+ext(c,b));
public static boolean ext(int[] c,int[] tests){
for(int test:tests){
if(!ArrayUtils.contains(c, test)){
return false;//test在c中有不存在的元素
}
}
return true;
}
--------------------编程问答-------------------- 用Set,线性 --------------------编程问答--------------------
引用 2 楼 u011213572 的回复:
Quote: 引用 1 楼 hzw2312 的回复:


public static void main(String[] args) {
int[] a = {1,2,3,6,4,5};
int[] b = {9,2,3,4,6,7,8};//要求数字
int[] c = {1,7,2,3,6,4,5};//所含数字
System.out.println(extsis(a,c)+" - "+ extsis(b,c));
}

/**
 * 返回true为全部包含
 * @return
 */
public static boolean extsis(int[] ab,int[] c){
if(ab.length<0||ab.length>c.length  ){
return false;
}
Arrays.sort(ab);
Arrays.sort(c);
//如果数组ab最大值大于c,最小值小于c
if(ab[ab.length-1]>c[c.length-1]||ab[0]<c[0]){
return false;
}
for (int i = 1; i < ab.length-2; i++) {
if(ab[i]!=c[i]){
return false;
}  
}
return true;
}


这样能过不?
对于这个题你的算是侥幸能过,有个问题:
  当变成int[] a = {3,6,4,5};
        int[] c = {1,7,3,6,4,5};//所含数字,代码就错误了,原因是被包含的数组可能有断层,不会按照c对应索引对应相同的值。

这样的话应该就可以了:
//main:System.out.println(ext(c,a)+"  --  "+ext(c,b));
public static boolean ext(int[] c,int[] tests){
for(int test:tests){
if(!ArrayUtils.contains(c, test)){
return false;//test在c中有不存在的元素
}
}
return true;
}


所噶 --------------------编程问答-------------------- 如果我写的吧。
我把A,B放到两个数组里面
int[] a = {1,2,3,4,5,6};
就给aa[1],aa[2],aa[3]..赋值为1,
然后遍历c,读取到3的话就判断aa[3]是否为0就可以了 --------------------编程问答-------------------- 面试的那道题目就是连个标点符号都没有?
你应该这么回答:这种需求应该采用cpp。 --------------------编程问答-------------------- java是解决事务问题的,而不是数学问题的,如果math包不能比较好地解决的话,那么用其他语言更合适。面试的人很多时候出的题目都脑残。 --------------------编程问答-------------------- 说错了,是数据结构。 --------------------编程问答-------------------- 这是数据结构方面的吧
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,