0.引言
題目:
- 242.有效的字母異位詞
- 349. 兩個數組的交集
- 202. 快樂數
- 1. 兩數之和
1.有效的字母異位詞
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (65.83%) | 736 | - |
給定兩個字符串 *s*
和 *t*
,編寫一個函數來判斷 *t*
是否是 *s*
的字母異位詞。
注意:若 *s*
和 *t*
中每個字符出現的次數都相同,則稱 *s*
和 *t*
互為字母異位詞。
示例 1:
輸入: s = "anagram", t = "nagaram"
輸出: true
示例 2:
輸入: s = "rat", t = "car"
輸出: false
提示:
1 <= s.length, t.length <= 5 * 10<sup>4</sup>
-
s
和t
僅包含小寫字母
**進階: **如果輸入字符串包含 unicode 字符怎么辦?你能否調整你的解法來應對這種情況?
1.1.自己想法及代碼實現
這個題目還是比較簡單,直接打表:
/*
* @lc app=leetcode.cn id=242 lang=cpp
*
* [242] 有效的字母異位詞
*/
// @lc code=start
class Solution {
public:
bool isAnagram(string s, string t) {
if (s == t) return true;
std::vector<int> record(26, 0);
for (auto& ch : s) {
record[ch - 'a']++;
}
for (auto& ch : t) {
record[ch - 'a']--;
}
// std::cout << record[0] << ", " << record[1];
for (auto& i : record) {
if (i != 0) return false;
}
return true;
}
};
// @lc code=end
1.2.參考思想及代碼實現
2.兩個數組的交集
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (74.33%) | 727 | - |
給定兩個數組 nums1
和 nums2
,返回 它們的交集 。輸出結果中的每個元素一定是 唯一 的。我們可以 不考慮輸出結果的順序 。
示例 1:
輸入:nums1 = [1,2,2,1], nums2 = [2,2]
輸出:[2]
示例 2:
輸入:nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出:[9,4]
解釋:[4,9] 也是可通過的
提示:
1 <= nums1.length, nums2.length <= 1000
0 <= nums1[i], nums2[i] <= 1000
2.1.自己想法及代碼實現
- 顯然是使用set求解了
/*
* @lc app=leetcode.cn id=349 lang=cpp
*
* [349] 兩個數組的交集
*/
// @lc code=start
class Solution {
public:
vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
std::set<int> check_set, res_set;
for (auto& num : nums1) {
check_set.insert(num);
}
for (auto& num : nums2) {
if (check_set.find(num) != check_set.end()) {
res_set.insert(num);
}
}
return std::vector(res_set.begin(), res_set.end());
}
};
// @lc code=end
3.快樂數
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (63.29%) | 1228 | - |
編寫一個算法來判斷一個數 n
是不是快樂數。
「快樂數」 定義為:
- 對于一個正整數,每一次將該數替換為它每個位置上的數字的平方和。
- 然后重復這個過程直到這個數變為 1,也可能是 無限循環 但始終變不到 1。
- 如果這個過程 結果為 1,那么這個數就是快樂數。
如果 n
是 快樂數 就返回 true
;不是,則返回 false
。
示例 1:
輸入:n = 19
輸出:true
解釋:
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1
示例 2:
輸入:n = 2
輸出:false
提示:
1 <= n <= 2<sup>31</sup> - 1
3.1.自己想法及代碼實現
- 實現題目敘述的計算
- 使用打表來判斷是否進入死循環
/*
* @lc app=leetcode.cn id=202 lang=cpp
*
* [202] 快樂數
*/
// @lc code=start
class Solution {
public:
bool isHappy(int n) {
int num = n;
std::set<int> check_set;
while (true) {
num = sum_of_squares(num);
if (check_set.find(num) != check_set.end()) {
return false;
} else {
check_set.insert(num);
}
if (num == 1) return true;
}
}
private:
int sum_of_squares(int n) {
int res = 0;
while (n) {
int num = n % 10;
res += num * num;
n /= 10;
}
return res;
}
};
// @lc code=end
4.兩數之和
Category | Difficulty | Likes | Dislikes |
---|---|---|---|
algorithms | Easy (52.88%) | 16466 | - |
給定一個整數數組 nums
和一個整數目標值 target
,請你在該數組中找出 **和為目標值 **target
的那 兩個 整數,并返回它們的數組下標。
你可以假設每種輸入只會對應一個答案。但是,數組中同一個元素在答案里不能重復出現。
你可以按任意順序返回答案。
示例 1:
輸入:nums = [2,7,11,15], target = 9
輸出:[0,1]
解釋:因為 nums[0] + nums[1] == 9 ,返回 [0, 1] 。
示例 2:
輸入:nums = [3,2,4], target = 6
輸出:[1,2]
示例 3:
輸入:nums = [3,3], target = 6
輸出:[0,1]
提示:
2 <= nums.length <= 10<sup>4</sup>
-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup>
-10<sup>9</sup> <= target <= 10<sup>9</sup>
- 只會存在一個有效答案
進階:你可以想出一個時間復雜度小于 O(n<sup>2</sup>)
的算法嗎?
4.1.自己想法及代碼實現
- 首先想到的當然是兩層循環玻璃破解,但這種方法兩數之和必然會超范圍
- 使用unordered_map管理數據,利用作差法打表,target - nums[i]
/*
* @lc app=leetcode.cn id=1 lang=cpp
*
* [1] 兩數之和
*/
// @lc code=start
class Solution {
public:
vector<int> twoSum(vector<int>& nums, int target) {
std::vector<int> res;
// map key = val, map val = index
std::unordered_map<int, int> hash_map;
for (int i = 0; i < nums.size(); ++i) {
if (hash_map.find(nums[i]) != hash_map.end()) {
continue;
} else {
hash_map[nums[i]] = i;
}
}
for (int i = 0; i < nums.size(); ++i) {
auto map_element = hash_map.find(target - nums[i]);
if (map_element != hash_map.end()) {
if (i == map_element->second) continue;
return std::vector<int>{i, map_element->second};
}
}
return {};
}
};
// @lc code=end
4.總結
- C++數據結構的使用