今天去HP面试,考官问的问题有点独到,题目大意是这样的
有两个数组String[] first = {"3","1","2","5","3","2"};
String[] second = {"2","1","3","2"};
以上数组中的数字可以出现很多,限于篇幅就只填那么些了,实现功能的是:
对于second数组出现的元素分别取出不同元素比如这里只有(2,1,3),如果first数组中有出现相匹配的元素比如这里有(3,1,2,3,2),只算出现一次匹配,即这里只匹配到first的第一次出现的3和2,对于每次满足上述条件的匹配打印一个"found".
=========================
以上输出为:
found
found
found

解决方案 »

  1.   

    看不懂?什么意思?是找first的元素是不是在second数组中吗?是就打印found?
      

  2.   

    是的,找first的元素是不是在second数组中,而且只匹配第一次出现的才打印
      

  3.   

    不知道我做的对不对,先贴下,继续关注public void show()
    {
    String[] first = {"3","1","2","5","3","2"}; 
    String[] second = {"2","1","3","2"}; 
            String[] second_copy=new String[second.length];
            int x=0;
            boolean only=true;
            for(int i=0;i<second.length;i++) {
             only=true;
             for(int j=0;j<x;j++)  {
               if(second[i]==second_copy[j])  {
               only=false;break;
              }
             }
             if(only) {
             second_copy[x]=second[i];
             x++;
             }
            }
            for(int i=0;i<second_copy.length;i++)  {
             for(int j=0;j<first.length;j++) {
             if(second_copy[i]==first[j]) {
             System.out.println("found");break;
             }
             }
            }
    }
      

  4.   

    import java.util.Hashtable;
    class match
    {
      public static void main(String[] args) {
        Hashtable<String,Integer> h = new Hashtable<String, Integer>();
        String[] first = {"3","1","2","5","3","2"};
        String[] second = {"2","1","3","2"};
        for (String key:second)
        {
          h.put(key, 1); 
        }
        for (String elem:first)
        {
          if (h.get(elem)!=null)
          {
            System.out.println("found");
            h.remove(elem);
          }
        }
      }
    }
      

  5.   


    两重循环如果两个数组足够长,那就不如MAP了
      

  6.   

    用MAP也未偿不可,但需要的存储空间很大。我直接用一个长度为10的int数组来做判断。
    String[] first = {"3","1","2","5","3","2"}; 
    String[] second = {"2","1","3","2"};
    int[] cs = new int[10];
    for(int i=0;i<second.length;i++){
    if(cs[second.charAt(0)-'0'] == 0)   //如果为0,说明是第一次找到
    cs[second.charAt(0)-'0'] ++;
                                                                //不为0说明已经有了,不需要重复 }
                    //通过上面运算这样cs中为1的索引就对应数字在second中存在 for(int i=0;i<first.length;i++){
    if(cs[first.charAt(0)-'0'] == 1){//说明数字在second中,且是第一次出现
    cs[first.charAt(0)-'0'] = 2;
    System.out.println("found");
    }
    }两次单循环,如果两个数组都很长,要比两重循环节N多计算。
    又比MAP节省大量空间和运算时间。这里仅调用每个元素的charAt(0)一次。其它的都是数学运算,没有什么复杂性。
    而上面的二重循环的==运算实际和MAP内部的hashCode和equals以及put的插入和替换都要比它复杂。因为
    字符串对象本身就保存一个char数组,charAt(0)只不过是对数组索引的包装。
      

  7.   


    class FoundNumber
    {
    static String[] first = { "1", "3", "5", "7", "9", "0" };
    static String[] second = { "0", "2", "3" };
    public static void main(String[] args) 
    {
    for ( String a : first )
    for ( String b : second )
    if ( b.equals( a ) )
    System.out.println( b + ", Found" );
    }
    }
      

  8.   

    即使限定是1位数的数组,first.charAt,second.charAt也是错误的.应该是first[i].charAt(0),second[i].charAt(0)吧?
      

  9.   


    import java.util.ArrayList;
    import java.util.List;public class FindString {
        public static void main(String[] args) {
            String[] first = {"3","1","2","5","3","2"};
            String[] second = {"2","1","3","2"};
            
            List<String> list = new ArrayList<String>();
            for (String str : second) {
                if (!list.contains(str))
                    list.add(str);
            }
            
            for (String str : first) {
                if (list.contains(str)) {
                    System.out.println("found");
                    list.remove(str);
                }
            }
        }
    }
      

  10.   


    原来list和hashtable,hashmap都可以的呀
    都是牛人
      

  11.   


    我的[i]是从我的blog中COPY出为变成html标签<i>了至于元素长度要根据题义,楼主并没说是一个整数,而是说“数字”,就是表示数的字,仅指0~9
    如果是用字符串表示的整数,那“整数”本身就没有意义了,这题就是普通字符串数组。
    变成了字符串去重和查找的问题。