C++ / JAVA Optimised Solutions for LeetCode Problems

Overview

LeetCode Solutions

Algorithms

My Most Voted Solutions

# Title Explanation Existence Complexity Difficulty Tags
8 String to Integer (atoi) Detailed Explanation On 1st page of Most Voted Category Time - O(n)
Space - O(1)
Medium String
50 Pow(x, n) Detailed Explanation On 1st page of Most Voted Category Time - O(log2(n))
Space - O(1)
Medium Math, Recursion
190 Reverse Bits Detailed Explanation On 1st page of Most Voted Category Time - O(1)
Space - O(1)
Easy Divide and Conquer, Bit Manipuation
989 Add to Array-Form of Integer Detailed Explanation On 1st page of Most Voted Category Time - O(max(n, log10(k)))
Space - O(max(n, log10(k)))
Easy Array, Math
1470 Shuffle the Array Detailed Explanation On 1st page of Most Voted Category Time - O(n)
Space - O(1)
Easy Array
2129 Capitalize the Title Explained Solution On 1st page of Most Voted Category Time - O(n)
Space - O(1)
Easy String

Explained Solutions

# Title Explanation Complexity Difficulty Tags
8 String to Integer (atoi) Detailed Explanation Time - O(n)
Space - O(1)
Medium String
9 Palindrome Number Explained Solution Time - O(log10(n))
Space - O(1)
Easy Math
33 Search in Rotated Sorted Array C++ Explanation
JAVA Explanation
Time - O(log2(n))
Space - O(1)
Medium Array, Binary Search
50 Pow(x, n) Detailed Explanation Time - O(log2(n))
Space - O(1)
Medium Math, Recursion
189 Rotate Array C++ Explanation
JAVA Explanation
Time - O(n)
Space - O(1)
Medium Math, Array, Two Pointers
190 Reverse Bits Detailed Explanation Time - O(1)
Space - O(1)
Easy Divide and Conquer, Bit Manipuation
191 Number of 1 Bits Detailed Explanation Time - O(1)
Space - O(1)
Easy Bit Manipulation
1431 Kids With the Greatest Number of Candies Commented Solution Time - O(n)
Space - O(n)
Easy Array
1470 Shuffle The Array Detailed Explanation Time - O(n)
Space - O(1)
Easy Array
2129 Capitalize the Title Explained Solution Time - O(n)
Space - O(1)
Easy String
2130 Rings and Rods Detailed Explanation Time - O(n)
Space - O(1)
Easy HashTable

Math

# Title Solution Complexity Difficulty Tags
7 Reverse Integer C++
JAVA
Time - O(log10(n))
Space - O(1)
Medium
9 Palindrome Number C++
JAVA
Time - O(log10(n))
Space - O(1)
Easy
50 Pow(x, n) JAVA Time - O(log2(n))
Space - O(1)
Medium Recursion
172 Factorial Trailing Zeroes C++
JAVA
Time - O(log5(n))
Space - O(1)
Medium
231 Power Of Two C++
JAVA
Time - O(1)
Space - O(1)
Easy Bit Manipuation, Recursion
258 Add Digits C++
JAVA
Time - O(1)
Space - O(1)
Easy Simulation, Number Theory
263 Ugly Number C++
JAVA
Time - O(log2(n))
Space - O(1)
Easy
1137 N-th Tribonacci Number C++
JAVA
Time - O(n)
Space - O(1)
Easy Dynamic Programming, Memoization
1281 Subtract the Product and Sum of Digits of an Integer C++
JAVA
Time - O(n)
Space - O(1)
Easy
1342 Number of Steps to Reduce a Number to Zero C++
JAVA
Time - O(log2(n))
Space - O(1)
Easy Bit Manipuation
1551 Minimum Operations to Make Array Equal C++
JAVA
Time - O(1)
Space - O(1)
Medium
1688 Count of Matches in Tournament C++
JAVA
Time - O(1)
Space - O(1)
Easy Simulation

Bit Manipulation

# Title Solution Complexity Difficulty Tags
191 Number of 1 Bits C++
JAVA
Time - O(1)
Space - O(1)
Easy
476 Number Complement C++
JAVA
Time - O(log2(n))
Space - O(1)
Easy
1009 Complement of Base 10 Integer C++
JAVA
Time - O(log2(n))
Space - O(1)
Easy

