Skip to content

Commit a90f746

Browse files
committed
Initial commit
0 parents  commit a90f746

File tree

10 files changed

+297
-0
lines changed

10 files changed

+297
-0
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto

.gitignore

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
##############################
2+
## Java
3+
##############################
4+
.mtj.tmp/
5+
*.class
6+
*.jar
7+
*.war
8+
*.ear
9+
*.nar
10+
hs_err_pid*
11+
12+
##############################
13+
## Maven
14+
##############################
15+
target/
16+
pom.xml.tag
17+
pom.xml.releaseBackup
18+
pom.xml.versionsBackup
19+
pom.xml.next
20+
pom.xml.bak
21+
release.properties
22+
dependency-reduced-pom.xml
23+
buildNumber.properties
24+
.mvn/timing.properties
25+
.mvn/wrapper/maven-wrapper.jar
26+
27+
##############################
28+
## Gradle
29+
##############################
30+
bin/
31+
build/
32+
.gradle
33+
.gradletasknamecache
34+
gradle-app.setting
35+
!gradle-wrapper.jar
36+
37+
##############################
38+
## IntelliJ
39+
##############################
40+
out/
41+
.idea/
42+
.idea_modules/
43+
*.iml
44+
*.ipr
45+
*.iws
46+
47+
##############################
48+
## Eclipse
49+
##############################
50+
.settings/
51+
tmp/
52+
.metadata
53+
.classpath
54+
.project
55+
*.tmp
56+
*.bak
57+
*.swp
58+
*~.nib
59+
local.properties
60+
.loadpath
61+
.factorypath
62+
63+
##############################
64+
## NetBeans
65+
##############################
66+
nbproject/private/
67+
nbbuild/
68+
dist/
69+
nbdist/
70+
nbactions.xml
71+
nb-configuration.xml
72+
73+
##############################
74+
## Visual Studio Code
75+
##############################
76+
.vscode/
77+
.code-workspace
78+
79+
##############################
80+
## OS X
81+
##############################
82+
.DS_Store

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# leetcode-solutions-java
2+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.andrewbayd;
2+
3+
import java.util.HashMap;
4+
5+
public class FirstUniqueCharInAString {
6+
7+
/*
8+
Given a string s, find the first non-repeating character in it and return its index.
9+
If it does not exist, return -1.
10+
The full description: https://leetcode.com/problems/first-unique-character-in-a-string/
11+
*/
12+
13+
public static int firstUniqChar(String s) {
14+
HashMap<Character, Integer> letters = new HashMap<>();
15+
for (int i = 0; i < s.length(); i++) {
16+
char c = s.charAt(i);
17+
letters.put(c, letters.getOrDefault(c, 0) + 1);
18+
}
19+
for (int i = 0; i < s.length(); i++) {
20+
if (letters.get(s.charAt(i)) == 1) {
21+
return i;
22+
}
23+
}
24+
return -1;
25+
}
26+
27+
public static void main(String[] args) {
28+
assert firstUniqChar("leetcode") == 0;
29+
}
30+
}

