Lexicographical Numbers : Beauty of DFS

Given an integer n, return 1 - n in lexicographical order. For example, given 13, return: [1,10,11,12,13,2,3,4,5,6,7,8,9]. Please optimize your algorithm to use less time and space. The input size may be as large as 5,000,000. See the beauty of DFS Solution import java.util.*; public class Main { public static void main(String args[]) { // Your Code Here … Continue reading Lexicographical Numbers : Beauty of DFS

Path In Zigzag Labelled Binary Tree

In an infinite binary tree where every node has two children, the nodes are labelled in row order. In the odd numbered rows (ie., the first, third, fifth,...), the labelling is left to right, while in the even numbered rows (second, fourth, sixth,...), the labelling is right to left. Given the label of a node in this … Continue reading Path In Zigzag Labelled Binary Tree

Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. Example: Input: [   1->4->5,   1->3->4,   2->6 ] Output: 1->1->2->3->4->4->5->6 Solution Java Program Implementation /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { … Continue reading Merge k Sorted Lists

Binary Search Tree : Insertion

You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the updated binary tree. You just have to complete the function. Input Format You are given a … Continue reading Binary Search Tree : Insertion

Find the Missing Number in an Array

In this article, I explained about how to find the missing number in an array. Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. Example 1: Input: [3,0,1] Output: 2 Example 2: Input: [9,6,4,2,3,5,7,0,1] Output: 8 Note:Your algorithm should run in linear run time … Continue reading Find the Missing Number in an Array

How to Reverse Bits – Bit Manipulation

Reversing bits is one of the popular problem. How to reverse bits? In this article, we going to see about how to reverse bits in an efficient way. Reversing bits tests out understanding about bits. One of the top leetcode problem in bits section. Let's see our given problem, Reverse bits of a given 32 … Continue reading How to Reverse Bits – Bit Manipulation

Reverse an Integer Digits

In this article, we going to see about How to Reverse an Integer?  Given a 32-bit signed integer, reverse digits of an integer. Example 1: Input: 123 Output: 321 Example 2: Input: -123 Output: -321 Example 3: Input: 120 Output: 21 Note:Assume we are dealing with an environment which could only store integers within the … Continue reading Reverse an Integer Digits

Valid Number

This article explains about Leetcode's Valid Number Explanation and Solution. Validate if a given string can be interpreted as a decimal number. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true" -90e3   " => true" 1e" => false"e3" => false" 6e-1" => true" 99e2.5 " => false"53.5e93" => true" --6 " => false"-+3" => false"95a54e53" => false Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here … Continue reading Valid Number