Array

# Title Solution Complexity Difficulty
1 Two Sum JAVA Time - O(n)
Space - O(n)
Easy
53 Maximum Subarray C++
JAVA
Time - O(n)
Space - O(1)
Easy
80 Remove Duplicates from Sorted Array II C++
JAVA
Time - O(n)
Space - O(1)
Medium
121 Best Time to Buy and Sell Stock C++
JAVA
Time - O(n)
Space - O(1)
Easy
122 Best Time to Buy and Sell Stock II C++
JAVA
Time - O(n)
Space - O(1)
Medium
136 Single Number C++
JAVA
Time - O(n)
Space - O(1)
Easy
162 Find Peak Element C++
JAVA
Time - O(log2(n))
Space - O(1)
Medium
169 Majority Element C++
JAVA
Time - O(n)
Space - O(1)
Easy
189 Rotate Array C++
JAVA,
Explanation
Time - O(n)
Space - O(1)
Medium
228 Summary Ranges JAVA Time - O(n)
Space - O(1)
Easy
229 Majority Element II C++
JAVA
Time - O(n)
Space - O(1)
Medium
287 Find the Duplicate Number JAVA Time - O(n)
Space - O(1)
Medium
413 Arithmetic Slices C++
JAVA
Time - O(n)
Space - O(1)
Medium
414 Third Maximum Number JAVA Time - O(n)
Space - O(1)
Easy
500 Keyboard Row JAVA Time - O(N)
Space - O(n)
Easy
525 Contiguous Array C++
JAVA
Time - O(n)
Space - O(n)
Medium
532 K-diff Pairs in an Array C++
JAVA
Time - O(n)
Space - O(n)
Medium
648 Replace Words JAVA Time - O(m * n)
Space - O(n)
Medium
739 Daily Temperatures JAVA Time - O(n)
Space - O(n)
Medium
746 Min Cost Climbing Stairs C++
JAVA
Time - O(n)
Space - O(1)
Easy
811 Subdomain Visit Count JAVA Time - O(n * m)
Space - O(n)
Medium
881 Boats to Save People JAVA Time - O(n logn)
Space - O(1)
Medium
848 Shifting Letters C++
JAVA
Time - O(n)
Space - O(1)
Medium
941 Valid Mountain Array C++
JAVA
Time - O(n)
Space - O(1)
Easy
1094 Car Pooling C++
JAVA
Time - O(n)
Space - O(n)
Medium
1207 Unique Number of Occurrences JAVA Time - O(n)
Space - O(n)
Easy
1282 Group the People Given the Group Size They Belong To JAVA Time - O(n)
Space - O(n)
Medium
1295 Find Numbers with Even Number of Digits C++
JAVA
Time - O(n)
Space - O(1)
Easy
1365 How Many Numbers Are Smaller Than the Current Number C++
JAVA
Time - O(n + m)
Space - O(m)
Easy
1431 Kids With the Greatest Number of Candies C++
JAVA
Time - O(n)
Space - O(n)
Easy
1441 Build an Array With Stack Operations JAVA Time - O(n)
Space - O(n)
Easy
1464 Maximum Product of Two Elements in an Array C++
JAVA
Time - O(n)
Space - O(1)
Easy
1470 Shuffle The Array C++
JAVA
Time - O(n)
Space - O(1)
Easy
1480 Running Sum of 1d Array C++
JAVA
Time - O(n)
Space - O(1)
Easy
1512 Number of Good Pairs C++
JAVA
Time - O(n)
Space - O(n)
Easy
1550 Three Consecutive Odds C++
JAVA
Time - O(n)
Space - O(1)
Easy
1646 Get Maximum in Generated Array C++
JAVA
Time - O(n)
Space - O(n)
Easy
1662 Check If Two String Arrays are Equivalent C++
JAVA
Time - O(n)
Space - O(1)
Easy
1672 Richest Customer Wealth C++
JAVA
Time - O(m * n)
Space - O(1)
Easy
1720 Decode XORed Array C++
JAVA
Time - O(n)
Space - O(1)
Easy
1748 Sum of Unique Elements C++
JAVA
Time - O(n)
Space - O(m)
Easy
1909 Remove One Element to Make the Array Strictly Increasing C++
JAVA
Time - O(n)
Space - O(1)
Easy
1929 Concatenation of Array C++
JAVA
Time - O(n)
Space - O(1)
Easy
1979 Find Greatest Common Divisor of Array C++
JAVA
Time - O(n)
Space - O(1)
Easy
1980 Find Unique Binary String C++
JAVA
Time - O(n)
Space - O(1)
Medium
2006 Count Number of Pairs With Absolute Difference K C++
JAVA
Time - O(n + m)
Space - O(m)
Easy
2011 Final Value of Variable After Performing Operations C++
JAVA
Time - O(n)
Space - O(m)
Easy
2023 Number of Pairs of Strings With Concatenation Equal to Target JAVA Time - O(n * m)
Space - O(n)
Easy
2078 Two Furthest Houses With Different Colors C++
JAVA
Time - O(n)
Space - O(1)
Easy
2079 Watering Plants C++
JAVA
Time - O(n)
Space - O(1)
Medium
2114 Maximum Number of Words Found in Sentences C++
JAVA
Time - O(n * m)
Space - O(1)
Easy
2125 Number of Laser Beams in a Bank C++
JAVA
Time - O(n * m)
Space - O(1)
Medium
2161 Partition Array According to Given Pivot JAVA Time - O(n)
Space - O(1)
Medium

