Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Week_04/id_102/leetcode_242_102.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
public:
int majorityElement(vector<int>& nums) {
/* 建立hash表 key-value */
unordered_map<int, int> map;
int ret = 0;
int len = nums.size();
for (int i = 0; i < len; i++) {
map[nums[i]]++;
if (map[nums[i]]>len/2) {
ret = nums[i];
break;
}
}
return ret;
}
};
27 changes: 27 additions & 0 deletions Week_04/id_102/leetcode_72_102.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class Solution {
public:
int minDistance(string word1, string word2) {
int row = word1.size();
int column = word2.size();

int f[row+1][column+1];
for (int i = 0; i <= row; i++) {
f[i][0] = i;
}

for (int j = 0; j <=column; j++) {
f[0][j] = j;
}

for (int i = 1; i <= row; i++) {
for (int j = 1; j <= column; j++) {
if (word1[i-1] == word2[j-1]) {
f[i][j] = f[i-1][j-1];
} else {
f[i][j] = 1 + min(f[i-1][j-1], min(f[i][j-1], f[i-1][j]));
}
}
}
return f[row][column];
}
};
14 changes: 14 additions & 0 deletions Week_04/id_102/leetcode_746_102.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
public:
int minCostClimbingStairs(vector<int>& cost) {
vector<int> dp(cost.size(), 0);
dp[0] = cost[0];
dp[1] = cost[1];

for (int i = 2; i < cost.size(); i++) {
dp[i] = min(dp[i-1], dp[i-2]) + cost[i];
}

return min(dp[dp.size()-1], dp[dp.size()-2]);
}
};
53 changes: 53 additions & 0 deletions Week_04/id_102/leetcode_784_012.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。

示例:
输入: S = "a1b2"
输出: ["a1b2", "a1B2", "A1b2", "A1B2"]

输入: S = "3z4"
输出: ["3z4", "3Z4"]

输入: S = "12345"
输出: ["12345"]
*/

class Solution {
public:
vector<string> letterCasePermutation(string S) {
if(S.empty()) {
return {};
}

vector<string> vec;
Permutation(0, vec, S);
return vec;
}

private:
void Permutation(int idx, vector<string>& ans, string S) {
if (idx == S.size()) {
ans.push_back(S);
return;
}

if (S[idx] >= '0' && S[idx] <= '9') {
Permutation(idx + 1, ans, S);
return;
}

if (S[idx] >= 'A' && S[idx] <= 'Z') {
S[idx] = tolower(S[idx]);
}
Permutation(idx + 1, ans, S);

if (S[idx] >= 'a' && S[idx] <= 'z') {
S[idx] = toupper(S[idx]);
}
Permutation(idx + 1, ans, S);

return;
}
};