170. Two Sum III - Data structure design
Design a data structure that accepts a stream of integers and checks if it has a pair of integers that sum up to a particular value.
Implement the
TwoSumclass:
TwoSum()Initializes theTwoSumobject, with an empty array initially.void add(int number)Addsnumberto the data structure.boolean find(int value)Returnstrueif there exists any pair of numbers whose sum is equal tovalue, otherwise, it returnsfalse.
Given an array
pointswherepoints[i] = [xi, yi]represents a point on the X-Y plane, returntrueif these points are a boomerang.A boomerang is a set of three points that are all distinct and not in a straight line.
434. Number of Segments in a String
Given a string
s, return the number of segments in the string.A segment is defined to be a contiguous sequence of non-space characters.
A string can be abbreviated by replacing any number of non-adjacent, non-empty substrings with their lengths. The lengths should not have leading zeros.
For example, a string such as
"substitution"could be abbreviated as (but not limited to):
"s10n"("s ubstitutio n")"sub4u4"("sub stit u tion")"12"("substitution")"su3i1u2on"("su bst i t u ti on")"substitution"(no substrings replaced)The following are not valid abbreviations:
"s55n"("s ubsti tutio n", the replaced substrings are adjacent)"s010n"(has leading zeros)"s0ubstitution"(replaces an empty substring)Given a string
wordand an abbreviationabbrA substring is a contiguous non-empty sequence of characters within a string.
2525. Categorize Box According to Criteria
Given four integers
length,width,height, andmass, representing the dimensions and mass of a box, respectively, return a string representing the category of the box.
The box is
"Bulky"if:
- Any of the dimensions of the box is greater or equal to
104.- Or, the volume of the box is greater or equal to
109.If the mass of the box is greater or equal to
100, it is"Heavy".If the box is both
"Bulky"and"Heavy", then its category is"Both".If the box is neither
"Bulky"nor"Heavy", then its category is"Neither".If the box is
"Bulky"but not"Heavy", then its category is"Bulky".If the box is
"Heavy"but not"Bulky", then its category is"Heavy".Note that the volume of the box is the product of its length, width and height.
Given an integer array
nums, return the third distinct maximum number in this array. If the third maximum does not exist, return the maximum number.
Given an array of integers
arr, returntrueif and only if it is a valid mountain array.Recall that arr is a mountain array if and only if:
arr.length >= 3There exists some
iwith0 < i < arr.length - 1such that:
arr[0] < arr[1] < ... < arr[i - 1] < arr[i]arr[i] > arr[i + 1] > ... > arr[arr.length - 1]
914. X of a Kind in a Deck of Cards
You are given an integer array
deckwheredeck[i]represents the number written on theithcard.Partition the cards into one or more groups such that:
- Each group has exactly
xcards wherex > 1, and- All the cards in one group have the same integer written on them.
Return
trueif such partition is possible, orfalseotherwise.
2047. Number of Valid Words in a Sentence
A sentence consists of lowercase letters (
'a'to'z'), digits ('0'to'9'), hyphens ('-'), punctuation marks ('!','.', and','), and spaces (' ') only. Each sentence can be broken down into one or more tokens separated by one or more spaces' '.A token is a valid word if all three of the following are true:
- It only contains lowercase letters, hyphens, and/or punctuation (no digits).
- There is at most one hyphen
'-'. If present, it must be surrounded by lowercase characters ("a-b"is valid, but"-ab"and"ab-"are not valid).- There is at most one punctuation mark. If present, it must be at the end of the token (
"ab,","cd!", and"."are valid, but"a!b"and"c.,"are not valid).Examples of valid words include
"a-b.","afad","ba-c","a!", and"!".Given a string
sentence, return the number of valid words insentence.