public List<Integer> findSubstring(String s, String[] words) {
   List<Integer> res = new ArrayList<Integer>();
   int wlength = words.length;
   if (wlength == 0) return res;
   int size = words[0].length();
   int slength = s.length();
   if (wlength * size > slength) return res;
   HashMap<String, Integer> wordHashMap = new HashMap<String, Integer>();
   for (int i = 0; i < wlength; i++)
     wordHashMap.put(
         words[i], !wordHashMap.containsKey(words[i]) ? 1 : wordHashMap.get(words[i]) + 1);
   for (int i = 0; i < slength - size * wlength + 1; i++) {
     HashMap<String, Integer> tempHashMap = new HashMap<String, Integer>(wordHashMap);
     for (int j = 0; j < wlength; j++) {
       String subString = s.substring(i + j * size, i + (j + 1) * size);
       if (tempHashMap.containsKey(subString)) {
         int count = tempHashMap.get(subString);
         if (count == 1) tempHashMap.remove(subString);
         else tempHashMap.replace(subString, count - 1);
       } else {
         break;
       }
     }
     if (tempHashMap.isEmpty()) res.add(i);
   }
   return res;
 }
 public int longestConsecutive(int[] num) {
   if (null == num || 0 == num.length) return 0;
   int max = 1;
   HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
   for (int i : num) {
     if (map.containsKey(i)) continue;
     map.put(i, 1);
     if (map.containsKey(i - 1)) max = Math.max(max, merge(map, i - 1, i));
     if (map.containsKey(i + 1)) max = Math.max(max, merge(map, i, i + 1));
   }
   return max;
 }
  public String minWindow(String s, String t) {
    if (s.length() == 0 || t.length() > s.length()) return "";
    HashMap<Character, Integer> target = new HashMap<Character, Integer>();
    char[] tArr = t.toCharArray();
    for (char one : tArr) {
      if (!target.containsKey(one)) target.put(one, 1);
      else target.put(one, target.get(one) + 1);
    }

    int len = s.length() + 1;
    String res = "";
    int pos = 0;
    int prev = 0;
    int count = 0;
    HashMap<Character, Integer> m = new HashMap<Character, Integer>();
    while (pos < s.length()) {
      char oneChar = s.charAt(pos);
      if (target.containsKey(oneChar)) {
        if (!m.containsKey(oneChar)) {
          count++;
          m.put(oneChar, 1);
        } else {
          m.put(oneChar, m.get(oneChar) + 1);
          if (m.get(oneChar) <= target.get(oneChar)) count++;
        }
      }

      // move the prev to update len
      if (count == t.length()) {
        while (prev <= pos) {
          char temp = s.charAt(prev);
          if (!m.containsKey(temp)) prev++;
          else if (m.get(temp) > target.get(temp)) {
            prev++;
            m.put(temp, m.get(temp) - 1);
          } else {
            if (pos - prev + 1 < len) {
              len = pos - prev + 1;
              res = s.substring(prev, pos + 1);
            }
            break;
          }
        }
      }
      pos++;
    }
    return res;
  }
 public boolean canPermutePalindrome(String s) {
   // Time: O(n), Space: O(n)
   if (s == null || s.length() == 0) {
     return true;
   }
   HashMap<Character, Integer> map = new HashMap<Character, Integer>();
   for (int i = 0; i < s.length(); i++) {
     char c = s.charAt(i);
     if (!map.containsKey(c)) {
       map.put(c, 1);
     } else {
       map.put(c, map.get(c) + 1);
     }
   }
   boolean single = false;
   for (Integer i : map.values()) {
     if (i % 2 == 1) {
       if (single) {
         return false;
       }
       single = true;
     }
   }
   return true;
 }
