problem0001

package
v0.0.0-...-6d8a413 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 18, 2019 License: BSD-3-Clause Imports: 0 Imported by: 0

README

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

题解

  • 暴力破解,两层 for 循环。

    时间复杂度:O(n^2), 空间复杂度:O(1)

  • hash 表

    时间复杂度:O(n), 空间复杂度:O(n)

Benchmark 的结果来看,在数量比较小的时候,暴力破解会更快。

延伸出来的一个问题是?

在实际使用过程中,我们应该怎么权衡何时使用这两种算法呢?

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL