Skip to content

Instantly share code, notes, and snippets.

@Cesarcmv
Cesarcmv / Solution.java
Created July 23, 2020 21:42
Single Number III
class Solution {
public int[] singleNumber(int[] nums) {
Set<Integer> set = new HashSet<>();
for(Integer i: nums){
if(!set.add(i)){
set.remove(i);
}
}
int j = 0;
@Cesarcmv
Cesarcmv / Solution.java
Created July 23, 2020 01:46
Binary Tree Zigzag Level Order Traversal Solution
class Solution {
public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
List<List<Integer>> finalList = new ArrayList();
if(root == null){
return finalList;
}
List<TreeNode> list = new ArrayList<>();
@Cesarcmv
Cesarcmv / Solution.java
Created July 22, 2020 22:35
Remove Linked List Elements
class Solution {
public ListNode removeElements(ListNode head, int val) {
if (head != null) {
ListNode prev = null;
ListNode node = head;
while (node != null) {
if (node.val == val) {
if (prev == null) {
head = node.next;
} else {
@Cesarcmv
Cesarcmv / Solution.java
Last active July 22, 2020 22:27
Top K Frequent Elements
class Solution {
public int[] topKFrequent(int[] nums, int k) {
if (k == nums.length) {
return nums;
}
Map<Integer, Integer> count = new HashMap();
for (int n: nums) {
class Solution {
public double myPow(double x, int n) {
double ans = 1;
if(n<0){
x = 1/x;
}
while(n!=0){
if(n%2!=0){
ans *= x;
@Cesarcmv
Cesarcmv / Solution.java
Created July 16, 2020 04:04
Reverse String
class Solution {
public String reverseWords(String s) {
List<String> list = Arrays.asList(s.split(" "))
.stream()
.filter(s1 -> !s1.isEmpty())
.collect(Collectors.toList());
if (list.size() == 0) {
return s.trim();
}
@Cesarcmv
Cesarcmv / Solution.java
Created July 14, 2020 01:36
Reverse Bits
public class Solution {
// you need treat n as an unsigned value
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;
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> result = new ArrayList();
result.add(new ArrayList<>());
for (int num : nums) {
List<List<Integer>> subList = new ArrayList();
for (List<Integer> current : result) {
List list = new ArrayList<>(current);
list.add(num);
subList.add(list);
}
public List<List<Integer>> threeSum(int[] nums) {
Map<Integer, Set<Integer>> map = new HashMap();
List<List<Integer>> threeSumList = new ArrayList();
Set<Map<Integer, Integer>> threeSumSet = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
putOnMap(i, nums[i], map);
}
for (int j = 0; j < nums.length; j++) {
@Cesarcmv
Cesarcmv / Solution.java
Created July 8, 2020 03:25
Island Perimeter
class Solution {
public int islandPerimeter(int[][] grid) {
int p = 0;
for(int j=0; j<grid.length; j++){
for(int i=0; i<grid[0].length; i++){
if(grid[j][i] == 1){
p += dfs(j,i,grid);
}
}
}