示例#5
0
  public int[] twoSum_hashmap(int[] numbers, int target) {
    if (numbers == null || numbers.length < 2) {
      return null;
    }
    HashMap<Integer, Integer> hs = new HashMap<Integer, Integer>();
    for (int i = 0; i < numbers.length; i++) {
      hs.put(numbers[i], i + 1);
    }

    int[] a = new int[2];

    for (int i = 0; i < numbers.length; i++) {
      if (hs.containsKey(target - numbers[i])) { // the complexity for containsKey is basically O(1)
        int index1 = i + 1;
        int index2 = hs.get(target - numbers[i]);
        if (index1 == index2) {
          continue;
        }
        a[0] = index1;
        a[1] = index2;
        return a;
      }
    }
    return a;
  }
示例#6
0
  /**
   * @param k: The number k.
   * @return: The kth prime number as description.
   */
  public long kthPrimeNumber(int k) {
    PriorityQueue<Long> queue = new PriorityQueue<Long>();
    HashMap<Long, Boolean> map = new HashMap<Long, Boolean>();

    Long[] Primes = new Long[3];
    Primes[0] = Long.valueOf(3);
    Primes[1] = Long.valueOf(5);
    Primes[2] = Long.valueOf(7);

    for (int i = 0; i < Primes.length; ++i) {
      queue.add(Primes[i]);
      map.put(Primes[i], true);
    }

    Long number = Primes[0];
    for (int i = 1; i <= k; ++i) {
      number = queue.poll();
      for (int j = 0; j < Primes.length; ++j) {
        if (map.containsKey(number * Primes[j])) {
          continue;
        } else {
          map.put(number * Primes[j], true);
          queue.add(number * Primes[j]);
        }
      }
    }
    return number;
  }
  /**
   * @param nums1 an integer array
   * @param nums2 an integer array
   * @return an integer array
   */
  public int[] intersection(int[] nums1, int[] nums2) {
    // Write your code here
    if (nums1 == null || nums2 == null) return null;
    HashMap<Integer, Integer> map = new HashMap();
    for (int i = 0; i < nums1.length; i++) {
      if (map.get(nums1[i]) != null) {
        map.put(nums1[i], map.get(nums1[i]) + 1);
      } else {
        map.put(nums1[i], 1);
      }
    }
    ArrayList<Integer> list = new ArrayList();
    for (int j = 0; j < nums2.length; j++) {
      if (map.containsKey(nums2[j]) && map.get(nums2[j]) > 0) {
        list.add(nums2[j]);
        map.put(nums2[j], map.get(nums2[j]) - 1);
      }
    }

    int[] result = new int[list.size()];

    for (int k = 0; k < list.size(); k++) {
      result[k] = list.get(k);
    }

    return result;
  }
示例#8
0
  public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> res = new ArrayList<List<Integer>>();
    List<Integer> list = new ArrayList<Integer>();
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();

    quickSort(nums, 0, nums.length - 1);
    for (int i = 0; i < nums.length; i++) {
      if (i > 0 && nums[i] == nums[i - 1]) continue;

      int target = 0 - nums[i];
      for (int j = i + 1; j < nums.length; j++) {
        int diff = target - nums[j];
        if (map.containsKey(diff)) {
          list.add(nums[i]);
          list.add(diff);
          list.add(nums[j]);
          res.add(list);
          list = new ArrayList<Integer>();
        }
        map.put(nums[j], j);
      }
      map.clear();
    }

    return res;
  }
示例#9
0
 // @return an integer
 public int get(int key) {
   // write your code here
   if (!hash.containsKey(key)) return -1;
   ListNode cur = hash.get(key);
   delete(cur);
   add(cur);
   return cur.val;
 }
  public boolean containsNearbyDuplicate(int[] nums, int k) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    for (int i = 0; i < nums.length; i++) {
      if (map.containsKey(nums[i])) {
        int preIndex = map.get(nums[i]);
        if (i - preIndex <= k) return true;
      }
      map.put(nums[i], i);
    }

    return false;
  }
