Compare commits

..

2 Commits

Author SHA1 Message Date
carry
828069bf1a 完成每日一题:分割数组的方案数 2025-01-13 12:50:43 +08:00
carry
3c514a3cda 两数相加,做了一半 2025-01-13 12:41:42 +08:00
2 changed files with 108 additions and 0 deletions

75
addTwoNumbers.cpp Normal file
View 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
View 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;
}