天天看點

Two Sum

given an array of integers, find two numbers such that they add up to a

specific target number.

the function twosum should return indices of the two numbers such that they

add up to the target, where index1 must be less than index2. please note that

your returned answers (both index1 and index2) are not zero-based.

you may assume that each input would have exactly one solution.

input: numbers={2, 7, 11, 15},

target=9

output: index1=1, index2=2

naive approach:

using hash-map

basic method of hashmap

modifier and type

method and description

<code>void</code>

<code>()</code>

removes all of the mappings from this map.

<code></code>

returns a shallow copy of

this hashmap instance: the keys and values themselves

are not cloned.

<code>boolean</code>

<code>( key)</code>

returns true if this map contains a

mapping for the specified key.

<code>( value)</code>

returns true if this map maps one or

more keys to the specified value.

<code>&lt;&lt;,&gt;&gt;</code>

returns a  view

of the mappings contained in this map.

returns the value to which the specified key is mapped,

or <code>null</code> if this map contains no mapping for the

key.

returns true if this map contains no

key-value mappings.

<code>&lt;&gt;</code>

of the keys contained in this map.

<code>( key,  value)</code>

associates the specified value with the specified key

in this map.

<code>(&lt;? extends ,? extends &gt; m)</code>

copies all of the mappings from the specified map to

this map.

removes the mapping for the specified key from this map

if present.

<code>int</code>

returns the number of key-value mappings in this

map.

of the values contained in this map.