String

# Title Solution Complexity Difficulty
5 Longest Palindromic Substring JAVA Time - O(n2)
Space - O(1)
Medium
49 Group Anagrams JAVA Time - O(n * m)
Space - O(n)
Medium
67 Add Binary JAVA Time - O(max(n, m))
Space - O(max(n, m))
Easy
125 Valid Palindrome C++
JAVA
Time - O(n)
Space - O(1)
Easy
151 Reverse Words in a String JAVA Time - O(n * m)
Space - O(n)
Medium
165 Compare Version Numbers C++
JAVA
Time - O(max(n, m))
Space - O(1)
Medium
171 Excel Sheet Column Number C++
JAVA
Time - O(n)
Space - O(1)
Easy
205 Isomorphic Strings C++
JAVA
Time - O(n)
Space - O(1)
Easy
242 Valid Anagram JAVA Time - O(n)
Space - O(1)
Easy
290 Word Pattern JAVA Time - O(n * m)
Space - O(n)
Easy
344 Reverse String C++
JAVA
Time - O(n)
Space - O(1)
Easy
389 Find the Difference C++
JAVA
Time - O(n)
Space - O(1)
Easy
392 Is Subsequence C++
JAVA
Time - O(n)
Space - O(1)
Easy
409 Longest Palindrome JAVA Time - O(n)
Space - O(n)
Easy
438 Find All Anagrams in a String JAVA Time - O(n)
Space - O(n)
Medium
500 Keyboard Row JAVA Time - O(n)
Space - O(n)
Easy
520 Detect Capital C++
JAVA
Time - O(n)
Space - O(1)
Easy
567 Permutation in String C++
JAVA
Time - O(n)
Space - O(1)
Medium
647 Palindromic Substrings C++
JAVA
Time - O(n2)
Space - O(1)
Medium
680 Valid Palindrome II C++
JAVA
Time - O(n)
Space - O(1)
Easy
796 Rotate String C++
JAVA
Time - O(m * n)
Space - O(1)
Easy
848 Shifting Letters C++
JAVA
Time - O(n)
Space - O(1)
Medium
1446 Consecutive Characters C++
JAVA
Time - O(n)
Space - O(1)
Easy
1461 Check If a String Contains All Binary Codes of Size K JAVA Time - O(n)
Space - O(2k)
Medium
1528 Shuffle String C++
JAVA
Time - O(n)
Space - O(1)
Easy
1544 Make The String Great C++
JAVA
Time - O(n)
Space - O(1)
Easy
1662 Check If Two String Arrays are Equivalent C++
JAVA
Time - O(n)
Space - O(1)
Easy
1678 Goal Parser Interpretation C++
JAVA
Time - O(n)
Space - O(1)
Easy
1689 Partitioning Into Minimum Number Of Deci-Binary Numbers JAVA Time - O(n)
Space - O(1)
Medium
1790 Check if One String Swap Can Make Strings Equal C++
JAVA
Time - O(n)
Space - O(1)
Easy
1769 Minimum Number of Operations to Move All Balls to Each Box C++
JAVA
Time - O(n)
Space - O(n)
Medium
1796 Second Largest Digit in a String C++
JAVA
Time - O(n)
Space - O(1)
Easy
1816 Truncate Sentence C++
JAVA
Time - O(n)
Space - O(1)
Easy
1844 Replace All Digits with Characters C++
JAVA
Time - O(n)
Space - O(1)
Easy
1903 Largest Odd Number in String C++
JAVA
Time - O(n)
Space - O(1)
Easy
1910 Remove All Occurrences of a Substring C++
JAVA
Time - O(m * n)
Space - O(n)
Medium
1945 Sum of Digits of String After Convert C++
JAVA
Time - O(n)
Space - O(1)
Easy
1941 Check if All Characters Have Equal Number of Occurrences C++
JAVA
Time - O(n)
Space - O(1)
Easy
2000 Reverse Prefix of Word C++
JAVA
Time - O(n)
Space - O(1)
Easy
2042 Check if Numbers Are Ascending in a Sentence C++
JAVA
Time - O(n)
Space - O(1)
Easy
2103 Rings and Rods C++
JAVA
Time - O(n)
Space - O(1)
Easy
1456 Maximum Number of Vowels in a Substring of Given Length C++
JAVA
Time - O(n)
Space - O(1)
Medium
2068 Check Whether Two Strings are Almost Equivalent C++
JAVA
Time - O(n)
Space - O(1)
Easy
2124 Check if All A's Appears Before All B's C++
JAVA
Time - O(n)
Space - O(1)
Easy
2129 Capitalize the Title Explanation
C++
JAVA
Time - O(n)
Space - O(1)
Easy

