Category: Java programming question
-
LeetCode Problem 50: Pow(x, n)
Problem Statement Implement the function myPow(x, n) which calculates x raised to the power n (i.e., xⁿ). You must not use built-in library functions like Math.pow() and must handle both positive and negative exponents efficiently. Examples Approach: Fast Exponentiation (Binary Exponentiation) Intuition The naive approach of multiplying x by itself n times takes O(n) time…
-
Problem: Reverse Words in a String (LeetCode 151)
Problem Statement Given an input string s, reverse the order of the words.A word is defined as a sequence of non-space characters.The returned string should have exactly one space separating the words, and no leading or trailing spaces. Example 1:Input: Output: Example 2:Input: Output: Example 3:Input: Output: Approach 1: Split and Reverse This is the…
-
LeetCode Problem 1344: Angle Between Hands of a Clock
Problem Statement Given two integers hour and minutes, return the smaller angle (in degrees) between the hour hand and the minute hand of a clock. Example 1: Example 2: Example 3: Constraints: Intuition A clock has two hands — hour and minute — each rotating at different speeds. We must find the smaller angle between…
-
LeetCode Problem 100: Same Tree
Problem Statement Given the roots of two binary trees, p and q, write a function to check if they are the same tree. Two binary trees are considered the same if: Example 1: Example 2: Example 3: Intuition We need to verify two conditions simultaneously at every node: This is a classic recursive comparison problem…
-
LeetCode Problem 190: Reverse Bits
Problem Statement Reverse the bits of a given 32-bit unsigned integer. Example 1: Example 2: Constraints: Intuition The task is to reverse the binary digits of a 32-bit number. For example: If the input binary is: then the output should be: That means we need to: Step-by-Step Approach This way, bits from the right of…
-
LeetCode Problem 5: Longest Palindromic Substring
Problem Statement Given a string s, return the longest palindromic substring in s. A palindrome is a string that reads the same forward and backward. Example 1: Example 2: Constraints: Intuition A palindrome has a symmetric property: A substring is a palindrome if it reads the same when reversed. For example: We want the longest…
-
LeetCode Problem 78: Subsets
Problem Statement Given an integer array nums of unique elements, return all possible subsets (the power set). The solution set must not contain duplicate subsets, and the order of subsets does not matter. Example: Intuition We are asked to generate all possible combinations of elements from the array — that is, the power set. If…
-
LeetCode Problem 430: Flatten a Multilevel Doubly Linked List
Problem Statement You are given a doubly linked list where in addition to the next and prev pointers, each node also has a child pointer that may point to a separate doubly linked list. These child lists may have one or more levels of children themselves. Your task is to flatten the list so that…
-
LeetCode Problem 662: Maximum Width of Binary Tree
Problem Statement Given the root of a binary tree, return the maximum width of the given tree. The width of a level** is defined as the length between the leftmost and rightmost non-null nodes, considering the null nodes between them in the complete binary tree structure. If the input tree is empty, return 0. Example:…
-
LeetCode Problem 15: 3Sum
Problem Statement Given an integer array nums, return all unique triplets [nums[i], nums[j], nums[k]] such that: The solution set must not contain duplicate triplets. Example: Intuition We need to find all unique triplets that sum up to zero.A brute-force approach would try all possible triplets (O(n³)), but we can do much better using sorting and…