示例#11
0
  /**
   * Stores a bitmap for use by a game tile in a level.
   *
   * @param int resourceId - The bitmap resource ID.
   * @return Bitmap - The Bitmap instance for the given resource ID.
   */
  private Bitmap setAndGetGameTileBitmap(int resourceId) {
    if (!mGameTileBitmaps.containsKey(resourceId)) {
      BitmapFactory.Options opts = new BitmapFactory.Options();
      opts.inJustDecodeBounds = true;
      Bitmap bitmap = BitmapFactory.decodeResource(mGameContext.getResources(), resourceId);

      if (bitmap != null) {
        mGameTileBitmaps.put(resourceId, bitmap);
      }
    }

    return mGameTileBitmaps.get(resourceId);
  }
示例#12
0
  public int lengthOfLongestSubstring(String s) {
    if (s.length() == 0) return 0;
    HashMap<Character, Integer> map = new HashMap<>();

    int max = 0;
    for (int i = 0, j = 0; i < s.length(); i++) {
      if (map.containsKey(s.charAt(i))) {
        j = Math.max(j, map.get(s.charAt(i)) + 1);
      }
      map.put(s.charAt(i), i);
      max = Math.max(max, i - j + 1);
    }
    return max;
  }
示例#13
0
  public int singleNumber_On(int[] A) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    int i;
    for (i = 0; i < A.length; i++) {
      map.put(A[i], map.containsKey(A[i]) ? map.get(A[i]) + 1 : 1);
    }

    for (i = 0; i < A.length; i++) {
      if (map.get(A[i]) != 3) {
        break;
      }
    }
    return A[i];
  }
示例#14
0
  public boolean check(String s1, String s2) {
    if (s1.equals(s2)) return true;
    if (s1.length() == 1) return false;

    HashMap<Character, Integer> map = new HashMap<Character, Integer>();
    for (int i = 0; i < s1.length(); i++) {
      char c = s1.charAt(i);
      if (map.containsKey(c)) map.put(c, map.get(c) + 1);
      else map.put(c, 1);
    }
    for (int i = 0; i < s2.length(); i++) {
      char c = s2.charAt(i);
      if (map.containsKey(c)) map.put(c, map.get(c) - 1);
      else return false;
    }
    for (Map.Entry<Character, Integer> entry : map.entrySet()) {
      int val = entry.getValue().intValue();
      if (val != 0) return false;
    }

    for (int i = 1; i < s1.length(); i++) {
      String left_s1 = s1.substring(0, i);
      String right_s1 = s1.substring(i);
      String left_s2 = s2.substring(0, i);
      String right_s2 = s2.substring(i);
      if (check(left_s1, left_s2) && check(right_s1, right_s2)) return true;
    }
    for (int i = 1; i < s1.length(); i++) {
      String left_s1 = s1.substring(0, i);
      String right_s1 = s1.substring(i);
      String left_s2 = s2.substring(0, s1.length() - i);
      String right_s2 = s2.substring(s1.length() - i);
      if (check(left_s1, right_s2) && check(right_s1, left_s2)) return true;
    }
    return false;
  }
示例#15
0
文件: 350.java 项目: madgd/leetcode
  public int[] intersect(int[] nums1, int[] nums2) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    List<Integer> intersect = new ArrayList<>();
    for (int i : nums1) {
      if (!map.containsKey(i)) {
        map.put(i, 1);
      } else {
        map.put(i, map.get(i) + 1);
      }
    }
    for (int i : nums2) {
      if (map.containsKey(i) && map.get(i) > 0) {
        intersect.add(i);
        map.put(i, map.get(i) - 1);
      }
    }

    int[] result = new int[intersect.size()];
    int i = 0;
    for (Integer num : intersect) {
      result[i++] = num;
    }
    return result;
  }
