From e930ceb242d1dab64c3f8af850064904c4964fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=8B=8F=E9=B9=8F?= Date: Sun, 12 May 2019 19:03:54 +0800 Subject: [PATCH] Week_04 homework by speng975 --- Week_04/id_102/leetcode_242_102.cpp | 17 +++++++++ Week_04/id_102/leetcode_72_102.cpp | 27 +++++++++++++++ Week_04/id_102/leetcode_746_102.cpp | 14 ++++++++ Week_04/id_102/leetcode_784_012.cpp | 53 +++++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 Week_04/id_102/leetcode_242_102.cpp create mode 100644 Week_04/id_102/leetcode_72_102.cpp create mode 100644 Week_04/id_102/leetcode_746_102.cpp create mode 100644 Week_04/id_102/leetcode_784_012.cpp diff --git a/Week_04/id_102/leetcode_242_102.cpp b/Week_04/id_102/leetcode_242_102.cpp new file mode 100644 index 00000000..08c51aec --- /dev/null +++ b/Week_04/id_102/leetcode_242_102.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int majorityElement(vector& nums) { + /* 建立hash表 key-value */ + unordered_map 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; + } +}; diff --git a/Week_04/id_102/leetcode_72_102.cpp b/Week_04/id_102/leetcode_72_102.cpp new file mode 100644 index 00000000..48c52f03 --- /dev/null +++ b/Week_04/id_102/leetcode_72_102.cpp @@ -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]; + } +}; diff --git a/Week_04/id_102/leetcode_746_102.cpp b/Week_04/id_102/leetcode_746_102.cpp new file mode 100644 index 00000000..b5f176d8 --- /dev/null +++ b/Week_04/id_102/leetcode_746_102.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + int minCostClimbingStairs(vector& cost) { + vector 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]); + } +}; diff --git a/Week_04/id_102/leetcode_784_012.cpp b/Week_04/id_102/leetcode_784_012.cpp new file mode 100644 index 00000000..e4c8e540 --- /dev/null +++ b/Week_04/id_102/leetcode_784_012.cpp @@ -0,0 +1,53 @@ +/* +给定一个字符串S,通过将字符串S中的每个字母转变大小写,我们可以获得一个新的字符串。返回所有可能得到的字符串集合。 + +示例: +输入: S = "a1b2" +输出: ["a1b2", "a1B2", "A1b2", "A1B2"] + +输入: S = "3z4" +输出: ["3z4", "3Z4"] + +输入: S = "12345" +输出: ["12345"] +*/ + +class Solution { +public: + vector letterCasePermutation(string S) { + if(S.empty()) { + return {}; + } + + vector vec; + Permutation(0, vec, S); + return vec; + } + +private: + void Permutation(int idx, vector& 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; + } +}; + +