Compare commits
2 Commits
e4090f9a09
...
828069bf1a
Author | SHA1 | Date | |
---|---|---|---|
|
828069bf1a | ||
|
3c514a3cda |
75
addTwoNumbers.cpp
Normal file
75
addTwoNumbers.cpp
Normal file
@ -0,0 +1,75 @@
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
||||
struct ListNode {
|
||||
int val;
|
||||
ListNode *next;
|
||||
ListNode() : val(0), next(nullptr) {}
|
||||
ListNode(int x) : val(x), next(nullptr) {}
|
||||
ListNode(int x, ListNode *next) : val(x), next(next) {}
|
||||
};
|
||||
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
ListNode *addTwoNumbers(ListNode *l1, ListNode *l2)
|
||||
{
|
||||
ListNode *l3 = new ListNode();
|
||||
ListNode *begin = l3;
|
||||
int carry = 0;
|
||||
while (1) {
|
||||
if (l1 == nullptr && l2 == nullptr) {
|
||||
return begin;
|
||||
} else if (l1 == nullptr) {
|
||||
l3->val = l2->val + carry;
|
||||
l2 = l2->next;
|
||||
if (l3->val > 10) {
|
||||
carry = 1;
|
||||
l3->val -= 10;
|
||||
} else {
|
||||
carry = 0;
|
||||
}
|
||||
if (l2->next == nullptr) {
|
||||
return begin;
|
||||
} else {
|
||||
l3->next = new ListNode();
|
||||
l3 = l3->next;
|
||||
}
|
||||
} else if (l2 == nullptr) {
|
||||
l3->val = l1->val + carry;
|
||||
l1 = l1->next;
|
||||
if (l3->val > 10) {
|
||||
carry = 1;
|
||||
l3->val -= 10;
|
||||
} else {
|
||||
carry = 0;
|
||||
}
|
||||
if (l1->next == nullptr) {
|
||||
return begin;
|
||||
} else {
|
||||
l3->next = new ListNode();
|
||||
l3 = l3->next;
|
||||
}
|
||||
} else {
|
||||
l3->val = l1->val + l2->val + carry;
|
||||
|
||||
if (l3->val >= 10) {
|
||||
carry = 1;
|
||||
l3->val -= 10;
|
||||
} else {
|
||||
carry = 0;
|
||||
}
|
||||
|
||||
l1 = l1->next;
|
||||
l2 = l2->next;
|
||||
l3->next = new ListNode();
|
||||
l3 = l3->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
}
|
33
waysToSplitArray.cpp
Normal file
33
waysToSplitArray.cpp
Normal file
@ -0,0 +1,33 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int waysToSplitArray(vector<int>& nums) {
|
||||
long long sum1 = 0;
|
||||
int lens = nums.size();
|
||||
for(int i = 0; i < lens;i++){
|
||||
sum1 += nums[i];
|
||||
}
|
||||
long long sum2 = 0;
|
||||
int counter = 0;
|
||||
lens--;
|
||||
for(int i = 0; i < lens;i++){
|
||||
sum2 += nums[i];
|
||||
sum1 -= nums[i];
|
||||
if(sum2 >= sum1){
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
|
||||
return counter;
|
||||
}
|
||||
};
|
||||
|
||||
int main(){
|
||||
vector<int> nums = {10,4,-8,7};
|
||||
Solution solution;
|
||||
cout << solution.waysToSplitArray(nums) << endl;
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user