Skip to content

Commit 10347c6

Browse files
Kevin Naughton JrKevin Naughton Jr
authored andcommitted
add more problem solutions
1 parent 4e65c9a commit 10347c6

18 files changed

+396
-0
lines changed

.DS_Store

-2 KB
Binary file not shown.

company/adobe/AddDigits.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
2+
3+
//For example:
4+
//Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
5+
6+
//Follow up:
7+
//Could you do it without any loop/recursion in O(1) runtime?
8+
9+
class AddDigits {
10+
public int addDigits(int num) {
11+
while(num >= 10) {
12+
int temp = 0;
13+
while(num > 0) {
14+
temp += num % 10;
15+
num /= 10;
16+
}
17+
num = temp;
18+
}
19+
20+
return num;
21+
}
22+
}
23+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Given an array of integers and an integer k, find out whether there are two distinct indices i and
2+
//j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
3+
4+
class ContainsDuplicatesII {
5+
public boolean containsNearbyDuplicate(int[] nums, int k) {
6+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
7+
for(int i = 0; i < nums.length; i++) {
8+
int current = nums[i];
9+
if(map.containsKey(current) && i - map.get(current) <= k) {
10+
return true;
11+
} else {
12+
map.put(current, i);
13+
}
14+
}
15+
16+
return false;
17+
}
18+
}
19+

company/amazon/LinkedListCycle.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Given a linked list, determine if it has a cycle in it.
2+
//Follow up:
3+
//Can you solve it without using extra space?
4+
/**
5+
* Definition for singly-linked list.
6+
* class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode(int x) {
10+
* val = x;
11+
* next = null;
12+
* }
13+
* }
14+
*/
15+
public class Solution {
16+
public boolean hasCycle(ListNode head) {
17+
if(head == null || head.next == null) {
18+
return false;
19+
}
20+
21+
ListNode slow = head;
22+
ListNode fast = head.next;
23+
while(fast != null && fast.next != null && fast != slow) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
}
27+
28+
return fast == slow;
29+
}
30+
}
31+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Given a linked list, determine if it has a cycle in it.
2+
//Follow up:
3+
//Can you solve it without using extra space?
4+
/**
5+
* Definition for singly-linked list.
6+
* class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode(int x) {
10+
* val = x;
11+
* next = null;
12+
* }
13+
* }
14+
*/
15+
public class Solution {
16+
public boolean hasCycle(ListNode head) {
17+
if(head == null || head.next == null) {
18+
return false;
19+
}
20+
21+
ListNode slow = head;
22+
ListNode fast = head.next;
23+
while(fast != null && fast.next != null && fast != slow) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
}
27+
28+
return fast == slow;
29+
}
30+
}
31+

company/microsoft/AddDigits.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
2+
3+
//For example:
4+
//Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
5+
6+
//Follow up:
7+
//Could you do it without any loop/recursion in O(1) runtime?
8+
9+
class AddDigits {
10+
public int addDigits(int num) {
11+
while(num >= 10) {
12+
int temp = 0;
13+
while(num > 0) {
14+
temp += num % 10;
15+
num /= 10;
16+
}
17+
num = temp;
18+
}
19+
20+
return num;
21+
}
22+
}
23+
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Given a linked list, determine if it has a cycle in it.
2+
//Follow up:
3+
//Can you solve it without using extra space?
4+
/**
5+
* Definition for singly-linked list.
6+
* class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode(int x) {
10+
* val = x;
11+
* next = null;
12+
* }
13+
* }
14+
*/
15+
public class Solution {
16+
public boolean hasCycle(ListNode head) {
17+
if(head == null || head.next == null) {
18+
return false;
19+
}
20+
21+
ListNode slow = head;
22+
ListNode fast = head.next;
23+
while(fast != null && fast.next != null && fast != slow) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
}
27+
28+
return fast == slow;
29+
}
30+
}
31+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Given an array of integers and an integer k, find out whether there are two distinct indices i and
2+
//j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
3+
4+
class ContainsDuplicatesII {
5+
public boolean containsNearbyDuplicate(int[] nums, int k) {
6+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
7+
for(int i = 0; i < nums.length; i++) {
8+
int current = nums[i];
9+
if(map.containsKey(current) && i - map.get(current) <= k) {
10+
return true;
11+
} else {
12+
map.put(current, i);
13+
}
14+
}
15+
16+
return false;
17+
}
18+
}
19+

company/yahoo/LinkedListCycle.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Given a linked list, determine if it has a cycle in it.
2+
//Follow up:
3+
//Can you solve it without using extra space?
4+
/**
5+
* Definition for singly-linked list.
6+
* class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode(int x) {
10+
* val = x;
11+
* next = null;
12+
* }
13+
* }
14+
*/
15+
public class Solution {
16+
public boolean hasCycle(ListNode head) {
17+
if(head == null || head.next == null) {
18+
return false;
19+
}
20+
21+
ListNode slow = head;
22+
ListNode fast = head.next;
23+
while(fast != null && fast.next != null && fast != slow) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
}
27+
28+
return fast == slow;
29+
}
30+
}
31+

