Posts

Showing posts from July, 2023

LeetCode Palindrome Number

 Problem Description Link:  https://leetcode.com/problems/palindrome-number/ Accepted Solution /* The solution is easy, and I believe everyone knows the process. However, one problem is that x is a number. If it is a string, in that case, we can get the result after reversing it. Therefore, to solve the problem, we have to find the digits by modulating 10, store them in an array, and finally compare from both sides. If we find any discrepancy, we can tell it is not a palindrome. Note that any negative integer would never be a palindrome since it should contain a minus (-) sign. */ class Solution { public:     bool isPalindrome ( int x ) {         int digit [ 50 ], len= 0 ;         bool f = true ;         if (x < 0 ) {             return false ;         }         while (x> 0 )         {             digit [len++] = x% 10 ;             x/= 10 ;         }         for ( int i= 0 , j=len- 1 ; i<len; i++, j--)         {             if ( digit [i] != digit [j])             {  

LeetCode Add Two Numbers

 Problem Description Link:  https://leetcode.com/problems/add-two-numbers/description/ Accepted Solution /*  The solution technique is simple. You are given two linked lists. Find which is the larger or smaller. If both of them are similar in length, no problem. However, if you find any discrepancy in length, you have to insert extra zeros just back to the numbers. Finally, reverse each linked list, perform a fundamental summation, and store the results in another linked list, as you must return a linked list from the desired function. For good understanding, I have utilized two arrays while performing summation. Although the process is straight, brainstorming is necessary for coding. */ /**  * Definition for singly-linked list.  * struct ListNode {  *     int val;  *     struct ListNode *next;  * };  */ void insert_data ( struct ListNode ** head , int data ) {     struct ListNode *ptr, *tmp;     tmp = ( struct ListNode*) malloc ( sizeof ( struct ListNode));         ptr = *head;

LeetCode Two Sum

 Problem Description Link:  https://leetcode.com/problems/two-sum/description/ Accepted Solution /* I have tried to avoid the solution with O(N^2). Therefore, I found the difference (x) between the target and current value and eventually checked whether x is present in the array or not. If it is, then the current position (i) and the last position ind[nums[i]] would be the answer. However, if both the current value and x are similar, in that case, you have to check the value that exists in the array multiple times.  The complexity of the solution in O(N) . */ class Solution { public:     vector < int > twoSum ( vector < int > & nums , int target ) {         map< int , int >ind;         map< int , int >cnt;         vector< int >sol;         for ( int i= 0 ; i< nums . size (); i++) {             ind [ nums [i]] = i; // number's indices             cnt [ nums [i]]+= 1 ; // number's count         }         for ( int i= 0 ; i< nums . s