Showing posts with label Leet Code Interview Easy Problems. Show all posts
Showing posts with label Leet Code Interview Easy Problems. Show all posts

## Wednesday, November 25, 2020

### Leet Code Problems Solutions

Intersection of Two Arrays I

Given two arrays, write a function to compute their intersection.

Example 1:

```Input: nums1 = [1,2,2,1], nums2 = [2,2]
Output: [2,2]
```

Example 2:

```Input: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
Output: [4,9]```

Note:

• Each element in the result should appear as many times as it shows in both arrays.
• The result can be in any order.

• What if the given array is already sorted? How would you optimize your algorithm?
• What if nums1's size is small compared to nums2's size? Which algorithm is better?
• What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
Code : Solution 1 -

```public class ArrayIntersection {

public static int[]  intersect(int[] nums1, int[] nums2) {

if (nums1.length > nums2.length) {
return intersect(nums2, nums1);
}

Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<Integer> list = new ArrayList<Integer>();

for (int i : nums1) {
map.put(i, map.getOrDefault(i, 0) + 1);
}

for (int i : nums2) {
int count = map.getOrDefault(i, 0);
if (count > 0) {
map.put(i, count - 1);
}
}

int[] result = new int[list.size()];

for (int i = 0; i < result.length; i++) {
result[i] = list.get(i);
}

return result;
}

public static void main(String... aa) {
int[] nums1= {1,2,2,1};
int[] nums2= {2,2};

System.out.println(intersect(nums1,nums2));
}
}
```

# Plus One

Given a non-empty array of decimal digits representing a non-negative integer, increment one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

```Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
```

Example 2:

```Input: digits = [4,3,2,1]
Output: [4,3,2,2]
Explanation: The array represents the integer 4321.
```

Example 3:

```Input: digits = 
Output: 
```

Constraints:

• `1 <= digits.length <= 100`
• `0 <= digits[i] <= 9`

Java

### Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > <... 