leetcode/.DS_Store

-6 KB
Binary file not shown.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//Given an array of integers and an integer k, find out whether there are two distinct indices i and
2+
//j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
3+
4+
class ContainsDuplicatesII {
5+
public boolean containsNearbyDuplicate(int[] nums, int k) {
6+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
7+
for(int i = 0; i < nums.length; i++) {
8+
int current = nums[i];
9+
if(map.containsKey(current) && i - map.get(current) <= k) {
10+
return true;
11+
} else {
12+
map.put(current, i);
13+
}
14+
}
15+
16+
return false;
17+
}
18+
}

leetcode/array/RemoveElement.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//Given an array and a value, remove all instances of that value in-place and return the new length.
2+
//Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
3+
//The order of elements can be changed. It doesn't matter what you leave beyond the new length.
4+
5+
//Example:
6+
//Given nums = [3,2,2,3], val = 3,
7+
//Your function should return length = 2, with the first two elements of nums being 2.
8+
9+
class RemoveElement {
10+
public int removeElement(int[] nums, int val) {
11+
int index = 0;
12+
for(int i = 0; i < nums.length; i++) {
13+
if(nums[i] != val) {
14+
nums[index++] = nums[i];
15+
}
16+
}
17+
18+
return index;
19+
}
20+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//Given an array of integers and an integer k, find out whether there are two distinct indices i and
2+
//j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
3+
4+
class ContainsDuplicatesII {
5+
public boolean containsNearbyDuplicate(int[] nums, int k) {
6+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
7+
for(int i = 0; i < nums.length; i++) {
8+
int current = nums[i];
9+
if(map.containsKey(current) && i - map.get(current) <= k) {
10+
return true;
11+
} else {
12+
map.put(current, i);
13+
}
14+
}
15+
16+
return false;
17+
}
18+
}
19+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//Given an array of integers, every element appears three times except for one,
2+
//which appears exactly once. Find that single one.
3+
4+
//Note:
5+
//Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
6+
7+
class SingleNumberII {
8+
public int singleNumber(int[] nums) {
9+
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
10+
for(int i: nums) {
11+
if(map.containsKey(i)) {
12+
map.put(i, map.get(i) + 1);
13+
} else {
14+
map.put(i, 1);
15+
}
16+
}
17+
18+
for(int key: map.keySet()) {
19+
if(map.get(key) == 1) {
20+
return key;
21+
}
22+
}
23+
24+
//no unique integer in nums
25+
return -1;
26+
}
27+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//Given a linked list, determine if it has a cycle in it.
2+
//Follow up:
3+
//Can you solve it without using extra space?
4+
/**
5+
* Definition for singly-linked list.
6+
* class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode(int x) {
10+
* val = x;
11+
* next = null;
12+
* }
13+
* }
14+
*/
15+
public class Solution {
16+
public boolean hasCycle(ListNode head) {
17+
if(head == null || head.next == null) {
18+
return false;
19+
}
20+
21+
ListNode slow = head;
22+
ListNode fast = head.next;
23+
while(fast != null && fast.next != null && fast != slow) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
}
27+
28+
return fast == slow;
29+
}
30+
}
31+

leetcode/math/AddDigits.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
2+
3+
//For example:
4+
//Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
5+
6+
//Follow up:
7+
//Could you do it without any loop/recursion in O(1) runtime?
8+
9+
class AddDigits {
10+
public int addDigits(int num) {
11+
while(num >= 10) {
12+
int temp = 0;
13+
while(num > 0) {
14+
temp += num % 10;
15+
num /= 10;
16+
}
17+
num = temp;
18+
}
19+
20+
return num;
21+
}
22+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//Given a linked list, determine if it has a cycle in it.
2+
//Follow up:
3+
//Can you solve it without using extra space?
4+
/**
5+
* Definition for singly-linked list.
6+
* class ListNode {
7+
* int val;
8+
* ListNode next;
9+
* ListNode(int x) {
10+
* val = x;
11+
* next = null;
12+
* }
13+
* }
14+
*/
15+
public class Solution {
16+
public boolean hasCycle(ListNode head) {
17+
if(head == null || head.next == null) {
18+
return false;
19+
}
20+
21+
ListNode slow = head;
22+
ListNode fast = head.next;
23+
while(fast != null && fast.next != null && fast != slow) {
24+
slow = slow.next;
25+
fast = fast.next.next;
26+
}
27+
28+
return fast == slow;
29+
}
30+
}

0 commit comments

Comments
 (0)