Leetcode(C++)
題目 : https://leetcode.com/problems/reverse-words-in-a-string-iii/ 。
想法 :
遇到空格或是截尾就翻轉那個單字。
時間複雜度 : O(n)。
程式碼 :
class Solution {
public:
string reverseWords(string s) {
int l=s.size(), first=0, last=0;
for(int i=0 ; i<=l ; i++){
if(s[i] == ' ' || i == l){
last=i-1;
cout << first << " " << last << endl;
for(int j=0 ; j<=(last-first)/2 ; j++){
char tmp = s[first+j];
s[first+j] = s[last-j];
s[last-j] = tmp;
}
first=i+1;
}
}
return s;
}
};
題目 : https://leetcode.com/problems/richest-customer-wealth/ 。 想法 : 語法。 時間複雜度 : O(n*m)。 程式碼 : (JAVA)
Feb 5, 2022題目 : https://leetcode.com/problems/coin-change-2/ 。 想法 : 無窮背包問題。 時間複雜度 : O(n*m)。 程式碼 : (JAVA)
Feb 5, 2022題目 : https://leetcode.com/problems/integer-break/ 。 想法 : 規律就在2跟3身上(?),把一個數字分解成2跟3的總和就好。 時間複雜度 : O(1)。 程式碼 : (JAVA)
Feb 5, 2022題目 : https://leetcode.com/problems/longest-common-subsequence/ 。 想法 : LCS。 if(str[i] == str[j]) dp[i][j]=dp[i-1][j-1]+1; else dp[i][j]=max(dp[i-1][j], dp[i][j-1]);
Feb 5, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up