Matrix

# Title Solution Complexity Difficulty Tags
73 Set Matrix Zeroes JAVA Time - O(m * n)
Space - O(m * n)
Easy Array, HashTable
566 Reshape the Matrix C++
JAVA
Time - O(m * n)
Space - O(1)
Easy Array, Simulation
867 Transpose Matrix C++
JAVA
Time - O(m * n)
Space - O(1)
Easy Array, Simulation
1260 Shift 2D Grid JAVA Time - O(n)
Space - O(1)
Easy Array, Simulation
1572 Matrix Diagonal Sum C++
JAVA
Time - O(n)
Space - O(1)
Easy Array
1582 Special Positions in a Binary Matrix C++
JAVA
Time - O(m * n)
Space - O(1)
Easy Array

Hash Table

# Title Solution Complexity Difficulty Tags
1 Two Sum JAVA Time - O(n)
Space - O(n)
Easy Array
49 Group Anagrams JAVA Time - O(n * m)
Space - O(n)
Medium String, Sorting
500 Keyboard Row JAVA Time - O(n)
Space - O(n)
Easy Array, String
1207 Unique Number of Occurrences JAVA Time - O(n)
Space - O(n)
Easy Array
1365 How Many Numbers Are Smaller Than the Current Number C++
JAVA
Time - O(n + m)
Space - O(m)
Easy Array, Sorting, Counting
2053 Kth Distinct String in an Array JAVA Time - O(n)
Space - O(n)
Easy Array, String, Counting
2085 Count Common Words With One Occurrence JAVA Time - O(n + m)
Space - O(n)
Easy Array, String, Counting

Binary Search

# Title Solution Complexity Difficulty Tags
33 Search in Rotated Sorted Array C++
JAVA
Time - O(log2(n))
Space - O(1)
Medium Array
74 Search a 2D Matrix C++
JAVA
Time - O(log2(m * n))
Space - O(1)
Medium Array, Matrix
153 Find Minimum in Rotated Sorted Array C++
JAVA
Time - O(log2(n))
Space - O(1)
Medium Array
162 Find Peak Element C++
JAVA
Time - O(log2(n))
Space - O(1)
Medium Array
704 Binary Search C++
JAVA
Time - O(log2(n))
Space - O(1)
Easy Array

