class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int res=0;
sort(nums.begin(),nums.end());
for (int i=0;i<nums.size();i=i+2)
{
res+=nums[i];
}
return res;
}
};
注:字符串s长度用s.length();
public:
int arrayPairSum(vector<int>& nums) {
int res=0;
sort(nums.begin(),nums.end());
for (int i=0;i<nums.size();i=i+2)
{
res+=nums[i];
}
return res;
}
};
Example 1:
Input: [1,4,3,2] Output: 4 Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
Note:
- n is a positive integer, which is in the range of [1, 10000].
- All the integers in the array will be in the range of [-10000, 10000].
class Solution {
public:
int arrayPairSum(vector<int>& nums) {
int res=0;
sort(nums.begin(),nums.end());
for (int i=0;i<nums.size();i=i+2)
{
res+=nums[i];
}
return res;
}
};
2
Write a function that takes a string as input and returns the string reversed.
Example:
Given s = "hello", return "olleh".
class Solution {
public:
string reverseString(string s) {
int lenth=s.length();
for(int i=0;i<lenth/2;i++)
{
char a=s[i];
s[i]=s[lenth-i-1];
s[lenth-i-1]=a;
}
return s;
}
};
注:字符串s长度用s.length();
容器的大小用nums.size()