[日常刷題]leetcode D33
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
- 分情況
相關推薦
[日常刷題]leetcode D33
414. Third Maximum Number Given a non-empty array of integers, return the third maximum number in this array. If it does not exist
[日常刷題]leetcode第十四天
155. Min Stack Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push el
[日常刷題]leetcode D37
475. Heaters Winter is coming! Your first job during the contest is to design a standard heater with fixed warm radius to warm all
[日常刷題]leetcode D42
521. Longest Uncommon Subsequence I Given a group of two strings, you need to find the longest uncommon subsequence of this group
[日常刷題]leetcode D39
492. Construct the Rectangle For a web developer, it is very important to know how to design a web page’s size. So, given a specif
[日常刷題]leetcode D29
367. Valid Perfect Square Given a positive integer num, write a function which returns True if num is a perfect square else False.
[日常刷題]leetcode D34
441. Arranging Coins You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactl
LeetCode日常刷題110
110. 平衡二叉樹給定一個二叉樹,判斷它是否是高度平衡的二叉樹。本題中,一棵高度平衡二叉樹定義為:一個二叉樹每個節點 的左右兩個子樹的高度差的絕對值不超過1。示例 1:給定二叉樹 [3,9,20,null,null,15,7] 3 / \ 9 20
LeetCode日常刷題542、
542. 01 矩陣給定一個由 0 和 1 組成的矩陣,找出每個元素到最近的 0 的距離。兩個相鄰元素間的距離為 1 。示例 1: 輸入:0 0 0 0 1 0 0 0 0 輸出:0 0 0 0 1 0 0 0 0 示例 2: 輸入:0 0 0 0 1 0 1 1 1 輸出:
LeetCode日常刷題542
542. 01 矩陣給定一個由 0 和 1 組成的矩陣,找出每個元素到最近的 0 的距離。兩個相鄰元素間的距離為 1 。示例 1: 輸入:0 0 0 0 1 0 0 0 0 輸出:0 0 0 0 1 0
LeetCode日常刷題605
605. 種花問題假設你有一個很長的花壇,一部分地塊種植了花,另一部分卻沒有。可是,花卉不能種植在相鄰的地塊上,它們會爭奪水源,兩者都會死去。給定一個花壇(表示為一個數組包含0和1,其中0表示沒種植花,
LeetCode日常刷題504、682
504. 七進位制數給定一個整數,將其轉化為7進位制,並以字串形式輸出。示例 1:輸入: 100 輸出: "202" 示例 2:輸入: -7 輸出: "-10" 注意: 輸入範圍是 [-1e7, 1e7
LeetCode日常刷題
693. 交替位二進位制數給定一個正整數,檢查他是否為交替位二進位制數:換句話說,就是他的二進位制數相鄰的兩個位數永不相等。示例 1:輸入: 5 輸出: True 解釋: 5的二進位制數是: 101 示
Java日常刷題第十一天
選擇題 1.類 ABC 定義如下: 1 . public class ABC{ 2 . public double max( double a, double b) { } 3 . 4 . } 將以下哪個方法插入行 3 是不合法的。(B) A.pu
Java日常刷題第十天
選擇題 1.在使用super和this關鍵字時,在子類構造方法中使用super()顯示呼叫父類的構造方法,super()必須寫在子類構造方法的第一行,否則編譯不通過. 解析:1)呼叫super()必須寫在子類構造方法的第一行,否則編譯不通過。每個子類構造方法的第一條語句,都是隱含地呼叫s
Java日常刷題第九天
1.java7後關鍵字 switch 支不支援字串作為條件:(錯) 解析:在Java7之前,Switch支援的條件型別: byte,short,chart,int,enum以及基本型別的封裝類,在Java7之後,開始支援字串型別。 2.佇列(Queue)是先進先出的。(對) 3.This呼叫
Java日常刷題第七天
選擇題 1.在類Tester中定義方法如下, public double max(int x, int y) { // 省略 } 則在該類中定義如下哪個方法頭是對上述方法的過載(Overload)?(B) A.public int max(int a, int b) {} B.p
Java日常刷題第六天
一、選擇題 1.下列關於Java語言的特點,描述錯誤的是(C。Java是面向過程的程式語言) A。Java是跨平臺的程式語言 B。Java支援分散式計算 C。Java是面向(物件)的程式語言 D。Java支援多執行緒 2.下列那個類的宣告是正確的?(D) A。abstract final
【日常刷題】NOIP練習題題解
NOIP練習題題解 1.小X與位運算 2.小x與機器人 3.賽車 4.whatbase 5.height 6.最優分解 7.angry 1.小X與位運算 題目描述 自從上次小X
【日常刷題】[SCOI2005]掃雷(多維動態規劃)
[SCOI2005]掃雷 我們對於這道題,我們可以採用DP的方法來解決。 我們設f[i][0/1][0/1][0/1]表示滿足前i個條件限制,第i-1,i,i+1分別放(1)和不放(0)的方案數。 對於a[i](第二列的數字),我們採用分類討論的方法來進行狀態轉移。 當a[i