示例#16
0
 public boolean isIsomorphic(String s, String t) {
   HashMap<Character, Character> map = new HashMap<>();
   for (int i = 0; i < s.length(); i++) {
     if (map.containsKey(s.charAt(i))) {
       if (t.charAt(i) == map.get(s.charAt(i))) continue;
       else {
         return false;
       }
     } else if (map.containsValue(t.charAt(i))) {
       return false;
     }
     map.put(s.charAt(i), t.charAt(i));
   }
   return true;
 }
  public boolean containsNearbyDuplicate(int[] nums, int k) {
    HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
    int min = Integer.MAX_VALUE;

    for (int i = 0; i < nums.length; i++) {
      if (map.containsKey(nums[i])) {
        int preIndex = map.get(nums[i]);
        int gap = i - preIndex;
        min = Math.min(min, gap);
      }
      map.put(nums[i], i);
    }

    if (min <= k) {
      return true;
    } else {
      return false;
    }
  }
示例#18
0
 // @param key, an integer
 // @param value, an integer
 // @return nothing
 public void set(int key, int value) {
   // write your code here
   if (hash.containsKey(key)) {
     hash.get(key).val = value;
     get(key);
   } else {
     ListNode cur = new ListNode(key, value);
     if (len == capacity) {
       ListNode temp = head.next;
       delete(temp);
       hash.remove(temp.key);
       hash.put(key, cur);
       add(cur);
     } else {
       add(cur);
       hash.put(key, cur);
       len++;
     }
   }
 }
 public int maxPoints(Point[] points) {
   if (points == null || points.length == 0) {
     return 0;
   }
   HashMap<Double, Integer> map = new HashMap<>();
   int res = 0;
   for (int i = 0; i < points.length; i++) {
     int vertical = 0;
     int dup = 1;
     for (int j = i + 1; j < points.length; j++) {
       if (points[i].x == points[j].x) {
         if (points[i].y == points[j].y) {
           dup++;
         } else {
           vertical++;
         }
       } else {
         double slope;
         if (points[j].y == points[i].y) {
           slope = 0.0;
         } else {
           slope = (1.0) * (points[j].y - points[i].y) / (points[j].x - points[i].x);
         }
         if (!map.containsKey(slope)) {
           map.put(slope, 1);
         } else {
           map.put(slope, map.get(slope) + 1);
         }
       }
     }
     for (Integer val : map.values()) {
       res = Math.max(res, dup + val);
     }
     res = Math.max(res, dup + vertical);
     map.clear();
   }
   return res;
 }