Sorting

# Title Solution Complexity Difficulty
1365 How Many Numbers Are Smaller Than the Current Number C++
JAVA
Time - O(n + m)
Space - O(m)
Easy

Two Pointers

# Title Solution Complexity Difficulty
80 Remove Duplicates from Sorted Array II C++
JAVA
Time - O(n)
Space - O(1)
Medium

Dynamic Programming

# Title Solution Complexity Difficulty Tags
70 Climbing Stairs C++
JAVA
Time - O(n)
Space - O(1)
Easy Math, Memoization
264 Ugly Number II C++
JAVA
Time - O(n)
Space - O(n)
Medium HashTable, Math, Heap(Priority Queue)
338 Counting Bits C++
JAVA
Time - O(n)
Space - O(n)
Easy Bit Manipulation
1137 N-th Tribonacci Number C++
JAVA
Time - O(n)
Space - O(1)
Easy Math, Memoization
1646 Get Maximum in Generated Array C++
JAVA
Time - O(n)
Space - O(n)
Easy Array, Simulation
You might also like...

🎓☕ Repository of lessons and exercises from loiane.training's course on data structure with Java

🎓☕ Repository of lessons and exercises from loiane.training's course on data structure with Java

☕ Curso estrutura de dados com Java by @loiane.training Repositório com as aulas e exercícios do curso de estrutura de dados com Java da loiane.traini

Feb 1, 2022

Facebook Clone created using java based on Graph data Structure

Facebook Clone created using java based on Graph data Structure

Facebook Clone Facebook Clone created using java based on Graph data Structure Representation of Social Media using Graph Data Structure in Java It is

Jan 16, 2022

JAVA DASAR PROGRAMING

JAVA DASAR PROGRAMING Halo , Selamat datang di repository kami ! BELAJAR JAVA DASAR HANYA DALAM WAKTU 14 HARI Ayo berpartisipasi menjadi bagian dari J

Nov 5, 2022

Worker-queue implementation on top of Java and database

Database Queue Library provides worker-queue implementation on top of Java and database. Fintech company YooMoney uses db-queue in cases where reliabi

Dec 12, 2022

Data structures and algorithms exercises in java

Data structures and algorithms exercises in java

Data structure and algorithms in Java About The Project [] In this repository you can find examples of data structure exercises solved in java and som

Nov 25, 2021

Data structures & algorithms implemented in Java and solutions to leetcode problems.

Hello, World! 👋 Hey everyone, I'm Sharad ☃ , and I'm a Software Engineer 💻 at eGain! This repository 📝 is all about data structures & algorithms an

Dec 16, 2022

Solutions to daily problems of Leetcode

leetcode-daily-problems-solutions Leetcode Daily Challenge : Date Problem Solution Comment 29/01/2022 84. Largest Rectangle in Histogram Solution Usin

Feb 3, 2022

Leetcode Problems & Solutions

LeetCode Problems & Solutions Optimal solutions to LeetCode Problems. Any kind of improvement suggestion is welcome. Algorithms Title Language(s) Diff

Jul 26, 2022

My solutions for leetcode problems.

