Our very own Let’s Code_ community initiative is now having a Patreon Landing page! Just another way for you to say thanks!
Go:
* SDE Career Contemplations * DSA Practice * Problem Solving in Java * Tech in General * Open Source guidance * Career guidance
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode() {} * TreeNode(int val) { this.val = val; } * TreeNode(int val, TreeNode left, TreeNode right) { * this.val = val; * this.left = left; * this.right = right; * } * } */ class Solution { public List<List> zigzagLevelOrder(TreeNode root) { //BFS List<List> result = new LinkedList<>(); if (root == null) return result; Queue q = new LinkedList<>(); q.add(root); while (!q.isEmpty()) { int nextLevelSize = q.size(); LinkedList level = new LinkedList<>(); for (int i=0; i<nextLevelSize; i++) { TreeNode current = q.remove(); if (result.size()%2 == 0) // level is an odd level to be added level.add(current.val); else //even-level has elements added in reverse level.addFirst(current.val); if (current.left != null) q.add(current.left); if (current.right != null) q.add(current.right); } result.add(level); } return result; } }
class Solution { public: int removeDuplicates(vector& nums) { int len = nums.size(); if (len<=1) return len; int i, top; for (i=1, top=1; i<len; i++) { if (nums[i] != nums[top-1]) nums[top++] = nums[i]; } return top; } };
class Solution { public int removeDuplicates(int[] nums) { int reducedLen = -1; if (nums.length == 0) return reducedLen+1; int pos = 0; while (pos < nums.length) { pos = getLastOccurenceIdx(nums, pos); nums[++reducedLen] = nums[pos]; pos++; } return reducedLen+1; } private int getLastOccurenceIdx(int[] nums, int firstPos) { int min = firstPos; int max = nums.length-1; while (min < max) { int mid = min + (max-min+1)/2; if (nums[mid]>nums[firstPos]) { max = mid-1; } else { min = mid; } } return min; } }
class Solution { public List> threeSum(int[] nums) { Arrays.sort(nums); List
> result = new LinkedList<>(); if (nums.length < 3) return result; for(int i=0; i < nums.length-2; i++) { // if(nums[i+1] == nums[i]) // continue; //bypass duplicate elements for first triplet // only first of repeated elements should be continued, to allow for repetition in triplet elements if(i==0 || (i>0 && (nums[i] != nums[i-1]))) { int sum = 0-nums[i]; int low = i+1; int high = nums.length-1; //Do 2Sum on following subarray while (low < high) { if (nums[low] + nums[high] == sum) { //add validated triplet result.add(Arrays.asList(nums[i], nums[low], nums[high])); //skip duplicate occurrences of the above valid pair of elements our triplet while(low < high && nums[low+1] == nums[low]) low++; while(low < high && nums[high-1] == nums[high]) high--; low++; high--; } else if ((nums[low] + nums[high]) > sum) { high--; } else { low++; } } } } return result; } }
class Solution { public int[] twoSum(int[] nums, int target) { Map<Integer, Integer> hm = new HashMap<>(); for (int i=0; i<nums.length; i++) { if (hm.get(target - nums[i]) != null) { return (new int[]{hm.get(target - nums[i]), i}); } hm.put(nums[i], i); } //never occours for appropriate inputs return new int[2]; } }
This is one Hard category problem on LeetCode.
A few things to observe in this problem are :
class Solution { public String makeLargestSpecial(String S) { ArrayList res = new ArrayList<>(); int count = 0, st = 0; for (int i=0; i<S.length(); i++) { if (S.charAt(i) == '1') count++; else count--; if (count == 0) { res.add('1' + makeLargestSpecial(S.substring(st+1, i)) + '0'); st = i+1; } } Collections.sort(res, Collections.reverseOrder()); return String.join("", res); } }
Problem Name: Palindrome Partitioning II Problem Description : https://www.interviewbit.com/problems/palindrome-partitioning-ii/ Problem Ap...