Raj shukla

Approach to solve question leetcode — 17 Letter Combinations of a Phone Number

  1. For solving the problem recursively, we need helper function , in which digits, result and empty string (current string) will be passed as well as there will be a function which will return corresponding characters for each digit.
  2. for each number in digits , we will get possible characters of that number (for 2 ‘abc’ , for 3 ‘def’ like that).
  3. We will add each possible character into the current string and call the function recursively starting form next digit.
  4. then we cut the current string from end otherwise it will be added in next steps recursively.

Code:

--

--

Approach to solve leetcode question- 16 3Sum Closest

  1. This question is somewhat similar to previous question leetcode -15 3Sum with a small change.

2. In this solution , we will take each element of array nums , and then for each element we will take two elements ahead of that element. Find the total of all three and compare which one is closer to the target.

3.return the closest distance from target.

Code :

--

--

Approach to solve leetcode question-15 3Sum

We can get all combinations of 3 elements and see their sum is equal to 0 or not and return all combinations which sums to 0, but this approach will not be efficient.

Instead we will use following approach:

  1. we will sort the array first.

2. we will take each element of array and for elements after this , we will choose two elements , calculate sum of all elements and if it is 0 , add all elements in result array . It will be clear in code.

Code :

--

--

Approach to solve leetcode 12 question — Integer to Roman

  1. This question is quite easy to solve , we just have to declare mappings properly .
  2. After that, we will loop through the mapping and keep decrementing number if it exists in mapping and simultaneously adding corresponding roman into the result.
  3. return the result in end

Code:

--

--