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.

0 commit comments

Comments
 (0)