Skip to content

Instantly share code, notes, and snippets.

@Javibot
Javibot / LRUCache.java
Created April 26, 2020 04:59
LRU Cache
class LRUCache {
public class ListNode {
int val;
int key;
ListNode next;
ListNode prev;
ListNode(int key, int val) {
this.val = val;
@Javibot
Javibot / ConstructBinarySearchTreeFromPreorderTraversal.java
Created April 26, 2020 04:57
Construct Binary Search Tree from Preorder Traversal
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
@Javibot
Javibot / NumberOfIslands.java
Created April 26, 2020 04:53
Number of Islands
class Solution {
public int numIslands(char[][] grid) {
int c = 0;
for (int j = 0; j < grid.length; j++) {
for (int i = 0; i < grid[j].length; i++) {
if (grid[j][i] == '1') {
c++;
findPath(grid, j, i);
}
}
@Javibot
Javibot / DiameterOfABinaryTree.java
Created April 26, 2020 04:51
Diameter of Binary Tree
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
@Javibot
Javibot / GroupOfAnagrams.java
Created April 26, 2020 04:50
Group of anagrams
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> groups = new HashMap<>();
for (int i = 0; i < strs.length; i++) {
char[] word = strs[i].toCharArray();
Arrays.sort(word);
String newWord = String.valueOf(word);
if (groups.containsKey(newWord)){
@Javibot
Javibot / CountingElements.java
Created April 26, 2020 04:48
Counting Elements
class Solution {
public int countElements(int[] arr) {
if (arr == null) {
return 0;
}
Set<Integer> uniqueElements = new HashSet<>();
for (int element : arr) {
@Javibot
Javibot / BestTimeToBuyAndSellStockII.java
Created April 26, 2020 04:41
Best time to buy and sell stock II
class Solution {
public int maxProfit(int[] prices) {
int maxProfit = 0;
int low = 0;
int max = 0;
for (int i = 1; i < prices.length; i++) {
if (max == 0 && prices[i - 1] < prices[i]) {
low = prices[i - 1];
}
if (low >= 0 && prices[i - 1] < prices[i]) {
@Javibot
Javibot / MoveZeros.java
Created April 26, 2020 04:39
Move zeros
class Solution {
public void moveZeroes(int[] nums) {
int y = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[y] = nums[i];
y++;
}
}
@Javibot
Javibot / HappyNumber.java
Created April 26, 2020 04:38
Happy Number
class Solution {
public boolean isHappy(int n) {
Set<Integer> set = new HashSet<>();
while (true) {
int sum = 0;
while (n > 0) {
sum = sum + (n % 10) * (n % 10);
n = n / 10;
}
@Javibot
Javibot / SingleNumber.java
Created April 26, 2020 04:36
Single Number
class Solution {
public int singleNumber(int[] nums) {
if (nums == null || nums.length == 0) return 0;
int single = 0;
for (int i = 0; i < nums.length; i++) single ^= nums[i];
return single;