归档 2016

Java中怎样遍历HashMap效率最高

Java中遍历HashMap对象中的条目(Entry)时,通常有两种方式:

  1. 通过KeySet遍历
  2. 通过EntrySet遍历

下面的测试分别采用KeySet和EntrySet,通过forEach遍历同一个HashMap对象中的条目。

测试结果表明,方法2通过EntrySet遍历效率优于方法1通过KeySet遍历。

Java代码:

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.BeforeClass;
import org.junit.Test;

public class TestMapTraversal {

    private static int MAXCOUNT = 3000000 ...

继续阅读

[LeetCode]Third Maximum Number

题目描述:

LeetCode 414. Third Maximum Number

Given an array of integers, return the 3rd Maximum Number in this array, if it doesn't exist, return the Maximum Number. The time complexity must be O(n) or less.

题目大意:

给定一个整数数组,返回数组中第3大的数,如果不存在 ...

继续阅读

[LeetCode]Arithmetic Slices

题目描述:

LeetCode 413. Arithmetic Slices

A sequence of number is called arithmetic if it consists of at least three elements and if the difference between any two consecutive elements is the same.

For example, these are arithmetic sequence:

1, 3 ...

继续阅读

LeetCode Weekly Contest 8解题报告

LeetCode Weekly Contest 8是LeetCode举办的第七场正式周赛,共4道题目,比赛时长2.5小时。

比赛链接:https://leetcode.com/contest/leetcode-weekly-contest-8/

题解列表:

LeetCode 415. Add Strings

LeetCode 416. Partition Equal Subset Sum

LeetCode 417. Pacific Atlantic Water Flow

LeetCode 418. Sentence Screen Fitting

每月存档

去年

明年