From 2ee49ea476226b0de62aaf2330daea47833bc62e Mon Sep 17 00:00:00 2001 From: Wander_Lust Date: Thu, 29 Jan 2026 01:04:37 +0530 Subject: [PATCH] Leetcode day 2 and 3 --- leetcode/lc11/mostWater.cpp | 16 ++++++++++++++ leetcode/lc11/notes.md | 3 +++ leetcode/lc12/IntToRom.cpp | 0 leetcode/lc12/notes.md | 4 ++++ leetcode/lc13/romanToInt.cpp | 17 +++++++++++++++ leetcode/lc5/ExpandCentre.cpp | 0 leetcode/lc5/brute.cpp | 23 ++++++++++++++++++++ leetcode/lc5/manachers.cpp | 41 +++++++++++++++++++++++++++++++++++ leetcode/lc5/notes.md | 0 leetcode/lc6/notes.md | 5 +++++ leetcode/lc6/zigzag.cpp | 24 ++++++++++++++++++++ leetcode/lc7/RevInt.cpp | 20 +++++++++++++++++ leetcode/lc7/notes.md | 13 +++++++++++ leetcode/lc8/atoi.cpp | 37 +++++++++++++++++++++++++++++++ leetcode/lc9/Palindrome.cpp | 2 +- 15 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 leetcode/lc11/mostWater.cpp create mode 100644 leetcode/lc11/notes.md create mode 100644 leetcode/lc12/IntToRom.cpp create mode 100644 leetcode/lc12/notes.md create mode 100644 leetcode/lc13/romanToInt.cpp create mode 100644 leetcode/lc5/ExpandCentre.cpp create mode 100644 leetcode/lc5/brute.cpp create mode 100644 leetcode/lc5/manachers.cpp create mode 100644 leetcode/lc5/notes.md create mode 100644 leetcode/lc6/notes.md create mode 100644 leetcode/lc6/zigzag.cpp create mode 100644 leetcode/lc7/RevInt.cpp create mode 100644 leetcode/lc7/notes.md create mode 100644 leetcode/lc8/atoi.cpp diff --git a/leetcode/lc11/mostWater.cpp b/leetcode/lc11/mostWater.cpp new file mode 100644 index 0000000..990b02a --- /dev/null +++ b/leetcode/lc11/mostWater.cpp @@ -0,0 +1,16 @@ +class Solution{ + public: + int maxArea(vector &height){ + int maxArea = 0; + int left = 0; + int right = height.size()-1; + while(leftfor(const auto& [value,symbol] : valueSymbols) + +here [value,symbol] in cpp is called structured binding that allows you to unpack the elements of a compound object (such as a std::pair, std::tuple, or struct) into separate, named variables. +Each element of valueSymbols is a pair representing a Roman numeral value and its symbol. The syntax for (const auto& [value, symbol] : valueSymbols) unpackseach pair so that value refers to the numeric part and symbol refers to the Roman string diff --git a/leetcode/lc13/romanToInt.cpp b/leetcode/lc13/romanToInt.cpp new file mode 100644 index 0000000..6f9461a --- /dev/null +++ b/leetcode/lc13/romanToInt.cpp @@ -0,0 +1,17 @@ +class Solution{ + public: + int romanToInt(string s){ + unordered_map convert = {{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}}; + int result = 0; + for(int i=0;i0;i--){ + for(int start = 0;start<=s.size()-length;start++){ + if(check(s,start,start+length)) + return s.substr(start,length); + } + } + } + private: bool check(string s, int i, int j){ + int left = i; + int right = j-1; + while(left p(n,0); + int C =0; + int R =0; + + for(int i=0;i=0 && t[i+p[i]+1] == t[i-p[i]-1]){ + p[i]++; + } + if(i+p[i]>R){ + C = i; + R = i+p[i]; + } + + } + int maxLen = 0; + int centerIndex = 0; + for(int i =0;imaxLen){ + maxLen = p[i]; + centerIndex = i; + } + } + int start = (centerIndex-maxLen)/2; + return s.substr(start, maxLen); + } +} diff --git a/leetcode/lc5/notes.md b/leetcode/lc5/notes.md new file mode 100644 index 0000000..e69de29 diff --git a/leetcode/lc6/notes.md b/leetcode/lc6/notes.md new file mode 100644 index 0000000..ab9d430 --- /dev/null +++ b/leetcode/lc6/notes.md @@ -0,0 +1,5 @@ +We can use 2D array instead of vector of vectors but prefer vector> when +It has to grow dynamically +Different rows have different lengths +To save memory + diff --git a/leetcode/lc6/zigzag.cpp b/leetcode/lc6/zigzag.cpp new file mode 100644 index 0000000..1ca815e --- /dev/null +++ b/leetcode/lc6/zigzag.cpp @@ -0,0 +1,24 @@ +class Solution{ + public: + string convert(string s,int numrows){ + if(numrows == 0|| numsrows>=s.length()) + return s; + vector> rows(numrows); + int index = 0, direc = 1; + + for(char c: s){ + rows[index].push_back(c); + if(index == 0) + direc =1; + else if(index == numrows -1) + direc = -1; + index+=direc; + } + string res; + for(const auto &row : rows) + for(char c : row) + res+=c; + + return res; + } +}; diff --git a/leetcode/lc7/RevInt.cpp b/leetcode/lc7/RevInt.cpp new file mode 100644 index 0000000..b8ebcf8 --- /dev/null +++ b/leetcode/lc7/RevInt.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int reverse(int x) { + int rev =0; + while(x!=0){ + int digit = x%10; + x/=10; + if(rev>INT_MAX/10 || rev==INT_MAX/10 && digit > 7){ + return 0; + } + if(rev + +Modern Alternative: + +#include + +numeric_limits::max(); +numeric_limits::min(); + +int maxInt = std::numeric_limits::max(); + + diff --git a/leetcode/lc8/atoi.cpp b/leetcode/lc8/atoi.cpp new file mode 100644 index 0000000..679593a --- /dev/null +++ b/leetcode/lc8/atoi.cpp @@ -0,0 +1,37 @@ +class Solution { +public: + int myAtoi(string s) { + if(s.empty()) return 0; + + const long long MAX_INT = INT_MAX; + const long long MIN_INT = INT_MIN; + + int n = s.length(); + int i = 0; + while(i= MAX_INT) + return MAX_INT; + if(sign*res <= MIN_INT) + return MIN_INT; + i++; + } + return sign*res; + } + +}; diff --git a/leetcode/lc9/Palindrome.cpp b/leetcode/lc9/Palindrome.cpp index 5f7285c..4fa50c5 100644 --- a/leetcode/lc9/Palindrome.cpp +++ b/leetcode/lc9/Palindrome.cpp @@ -9,6 +9,6 @@ class Solution{ rev = (rev*10) + (x%10); x/=10; } - return ( x==rev || x = rev/10); + return ( x==rev || x == rev/10); } }