Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
private static final int[] values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};
private static final String[] symbols = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};

public String intToRoman(int num) {
StringBuilder sb = new StringBuilder();
// Loop through each symbol, stopping if num becomes 0.
for (int i = 0; i < values.length && num > 0; i++) {
// Repeat while the current symbol still fits into num.
while (values[i] <= num) {
num -= values[i];
sb.append(symbols[i]);
}
}
return sb.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {

static Map<String, Integer> values = new HashMap<>();

static {
values.put("M", 1000);
values.put("D", 500);
values.put("C", 100);
values.put("L", 50);
values.put("X", 10);
values.put("V", 5);
values.put("I", 1);
}

public int romanToInt(String s) {

int sum = 0;
int i = 0;
while (i < s.length()) {
String currentSymbol = s.substring(i, i + 1);
int currentValue = values.get(currentSymbol);
int nextValue = 0;
if (i + 1 < s.length()) {
String nextSymbol = s.substring(i + 1, i + 2);
nextValue = values.get(nextSymbol);
}

if (currentValue < nextValue) {
sum += (nextValue - currentValue);
i += 2;
}
else {
sum += currentValue;
i += 1;
}

}
return sum;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public int singleNumber(int[] nums) {
HashMap<Integer, Integer> hash_table = new HashMap<>();
for (int i : nums) {
hash_table.put(i, hash_table.getOrDefault(i, 0) + 1);
}
for (int i : nums) {
if (hash_table.get(i) == 1) {
return i;
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//You must implement a solution with a/linear runtime/complexity and use only
//constant extra space.
//a⊕0=a ; a⊕a=0 ; a⊕b⊕a=(a⊕a)⊕b=0⊕b=b
class Solution {
public int singleNumber(int[] nums) {
int a = 0;
for (int i : nums) {
a ^= i;
}
return a;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//option 1 time limit exceeded
import java.math.BigInteger;
class Solution {
public int trailingZeroes(int n) {
int res = 0;
BigInteger Fac = BigInteger.ONE;
for(int i = 2; i <= n; i++){
Fac = Fac.multiply(BigInteger.valueOf(i));
}
while(Fac.mod(BigInteger.TEN).equals(BigInteger.ZERO)){
Fac = Fac.divide(BigInteger.TEN);
res += 1;
}
return res;
}
}
//option 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Solution {
public int reverseBits(int n) {
int ans = 0;
for (int i = 0; i < 32; i++) {
ans <<= 1;
ans = ans | (n & 1);
n >>= 1;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public int hammingWeight(int n) {
int bits = 0;
int mask = 1;
for (int i = 0; i < 32; i++) {
if ((n & mask) != 0) {
bits++;
}
mask <<= 1;
}
return bits;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class Solution {
public boolean containsDuplicate(int[] nums) {
Set<Integer> hashset = new HashSet<>(nums.length);
for (int x : nums){
if(hashset.contains(x)){
return true;
}
hashset.add(x);
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution {
public boolean containsNearbyDuplicate(int[] nums, int k) {
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; ++i) {
if (set.contains(nums[i])) return true;
set.add(nums[i]);
if (set.size() > k) {
set.remove(nums[i - k]);
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Solution {
public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) {
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < nums.length; ++i) {
// Find the successor of current element
Integer s = set.ceiling(nums[i]);
if (s != null && (long) s <= nums[i] + t) return true;

// Find the predecessor of current element
Integer g = set.floor(nums[i]);
if (g != null && nums[i] <= (long) g + t) return true;

set.add(nums[i]);
if (set.size() > k) {
set.remove(nums[i - k]);
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public boolean isPowerOfTwo(int n) {
if(n == 0){
return false;
}
if(n == 1){
return true;
}
if(n%2 != 0){
return false;
}
while(n%2 == 0){
n /= 2;
}
return n == 1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution {
public int mySqrt(int x) {
if (x < 2) return x;

int left = mySqrt(x >> 2) << 1;
int right = left + 1;
return (long)right * right > x ? left : right;
}
}