More Easy Flashcards

1
Q

Palindrome Number

A

x = 1221, return true
x = -1221, return false
x = 10, return false
https://leetcode.com/problems/palindrome-number/description/

// edge cases => negative case, ending in zero
Solution: reverse until half(even number of digits) or digit more than half(odd num of digits)

if reversedNum == x or reversedNum/10 == x => return true
if rversed nunber is equal ot first half of the number or first half excluding the mid, then its a palindrome

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Roman to Integer

A

Given roman number = III, what is integer = 3

start from right to left, take the last value
start a loop from the penultimate, call it current value, if current value < last value, subtract current val from current val. Otherwise add

move the current value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Valid Parentheses

A

Use a map to store
map.put(‘]’, ‘[’);
map.put(‘}’, ‘{‘);
map.put(‘)’, ‘(‘);

if you find a closing brace while parsing the input, check if the stack has matching open brace. If not, return false
Otherwise, push it (as it must be an open brace)

if stack is empty, it must be valid parentheses
https://leetcode.com/problems/valid-parentheses/

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Valid word abbreviation

A

https://leetcode.com/problems/valid-word-abbreviation/submissions/1125493232/

internationalization
                   i            

i12iz4n
      j

number  = 0

numer = 12

if char proceed
if number, find the number, get to i + 1th index of the number

if i0iz4n - if number is zero, we return false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Buildings with ocean view

A

https://leetcode.com/problems/buildings-with-an-ocean-view/submissions/1125507827/

Input: heights = [4,2,3,1]
Output: [0,2,3]
Explanation: Building 1 (0-indexed) does not have an ocean view because building 2 is taller.

start from right, maintain a maxheightsofar
last building with always have a view and make maxheightsofar as the last building
from there while traversing left, check if the val is less than

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly