zoukankan      html  css  js  c++  java
  • Java HashMap存储问题

     1 public static boolean isIsomorphic(String s, String t) {
     2     Map map1 = new HashMap<>();
     3     Map map2 = new HashMap<>();
     4     for (int i = 0; i < s.length(); i++) {
     5         if(map1.put(s.charAt(i),i) != map2.put(t.charAt(i),i))
     6         {
     7             return false;
     8         }
     9     }
    10     return true;
    11 }

    leetcode连接   https://leetcode.com/problems/isomorphic-strings/

    上面这段代码第4行,i的类型为“int”,此时在Leetcode上提交 做判断程序会出错。而将i的类型改为“Integer”则程序不报错。

    一种解释:map在存储value时,将i值,转换为“Integer”类型,在存储时同一个值会生成两个对象,所以比较相等时会判断错误。

    这个解释貌似很正确。但是,我在windows上IDEA +JDK1.8测试了很多次都没有错误,难道是Leetcode的在线测试环境导致的?不明,待确认....

  • 相关阅读:
    cf C. Vasya and Robot
    zoj 3805 Machine
    cf B. Vasya and Public Transport
    cf D. Queue
    cf C. Find Maximum
    cf B. Two Heaps
    cf C. Jeff and Rounding
    cf B. Jeff and Periods
    cf A. Jeff and Digits
    I Think I Need a Houseboat
  • 原文地址:https://www.cnblogs.com/ibyte/p/5812855.html
Copyright © 2011-2022 走看看