算法题1:两数之和(LeetCode01:Array And HashMap)
发布日期:2021-05-14 18:05:02 浏览次数:19 分类:精选文章

本文共 1965 字,大约阅读时间需要 6 分钟。

��������������������������������������������������������������������������������������������������������������������������������������������������� HashMap ��������������������������������� O(n) ������������������������������������

������������

������������������������������������������������������������ HashMap ������������������������������������������������������������������

  • ��������������������� HashMap���������������������������������������������������
  • ��������������������������������������������������������� HashMap ������
  • ��������������������������������������������������������������������������������������������������������������������������������� HashMap ������������������������������������������������������������������
  • ��������������������������������� O(n)������������������������������������������������������������������������������������

    ������������

    public class Solution {
    public int[] twoSum(int[] nums, int target) {
    HashMap
    map = new HashMap<>();
    int[] result = new int[2];
    for (int i = 0; i < nums.length; i++) {
    int current = nums[i];
    if (map.containsKey(target - current)) {
    result[0] = map.get(target - current);
    result[1] = i;
    break;
    }
    map.put(current, i);
    }
    return result;
    }
    }

    ������������

  • ��������� HashMap��������������������� HashMap ���������������������������������������������
  • ��������������������������������������������������������� HashMap ���������������������������������������������������������������������������������������
  • ��������������������������������������������������������������������������������������������������������������� HashMap ������������������������������������������������������������������������������������������������������������
  • ������������������������������ O(n) ������������������������������������������������������������������������

    上一篇:什么是时间复杂度
    下一篇:Java:For-each

    发表评论

    最新留言

    初次前来,多多关照!
    [***.217.46.12]2025年04月23日 19时26分55秒