Leetcode
https://leetcode.com/problems/add-two-numbers/
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example 1:
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
Constraints:
The number of nodes in each linked list is in the range [1, 100].
0 <= Node.val <= 9
It is guaranteed that the list represents a number that does not have leading zeros.
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2){
struct ListNode *l3 = (struct ListNode *)malloc(sizeof(struct ListNode));
l3->val=0;
l3->next=NULL;
bool carry=false;
struct ListNode *p = l1, *q = l2, *r = l3;
while(true){
if (carry==true) r->val+=1;
if (p!=NULL){
r->val+=p->val;
p = p->next;
}
if (q!=NULL){
r->val+=q->val;
q = q->next;
}
carry=false;
if (r->val>=10){
carry=true;
r->val -= 10;
}
if(p==NULL && q==NULL && carry==false)
break;
r->next = (struct ListNode *)malloc(sizeof(struct ListNode));
r = r->next;
r->val=0;
r->next=NULL;
}
return l3;
}
https://leetcode.com/problems/house-robber/ Description You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and it will automatically contact the police if two adjacent houses were broken into on the same night. Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight without alerting the police. Example 1: Input: nums = [1,2,3,1]
Dec 9, 2020https://leetcode.com/problems/coin-change/ Description You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. You may assume that you have an infinite number of each kind of coin. Example 1: Input: coins = [1,2,5], amount = 11
Dec 7, 2020https://leetcode.com/problems/special-array-with-x-elements-greater-than-or-equal-x/ Description You are given an array nums of non-negative integers. nums is considered special if there exists a number x such that there are exactly x numbers in nums that are greater than or equal to x. Notice that x does not have to be an element in nums. Return x if the array is special, otherwise, return -1. It can be proven that if nums is special, the value for x is unique. Example 1:
Dec 7, 2020https://leetcode.com/problems/validate-stack-sequences/ Description Given two sequences pushed and popped with distinct values, return true if and only if this could have been the result of a sequence of push and pop operations on an initially empty stack. Example 1: Input: pushed = [1,2,3,4,5], popped = [4,5,3,2,1] Output: true Explanation: We might do the following sequence:
Dec 7, 2020or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up