文章目录
414. Third Maximum Number
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_MIN更小,以免输入为INT_MIN时,最后判断出错;所以都采取范围更大的long或者long long来进行。接下来就是对第三大的几种分类及数据更新问题,记得将最大值修改后其后比它小的数也要跟着更新即可
自己代码
int thirdMax(vector<int>& 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<int>& 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);
}
415. Add Strings
Given two non-negative integers num1
and num2
represented as string, return the sum of num1
and num2
.
Note:
- The length of both
num1
andnum2
is < 5100. - Both
num1
andnum2
contains only digits0-9
. - Both
num1
andnum2
does not contain any leading zero. - You must not use any built-in BigInteger library or convert the inputs to integer directly.
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;
}
434. Number of Segments in a String
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;
}
438. Find All Anagrams in a String
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++:
关键点:
- 哈哈
思路:
- 很暴力的方法,就是遍历,然后判断是不是p的anagrams,这个判断之前刷的题里面也有过,大致思想就是字母数量一致即可。
vector<int> findAnagrams(string s, string p) {
vector<int> 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<int> 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;
}
小结
哇,今天刷题还不错,昨天的代码就改了范围就跑通了,然后就是接下来的两题,写的代码看都不想看,不知道想的有多复杂,哎,可能还是分情况讨论能力不太强,看到别人的简洁代码我真是羡慕不已啊。
知识点
- clean code
- 分情况