Skip to content

add 2 numbers and unique paths count #298

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions Medium/02.Add2Numbers/Add2Numbers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class Solution {
public:
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
ListNode *headnode = new ListNode();
ListNode *result = headnode ;

int carry = 0 , sum = 0 ;

while(l1 || l2){

sum = 0 ;

if(l1){
sum += l1->val;
l1 = l1->next ;
}

if(l2){
sum += l2->val;
l2 = l2->next ;
}

sum += carry ;

carry = sum > 9 ? 1: 0;
sum %= 10 ;

ListNode *dNode = new ListNode(sum);
headnode->next = dNode ;
headnode = dNode ;

}

if(carry){
ListNode *dNode = new ListNode(carry);
headnode->next = dNode;
return result->next ;
}
return result->next ;
}
};
23 changes: 23 additions & 0 deletions Medium/62.UniquePaths/Unique_paths.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public:
int solve(int m , int n , vector<vector <int>> &dp ){
if(m ==1 && n ==1)
return 1 ;

if(!m || !n)
return 0;

if(dp[m][n] != -1)
return dp[m][n] ;

dp[m][n] = solve(m-1,n, dp) + solve(m ,n-1,dp) ;

return dp[m][n] ;

}

int uniquePaths(int m, int n) {
vector<vector<int>> dp(m+1 ,vector<int>(n+1 ,-1));
return solve(m,n,dp) ;
}
};