示例#20
0
 /*
 我们有一个O(N)的解法。使用Map 来记录index, sum的值。当遇到两个index的sum相同时,表示从index1+1到index2是一个解。
 注意:添加一个index = -1作为虚拟节点。这样我们才可以记录index1 = 0的解。
 空间复杂度:O(N)
 */
 public ArrayList<Integer> subarraySum(int[] nums) {
   ArrayList<Integer> rst = new ArrayList<Integer>();
   if (nums == null || nums.length == 0) {
     return rst;
   }
   int sum = 0;
   HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(); // key: sum, value: index
   map.put(0, -1);
   // we know that sub-array (a,b) has zero sum if SUM(0 ... a-1) = SUM(0 ... b)
   for (int i = 0; i < nums.length; i++) {
     sum += nums[i];
     if (map.containsKey(sum)) {
       // For example:
       //        -3  1  2 -3 4
       // SUM: 0 -3 -2  0 -3 1
       // then we got the solution is : 0 - 2
       rst.add(map.get(sum) + 1);
       rst.add(i);
       return rst;
     }
     map.put(sum, i);
   } // for
   return rst;
 }
  public List<Integer> findSubstring(String s, String[] words) {
    // alg:
    // extra HashMap to count remaining word in words.
    // use two pointers to create a sliding window of length == len(word) in words.
    // check in HashMap if words element is found, and update the counter
    // if a word in current sliding window is not in HashMap.keys, or if a counter is < 0,
    // HashMap back to the original and window moves a step a head.

    // corner case:
    // null, empty, s shorter then the total length of words
    if (s == null || words == null) {
      return null;
    }

    int sl = s.length();
    int wn = words.length;

    if (sl == 0 || wn == 0) {
      return null;
    }

    int wl = words[0].length();
    if (sl < wn * wl) {
      return null;
    }

    // general case:
    List<Integer> rst = new ArrayList<Integer>();
    HashMap<String, Integer> wCounter = new HashMap<String, Integer>();
    for (String w : words) {
      if (!wCounter.containsKey(w)) {
        wCounter.put(w, 1);
      } else {
        wCounter.put(w, wCounter.get(w) + 1);
      }
    }

    String[] sWords = new String[sl - wn * wl];
    for (int i = 0; i < sWords.length; i++) {
      sWords[i] = s.substring(i, i + wl * wn);
    }

    HashMap<String, Integer> swCounter;
    int index;
    String curr;
    for (int j = 0; j < sWords.length; j++) {
      swCounter = new HashMap<String, Integer>();
      index = 0;
      while (index < wn * wl) {
        curr = sWords[j].substring(index, index + wl);
        if (!swCounter.containsKey(curr)) {
          swCounter.put(curr, 1);
        } else {
          swCounter.put(curr, swCounter.get(curr) + 1);
        }

        index += wl;
      }

      if (swCounter.equals(wCounter)) {
        rst.add(j);
      }
    }

    return rst;
  }
 boolean contains(int child) {
   return rec.containsKey(child);
 }
  public List<Integer> findSubstring(String s, String[] words) {
    // alg:
    // extra HashMap to count remaining word in words.
    // use two pointers to create a sliding window of length == len(word) in words.
    // check in HashMap if words element is found, and update the counter
    // if a word in current sliding window is not in HashMap.keys, or if a counter is < 0,
    // HashMap back to the original and window moves a step a head.

    // corner case:
    // null, empty, s shorter then the total length of words
    if (s == null || words == null) {
      return null;
    }

    int sl = s.length();
    int wn = words.length;

    if (sl == 0 || wn == 0) {
      return null;
    }

    int wl = words[0].length();
    if (sl < wn * wl) {
      return null;
    }

    // general case:
    HashMap<String, int[]> counter = new HashMap<String, int[]>();
    for (String w : words) {
      if (!counter.containsKey(w)) {
        counter.put(
            w,
            new int[] {
              1, 1
            }); // second element in value array is the back up of original counter record to
        // recover.
      } else {
        counter.get(w)[0] = counter.get(w)[0] + 1;
        counter.get(w)[1] = counter.get(w)[0];
      }
    }

    int p1 = 0;
    int p2 = p1 + wl - 1; // sliding window.
    int remain = wn; // remain words to find in words
    List<Integer> rst = new ArrayList<Integer>();
    int index = p1;
    String curr = "";
    while (p2 < sl) {
      curr = s.substring(p1, p2 + 1);
      if (counter.containsKey(curr) == false) {
        for (int[] val : counter.values()) {
          val[0] = val[1];
        }
        p1 = index + 1;
        p2 = p1 + wl - 1;
        remain = wn;
        index = p1;
        continue;
      }

      counter.get(curr)[0] = counter.get(curr)[0] - 1;
      if (counter.get(curr)[0] < 0) {
        for (int[] val : counter.values()) {
          val[0] = val[1];
        }
        p1 = index + 1;
        p2 = p1 + wl - 1;
        remain = wn;
        index = p1;
        continue;
      }
      remain--;
      p2 += wl;
      p1 += wl;
      if (remain == 0) {
        rst.add(index);
        for (int[] val : counter.values()) {
          val[0] = val[1];
        }
        p1 = index + 1;
        p2 = p1 + wl - 1;
        remain = wn;
        index = p1;
      }
    }

    return rst;
  }
示例#24
0
 /**
  * Returns <tt>true</tt> if this set contains the specified element.
  *
  * @param o element whose presence in this set is to be tested.
  * @return <tt>true</tt> if this set contains the specified element.
  */
 public boolean contains(Object o) {
   return map.containsKey(o);
 }