1. Two Sum
πππ
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
1 | Given nums = [2, 7, 11, 15], target = 9, |
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15class Solution {
public int[] twoSum(int[] nums, int target) {
int[] resultArray = new int[2];
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) {
resultArray[0] = i;
resultArray[1] = j;
return resultArray;
}
}
}
return resultArray;
}
}
Performance: 27ms
Solution 2:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19public class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null){
return null;
}
int[] result = new int[2];
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++){
if (map.containsKey(target - nums[i])){
result[0] = map.get(target - nums[i]);
result[1] = i;
return result;
} else {
map.put(nums[i], i);
}
}
return result;
}
}
Performance: 9ms
2. Add Two Numbers
πππ
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:1
2
3Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
//Input Validation
if (l1 == null && l2 == null){
return null;
}
if (isNotValidListNode(l1)) {
return l2;
}
if (isNotValidListNode(l2)) {
return l1;
}
//Create dummyListNode head to record the head node address.
ListNode dummyListNode = new ListNode(1);
ListNode currentNode = dummyListNode;
int sum = 0;
int carry = 0;
while (l1 != null && l2 != null){
sum = l1.val + l2.val + carry;
currentNode.next = new ListNode(sum % 10);
carry = sum >= 10 ? 1 : 0;
l1 = l1.next;
l2 = l2.next;
currentNode = currentNode.next;
}
while (l1 != null){
sum = l1.val + carry;
currentNode.next = new ListNode(sum % 10);
carry = sum >= 10 ? 1 : 0;
l1 = l1.next;
currentNode = currentNode.next;
}
while (l2 != null){
sum = l2.val + carry;
currentNode.next = new ListNode(sum % 10);
carry = sum >= 10 ? 1 : 0;
l2 = l2.next;
currentNode = currentNode.next;
}
if (carry != 0) {
currentNode.next = new ListNode(1);
currentNode = currentNode.next;
}
return dummyListNode.next;
}
private boolean isNotValidListNode(ListNode node){
return node == null || (node.val == 0 && node.next == null);
}
}
Performance: 28ms
Solution 2:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode t1 = l1, t2 = l2;
ListNode cur = dummy;
int sum = 0;
while (t1 != null || t2 != null) {
if (t1 != null) {
sum += t1.val;
t1 = t1.next;
}
if (t2 != null) {
sum += t2.val;
t2 = t2.next;
}
cur.next = new ListNode(sum % 10);
cur = cur.next;
sum /= 10;
}
if (sum == 1) {
cur.next = new ListNode(1);
}
return dummy.next;
}
}
Performance: 34ms
3. Longest Substring Without Repeating Characters
πππ
Given a string, find the length of the longest substring without repeating characters.
Example 1:1
2
3Input: "abcabcbb"
Output: 3
Explanation: The answer is "abc", which the length is 3.
Example 2:1
2
3Input: "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.
Example 3:1
2
3
4Input: "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
Solution 1:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27class Solution {
public int lengthOfLongestSubstring(String s) {
if (s == null || s.length() ==0){
return 0;
}
int length = 1;
for (int i = 0; i < s.length(); i++) {
for (int j = i + 1; j <= s.length(); j++) {
if ((j - i > length) && isValidString(s.substring(i, j))) {
length = j - i;
}
}
}
return length;
}
private boolean isValidString(String s) {
char[] charArray = s.toCharArray();
HashSet<Character> set = new HashSet<>();
for (int i = 0; i < charArray.length; i++){
if (set.contains(charArray[i])){
return false;
}
set.add(charArray[i]);
}
return true;
}
}
Performance: Time Limit Exceeded
Solution 2:1
2
3
4
5
6
7
8
9
10
11
12
13public int lengthOfLongestSubstring(String s) {
int n = s.length(), ans = 0;
Map<Character, Integer> map = new HashMap<>(); // current index of character
// try to extend the range [i, j]
for (int j = 0, i = 0; j < n; j++) {
if (map.containsKey(s.charAt(j))) {
i = Math.max(map.get(s.charAt(j)), i);
}
ans = Math.max(ans, j - i + 1);
map.put(s.charAt(j), j + 1);
}
return ans;
}
Performance: 66ms
4. Median of Two Sorted Arrays
πΆπΆπΆ
There are two sorted arrays nums1 and nums2 of size m and n respectively.
Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
You may assume nums1 and nums2 cannot be both empty.
Example 1:1
2
3
4nums1 = [1, 3]
nums2 = [2]
The median is 2.0
Example 2:1
2
3
4nums1 = [1, 2]
nums2 = [3, 4]
The median is (2 + 3)/2 = 2.5
1 | class Solution { |