My Java Solutions for Leetcode (inspired by qiyuangong's leetcode) Problems & Solutions # Title Solution 202 Happy Number Java 215 Kth Largest Element

Jun 25, 2022

Java solutions for LeetCode

leetcode-java Java solutions for LeetCode Environment Intellij IDEA 2021.2 JDK 16.0.1 Gradle 7.1 Junit 5.7 ProblemList # Title Difficulty Link Solutio

Aug 5, 2021

Repository with LeetCode Solutions and Dedicated Index to prepare for your FAANGM Interviews.

Repository with LeetCode Solutions and Dedicated Index to prepare for your FAANGM Interviews. Feel free to share and Contribute to this repository.

Jan 1, 2023

This repository consists of solutions to various LeetCode questions

This repository consists of solutions to various LeetCode questions. I have tried to add as many solutions in as many languages as possible. Feel free to contribute to this repository while following the contribution rules.

Mar 24, 2022

Solutions for some common algorithm problems written in Java.

Solutions for some common algorithm problems written in Java.

Algorithms This repository contains my solution for common algorithms. I've created this repository to learn about algorithms and improve solutions to

Dec 30, 2022

This JAVA repository contains solutions for common algorithms and problems.

This JAVA repository contains solutions for common algorithms and problems.

JAVA-Algorithms 🚀 Description Beep Boop! Boop Beep!. I have created this repository to improve my Logical thinking skills & Knowledge in programming.

Apr 11, 2022

☕️🚀🚨 This repository consists of solutions for the Community Classroom complete DSA Java Bootcamp assignment problems.

Java DSA Bootcamp 🌐 Assignments Go to Kunal Kushwaha's 👨🏻‍💻 course repository : ☕️ Java DSA Bootcamp ⚠️ 🚨 this repository is now inactive due to

Dec 12, 2022

Solutions of many problems from HackerRank

Solutions of some problems from HackerRank made specially for beginners and who are in the intermediate level If you like this project, please leave m

Nov 14, 2022

This repository contains my solutions to programming problems of the UVa online-judge.

UVa Solutions This repository contains my solutions to programming problems of the UVa online-judge. Problem Set Volumes (100...1999) Volume 12 Proble

Nov 15, 2021

Solutions to HackerRank problems

HackerRank This is my solutions to HackerRank problems using java, might not be the best solution but at least i tried Algorithms Subdomain Difficulty

Apr 4, 2022
Comments
  • Create subdirectories for easy, medium, hard

    Create subdirectories for easy, medium, hard

    Its a nice intiative akshaya, can you additionally segregate the problems by creating three folders for easy, medium and hard so that it's easy to filter the problem choice

    opened by saxenarrohit 1
Owner
Akshaya Amar
Android Developer | Freelancer
Akshaya Amar
Solutions to daily problems of Leetcode

leetcode-daily-problems-solutions Leetcode Daily Challenge : Date Problem Solution Comment 29/01/2022 84. Largest Rectangle in Histogram Solution Usin

Jay Teli 1 Feb 3, 2022
A Persistent Java Collections Library

PCollections A Persistent Java Collections Library Overview PCollections serves as a persistent and immutable analogue of the Java Collections Framewo

harold cooper 708 Dec 28, 2022
A better compressed bitset in Java

RoaringBitmap Bitsets, also called bitmaps, are commonly used as fast data structures. Unfortunately, they can use too much memory. To compensate, we

Roaring bitmaps: A better compressed bitset 2.9k Dec 29, 2022
A lightning fast, transactional, file-based FIFO for Android and Java.

Tape by Square, Inc. Tape is a collection of queue-related classes for Android and Java. QueueFile is a lightning-fast, transactional, file-based FIFO

Square 2.4k Dec 30, 2022
gRPC and protocol buffers for Android, Kotlin, and Java.

Wire “A man got to have a code!” - Omar Little See the project website for documentation and APIs. As our teams and programs grow, the variety and vol

Square 3.9k Dec 23, 2022
A fast, simple persistent queue written in Java

Ladder Introduction Ladder is a lightning fast persistent queue written in Java. Usage Installation // TODO publish to Maven Central Create persistent

null 6 Sep 9, 2022
Example of implementing data structures in Java

Data Structures Example of implementing data structures in Java Implemented data structure : Queue Stack Linked List Binary Search Tree (BST) Trie Hea

Ismaïl BENHALLAM 2 Sep 27, 2021
This repository contains codes for various data structures and algorithms in C, C++, Java, Python, C#, Go, JavaScript and Kotlin.

Overview The goal of this project is to have codes for various data structures and algorithms - in C, C++, Java, Python, C#, Go, JavaScript and Kotlin

Manan 25 Mar 2, 2022
Material de apoio da turma de introdução ao JAVA da Let's Code

Introdução ao Java Esse repositório foi criado com o intuito de servir como material de apoio ao módulo de Introdução a Java da Let's Code. Sumário Li

Bruno Pereira Pinho 5 Oct 25, 2022
A repository that contains Data Structure and Algorithms coded on Java

A repository that contains Data Structure and Algorithms coded on Java . It will also contain solutions of questions from Leetcode.

Akshat Gupta 6 Oct 15, 2022