Skip to content

Commit 4e709bd

Browse files
committed
updated leetcode solutions
1 parent 8f456a8 commit 4e709bd

File tree

5 files changed

+129
-3
lines changed

5 files changed

+129
-3
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
- [Time conversion](https://www.hackerrank.com/challenges/time-conversion/problem?isFullScreen=true) | [Solution](hackerrank/algorithms/warmup/time_conversion.py)
99
- Implementation
1010
- [Breaking best and worst records](https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem) | [Solution](hackerrank/algorithms/implementation/breaking_best_and_worst_records.py)
11+
1112
- Data structures
1213
- Linked lists
1314
[Print the elements of a linked list](https://www.hackerrank.com/challenges/print-the-elements-of-a-linked-list/problem) | [Solution](hackerrank/data_structures/linked_list/print_linked_list_elements.py)
@@ -59,12 +60,17 @@
5960

6061

6162

63+
- Java
64+
| Problem | Solution |
65+
|---|---|
66+
|[Java stdin and stdout I](https://www.hackerrank.com/challenges/java-stdin-and-stdout-1)|[Solution](hackerrank\java\introduction\java_stdin_stdout_1.java)|
67+
|[Java If Else](https://www.hackerrank.com/challenges/java-if-else)|[Solution](hackerrank\java\introduction\java_if_else.java)|
6268
- Leetcode
6369
- Arrays
6470
| Problem | Python | CPP | Java |
6571
|---|---|---|---|
6672
|[Remove duplicates from Sorted Array](https://leetcode.com/problems/remove-duplicates-from-sorted-array/)|[Solution](leetcode/arrays/python/RemoveDuplicates.py)| | |
67-
- [First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Solution](leetcode/arrays/python/first-unique-character-in-a-string.py)| | |
68-
- [Remove Element](https://leetcode.com/problems/remove-element/) | [Solution](leetcode/arrays/python/remove-element.py)| | |
73+
|[First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/) | [Solution](leetcode/arrays/python/first-unique-character-in-a-string.py)| | |
74+
|[Remove Element](https://leetcode.com/problems/remove-element/) | [Solution](leetcode/arrays/python/remove-element.py)| | |
6975
- Math
70-
- [Palindrome Number](https://leetcode.com/problems/palindrome-number/python/description/) | [Solution](leetcode/math/python/palindrome.py)| | |
76+
|[Palindrome Number](https://leetcode.com/problems/palindrome-number/python/description/) | [Solution](leetcode/math/python/palindrome.py)| | |
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.io.*;
2+
import java.math.*;
3+
import java.security.*;
4+
import java.text.*;
5+
import java.util.*;
6+
import java.util.concurrent.*;
7+
import java.util.regex.*;
8+
9+
public class Solution {
10+
11+
12+
13+
private static final Scanner scanner = new Scanner(System.in);
14+
15+
public static void main(String[] args) {
16+
int N = scanner.nextInt();
17+
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
18+
19+
if (N % 2 != 0)
20+
{
21+
System.out.println("Weird");
22+
}
23+
else {
24+
if (N >= 2 && N <= 5)
25+
{
26+
System.out.println("Not Weird");
27+
}
28+
else if (N >= 6 && N <= 20)
29+
{
30+
System.out.println("Weird");
31+
}
32+
else if (N > 20)
33+
{
34+
System.out.println("Not Weird");
35+
}
36+
}
37+
38+
39+
40+
scanner.close();
41+
}
42+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
5+
public static void main(String[] args) {
6+
Scanner scan = new Scanner(System.in);
7+
int a = scan.nextInt();
8+
int b = scan.nextInt();
9+
int c = scan.nextInt();
10+
11+
System.out.println(a);
12+
System.out.println(b);
13+
System.out.println(c);
14+
}
15+
}

leetcode/arrays/java/two-sum.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Solution 1: Brute Force
2+
class Solution {
3+
public int[] twoSum(int[] nums, int target) {
4+
int n = nums.length;
5+
for(int i = 0; i < n-1; i++)
6+
{
7+
for(int j = i + 1; j < n; j++)
8+
{
9+
if(nums[i] + nums[j] == target)
10+
{
11+
return new int[]{i,j};
12+
}
13+
}
14+
}
15+
return new int[]{};
16+
}
17+
}
18+
19+
// Solution 2: Hash Table
20+
class Solution {
21+
public int[] twoSum(int[] nums, int target) {
22+
Map<Integer,Integer> index_mapping = new HashMap<>();
23+
int list_len = nums.length;
24+
25+
// Map element with its index in hash table
26+
for(int i = 0; i < list_len; i++)
27+
{
28+
index_mapping.put(nums[i],i);
29+
}
30+
31+
for(int i = 0; i < list_len-1; i++)
32+
{
33+
int second_element = target - nums[i];
34+
if(index_mapping.containsKey(second_element) && index_mapping.get(second_element) != i)
35+
{
36+
return new int[]{i, index_mapping.get(second_element)};
37+
}
38+
}
39+
return new int[]{};
40+
}
41+
}

leetcode/arrays/python/two-sum.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Solution 1: Brute Force
2+
class Solution:
3+
def twoSum(self, nums: List[int], target: int) -> List[int]:
4+
for i in range(0,len(nums)-1):
5+
for j in range(i+1,len(nums)):
6+
if nums[i] + nums[j] == target:
7+
return [i,j]
8+
9+
# Solution 2: Hash Table
10+
class Solution:
11+
def twoSum(self, nums: List[int], target: int) -> List[int]:
12+
index_mapping = {}
13+
list_len = len(nums)
14+
# Map element with its index in hash table
15+
for i in range(list_len):
16+
index_mapping[nums[i]] = i
17+
18+
for i in range(list_len):
19+
second_element = target - nums[i]
20+
if second_element in index_mapping and index_mapping[second_element] != i:
21+
return [i,index_mapping[second_element]]
22+
return []

0 commit comments

Comments
 (0)