Given a non-empty array of integers, return the third maximum number in this array. If it does not exist, return the maximum number. The time complexity must be in O(n).
Example 1:
Input: [3, 2, 1]
Output: 1
Explanation: The third maximum is 1.
Example 2:
Input: [1, 2]
Output: 2
Explanation: The third maximum does not exist, so the maximum (2) is returned instead.
Example 3:
Input: [2, 2, 3, 1]
Output: 1
Explanation: Note that the third maximum here means the third maximum distinct number.
Both numbers with value 2 are both considered as second maximum.
Solution in C++:
关键点:
思路:
自己代码
int thirdMax(vector& nums) {
size_t size = nums.size();
if (size == 0)
return 0;
else if (size == 1)
return nums[0];
int max = nums[0];
long long semax = LONG_MIN;
long long thmax = LONG_MIN;
bool flag = false;
for(auto num : nums){
if (num < max && num < semax && thmax < num){
flag = true;
thmax = num;
} else if(num > max){
if (semax < max){
if (thmax < semax){
flag = true;
thmax = semax;
}
semax = max;
}
max = num;
} else if (num < max && num > semax){
if (thmax < semax){
flag = true;
thmax = semax;
}
semax = num;
}
}
if (!flag)
return max;
else
return thmax;
}
简介版
int thirdMax(vector& nums) {
if (nums.size() == 0) {
return 0;
}
long firstMax = LONG_MIN;
long secondMax = LONG_MIN;
long thirdMax = LONG_MIN;
for (int num : nums) {
if (num > firstMax) {
thirdMax = secondMax;
secondMax = firstMax;
firstMax = num;
} else if (num > secondMax && num < firstMax) {
thirdMax = secondMax;
secondMax = num;
} else if (num > thirdMax && num < firstMax && num < secondMax) {
thirdMax = num;
}
}
return (int) (thirdMax == LONG_MIN ? firstMax : thirdMax);
}
Given two non-negative integers num1
and num2
represented as string, return the sum of num1
and num2
.
Note:
num1
and num2
is < 5100.num1
and num2
contains only digits 0-9
.num1
and num2
does not contain any leading zero.Solution in C++:
关键点:
思路:
自己代码
string addStrings(string num1, string num2) {
string result;
size_t size1 = num1.size();
size_t size2 = num2.size();
size_t minsize = size1 < size2 ? size1 : size2;
int carry = 0;
for(int i = 1; i <= minsize; ++i){
int tmp = num1[size1 - i] + num2[size2 -i] - 2 * '0' + carry;
if (tmp > 9){
carry = 1;
tmp = tmp - 10;
}
else
carry = 0;
result = to_string(tmp) + result;
}
if (carry == 0){ // 无进位
if (size1 > size2)
result = num1.substr(0, size1 - size2) + result;
else
result = num2.substr(0, size2 - size1) + result;
} else{ // 有进位
if (size1 > size2){
for(int i = size1 - size2 - 1; i >=0 ; --i){
int tmp = num1[i] -'0' + carry;
if (tmp > 9){
carry = 1;
tmp = tmp - 10;
}
else
carry = 0;
result = to_string(tmp) + result;
}
} else if (size1 < size2){
for(int i = size2 - size1 - 1; i >=0 ; --i){
int tmp = num2[i] - '0' + carry;
if (tmp > 9){
carry = 1;
tmp = tmp - 10;
}
else
carry = 0;
result = to_string(tmp) + result;
}
}
}
if (carry == 1)
result = to_string(1) + result;
return result;
}
简洁版
string addStrings(string num1, string num2) {
string result;
int carry = 0;
for(int i = num1.size() - 1, j = num2.size() - 1; i >= 0 || j >= 0 || carry == 1; i--, j--){
int x = i < 0 ? 0 : num1[i] - '0';
int y = j < 0 ? 0 : num2[j] - '0';
result = to_string((x + y + carry) % 10) + result;
carry = (x + y + carry) / 10;
}
return result;
}
Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.
Please note that the string does not contain any non-printable characters.
Example:
Input: "Hello, my name is John"
Output: 5
Solution in C++:
关键点:
思路:
自己代码
int countSegments(string s) {
int count = 0;
size_t size = s.size();
// 去掉头部空格
int i = 0;
while(s[i] == ' ')
++i;
// 去掉尾部空格
int j = 0;
while(s[size - j - 1] == ' ')
++j;
bool flag = false;
for(; i < size - j; ++i){
flag = true;
if (s[i] == ' '){
++count;
while(s[i] == ' ')
++i;
--i;
}
}
if (flag)
return count+1;
else
return count;
}
简洁版
int countSegments(string s) {
int count = 0;
size_t size = s.size();
for(int i = 0; i < size; ++i){
if ((i == 0 || s[i-1] == ' ') && s[i] != ' ')
++count;
}
return count;
}
Given a string s and a non-empty string p, find all the start indices of p’s anagrams in s.
Strings consists of lowercase English letters only and the length of both strings s and p will not be larger than 20,100.
The order of output does not matter.
Example 1:
Input:
s: "cbaebabacd" p: "abc"
Output:
[0, 6]
Explanation:
The substring with start index = 0 is "cba", which is an anagram of "abc".
The substring with start index = 6 is "bac", which is an anagram of "abc".
Example 2:
Input:
s: "abab" p: "ab"
Output:
[0, 1, 2]
Explanation:
The substring with start index = 0 is "ab", which is an anagram of "ab".
The substring with start index = 1 is "ba", which is an anagram of "ab".
The substring with start index = 2 is "ab", which is an anagram of "ab".
Solution in C++:
关键点:
思路:
vector findAnagrams(string s, string p) {
vector result;
size_t sizes = s.size();
size_t sizep = p.size();
if (sizes < sizep || sizes == 0)
return result;
for(int i = 0; i < sizes - sizep + 1; ++i){
// 判断是否s[i , i+sizep]为p的anagrams
bool flag = true;
vector letters(26,0);
for(int j = 0; j < sizep; ++j){
++letters[p[j]-'a'];
--letters[s[i+j]-'a'];
}
for(auto letter : letters)
if(letter != 0)
flag = false;
if (flag)
result.push_back(i);
}
return result;
}
哇,今天刷题还不错,昨天的代码就改了范围就跑通了,然后就是接下来的两题,写的代码看都不想看,不知道想的有多复杂,哎,可能还是分情况讨论能力不太强,看到别人的简洁代码我真是羡慕不已啊。