src/com/andrewbayd/FizzBuzz.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.andrewbayd;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class FizzBuzz {
7+
8+
/*
9+
Classic FizzBuzz: https://leetcode.com/problems/fizz-buzz/
10+
*/
11+
12+
public static List<String> fizzBuzz(int n) {
13+
List<String> result = new ArrayList<>();
14+
for (int i = 1; i <= n; i++) {
15+
if (i % 3 == 0 && i % 5 == 0) result.add("FizzBuzz");
16+
else if (i % 3 == 0) result.add("Fizz");
17+
else if (i % 5 == 0) result.add("Buzz");
18+
else result.add("" + i);
19+
}
20+
return result;
21+
}
22+
23+
public static void main(String[] args) {
24+
System.out.println(fizzBuzz(3)); //-> [1, 2, Fizz]
25+
System.out.println(fizzBuzz(15)); //-> [1, 2, Fizz, 4, Buzz, Fizz, 7, 8, Fizz, Buzz, 11, Fizz, 13, 14, FizzBuzz]
26+
}
27+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.andrewbayd;
2+
3+
import java.util.HashMap;
4+
5+
public class RomanToInteger {
6+
7+
/*
8+
Convert given roman numeral into integer.
9+
The full description: https://leetcode.com/problems/roman-to-integer/
10+
*/
11+
12+
public static int romanToInt(String s) {
13+
14+
HashMap<Character, Integer> roman = new HashMap<>();
15+
roman.put('I', 1);
16+
roman.put('V', 5);
17+
roman.put('X', 10);
18+
roman.put('L', 50);
19+
roman.put('C', 100);
20+
roman.put('D', 500);
21+
roman.put('M', 1000);
22+
23+
int n = s.length();
24+
int result = 0;
25+
26+
for (int i = 0; i < n; i++) {
27+
char c = s.charAt(i);
28+
if ((i != n - 1) && (roman.get(c) < roman.get(s.charAt(i + 1)))) {
29+
result -= roman.get(c);
30+
} else {
31+
result += roman.get(c);
32+
}
33+
}
34+
35+
return result;
36+
}
37+
38+
public static void main(String[] args) {
39+
assert romanToInt("III") == 3;
40+
assert romanToInt("LVIII") == 58;
41+
assert romanToInt("MCMXCIV") == 1994;
42+
}
43+
}

src/com/andrewbayd/Sqrt.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.andrewbayd;
2+
3+
public class Sqrt {
4+
5+
/*
6+
Given a non-negative integer x, compute and return the square root of x.
7+
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
8+
Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.
9+
The full description: https://leetcode.com/problems/sqrtx/
10+
*/
11+
12+
public static int mySqrt(int x) {
13+
if (x == 0) return 0;
14+
if (x < 4) return 1;
15+
16+
int start = 0;
17+
int end = x;
18+
int res = 2;
19+
20+
while (start < end) {
21+
long mid = (start + end) / 2;
22+
if ((mid*mid <= x) && ((mid+1) * (mid+1) > x)) {
23+
return (int)mid;
24+
} else if (mid*mid > x) {
25+
end = (int)mid;
26+
} else {
27+
start = (int)mid;
28+
}
29+
}
30+
return res;
31+
}
32+
33+
public static void main(String[] args) {
34+
assert mySqrt(0) == 0;
35+
assert mySqrt(3) == 1;
36+
assert mySqrt(8) == 2;
37+
assert mySqrt(2147395599) == 46339;
38+
}
39+
}

src/com/andrewbayd/TwoSum.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.andrewbayd;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
6+
public class TwoSum {
7+
8+
/*
9+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
10+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
11+
You can return the answer in any order.
12+
The full description: https://leetcode.com/problems/two-sum/
13+
*/
14+
15+
public static int[] twoSum(int[] nums, int target) {
16+
int[] result = new int[2];
17+
HashMap<Integer, Integer> passed = new HashMap<>();
18+
19+
for (int i = 0; i < nums.length; i++){
20+
if (passed.containsKey(target - nums[i])) {
21+
result[0] = passed.get(target - nums[i]);
22+
result[1] = i;
23+
break;
24+
}
25+
passed.put(nums[i], i);
26+
}
27+
28+
return result;
29+
}
30+
31+
public static void main(String[] args) {
32+
int[] arr = {2, 11, 7, 15};
33+
System.out.println(Arrays.toString(twoSum(arr, 9))); //-> [0, 2]
34+
}
35+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.andrewbayd.mergeTwoSortedLists;
2+
3+
public class ListNode {
4+
int val;
5+
ListNode next;
6+
ListNode() {}
7+
ListNode(int val) { this.val = val; }
8+
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.andrewbayd.mergeTwoSortedLists;
2+
3+
public class MergeTwoSortedLists {
4+
public static ListNode mergeTwoLists(ListNode list1, ListNode list2) {
5+
if (list1 == null) return list2;
6+
if (list2 == null) return list1;
7+
8+
ListNode merged = new ListNode();
9+
ListNode result = merged;
10+
11+
while (list1 != null && list2 != null) {
12+
if (list1.val < list2.val) {
13+
merged.next = new ListNode(list1.val);
14+
list1 = list1.next;
15+
} else {
16+
merged.next = new ListNode(list2.val);
17+
list2 = list2.next;
18+
}
19+
merged = merged.next;
20+
}
21+
22+
if (list1 != null) merged.next = new ListNode(list1.val, list1.next);
23+
if (list2 != null) merged.next = new ListNode(list2.val, list2.next);
24+
25+
26+
return result.next;
27+
}
28+
}

0 commit comments

Comments
 (0)