NeetCode 150

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target. You may assume that each input would have exactly one solution, and you may not use the same element twice.

This question is going to use a HashMap data structure so that we can quickly look up the position of an integer we have seen before... iterate through elements. if target - elements[i] is contained in the hash map(i.e. we have seen the complement of elements[i] that adds up to target), return the element and the complement. if complement is not in hash map, store current element and its position, since that could be a possible complement to a element discovered later on.

Given an array of strings strs, group the anagrams together. You can return the answer in any order. i.e. - Input: strs = ["eat","tea","tan","ate","nat","bat"] Output: [["bat"],["nat","tan"],["ate","eat","tea"]] An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.

We will use a hashmap in this case. the format of the string will be key:value with the first sorted string as key, and a list of Strings that we iterate through as value. Then we must iterate through values of hashmap and add it to list, and return the list.

convert a character array to string

create a new string using constructor with character array as argument... new String(ch1), where ch1 is a char[]

how to convert a string to character array

if you have string str1, to convert to a Char array, str1.toCharArray();

how to iterate through all entries in a hashmap

use for each loop-- for(Map.Entry<Key, Value> entrySet: hm){ } where hm is the hashmap


Ensembles d'études connexes

Предмет психології

View Set

A & P 1 Lab Week 4 Exercises 6-7

View Set

Reproduction: Postpartum Newborn and Complications

View Set

Universal Gravitation and Satellite Motion

View Set

Prepu - Chapter 13: Fluid and Electrolytes: Balance and Disturbance

View Set

ManagerialAccounting Ch5 Learnsmart

View Set