LeetCode代码
LeetCode 242. 有效的字母异位词
#include <iostream>
#include <set>
#include <string>
using namespace std;
class Solution {
public:
bool isAnagram(string s, string t) {
multiset<char> c_set;
for(int i=0; i<s.size(); i++)
c_set.insert(s[i]);
for(int i=0; i<t.size(); i++) {
multiset<char>::iterator it = c_set.find(t[i]);
if(it != c_set.end()) {
c_set.erase(it);
} else {
return false;
}
}
return c_set.empty();
}
};
int main() {
Solution s;
bool b = s.isAnagram("ab", "a");
cout << b << endl;
// multiset<char> c_set;
// c_set.insert('c');
// c_set.insert('c');
// c_set.insert('b');
// cout << c_set.count('c') << endl;
// c_set.erase('c');
// cout << c_set.count('c') << endl;
return 0;
}
LeetCode 202. 快乐数
#include <iostream>
#include <unordered_set>
#include <cmath>
using namespace std;
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> sumSet;
long long sum = 0;
while (sum != 1) {
sum = 0;
while (n) { //求各位平方和
sum += pow(n % 10, 2);
n /= 10;
}
//cout << sum << endl;
if(sumSet.count(sum))
return false;
sumSet.insert(n = sum);
}
return true;
}
};
int main() {
Solution s;
cout << s.isHappy(7) << endl;
return 0;
}
LeetCode 290. 单词规律
#include <cstdio>
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
using namespace std;
class Solution {
private:
vector<string> split(string s) {
vector<string> resultVector;
string tempStr = "";
for (int i = 0; i < s.length(); i++) {
if (i == s.length() - 1 && s[i] != ' ') {
tempStr+= s[i];
resultVector.push_back(tempStr);
}
if (s[i] == ' ' && tempStr != "") {
resultVector.push_back(tempStr);
tempStr = "";
}
if (s[i] != ' ')
tempStr += s[i];
}
return resultVector;
}
public:
bool wordPattern(string pattern, string str) {
//两个map用于建立双向映射
unordered_map<char, string> k_map;
unordered_map<string, char> v_map;
//分割字符串
vector<string> splitVector = this->split(str);
//判断长度是否一样
if(splitVector.size() != pattern.length())
return false;
//使用pattern遍历
for (int i = 0; i < pattern.size(); i++) {
//char不在k_map中
if(k_map.count(pattern[i]) == 0) {
if(v_map.count(splitVector[i]) == 0) {
//该字符串没有被映射过
k_map.insert(pair<char, string>(pattern[i], splitVector[i]));
v_map.insert(pair<string, char>(splitVector[i], pattern[i]));
} else {
//说明该字符串已经被其他字符映射过了
return false;
}
} else {
//char在k_map中
if(k_map[pattern[i]] != splitVector[i])
return false;
}
}
return true;
}
};
int main() {
// Solution s;
// vector<string> v = s.split("Hello TangJiao. And I love you");
//
// for (auto s : v) {
// cout << s << endl;
// }
Solution s;
string pattern = "abba", str = "dog dog dog dog" ;
cout << s.wordPattern(pattern, str) << endl;
return 0;
}
LeetCode 451. 根据字符出现频率排序
#include <cstdio>
#include <iostream>
#include <unordered_map>
#include <vector>
#include <algorithm>
using namespace std;
bool comp(const pair<char, int> &a, const pair<char, int> &b) {
return a.second > b.second;
}
class Solution {
public:
string frequencySort(string s) {
unordered_map<char, int> hmap;
for (char c : s)
hmap[c] += 1;
vector<pair<char, int> > vec(hmap.begin(), hmap.end());
sort(vec.begin(), vec.end(), comp);
string result;
for (auto l : vec) {
for (int i = 0; i < l.second; i++)
result += l.first;
}
return result;
}
};
int main() {
Solution s;
cout << s.frequencySort("wwsssaaaadddqwer") << endl;
return 0;
}
LeetCode 454. 四数相加 II
#include <iostream>
#include <unordered_map>
using namespace std;
class Solution {
public:
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
int cnt = 0;
unordered_map<int, int> record;
for(int i : C)
for (int j : D)
record[ i + j ] += 1;
for(int i : A)
for(int j : B)
if( record.find(-(i + j)) != record.end())
cnt += record[-(i + j)];
return cnt;
}
};
int main_454() {
return 0;
}
LeetCode 49. 字母异位词分组
#include <iostream>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
class Solution {
public:
vector<vector<string> > groupAnagrams(vector<string>& strs) {
unordered_map<string, int> hmap;
vector<vector<string> > result;
int rows = 0;
for(string s : strs) {
string scopy = s;
sort(scopy.begin(), scopy.end());
if(hmap.count(scopy) == 0) {
hmap[scopy] = rows;
result.push_back(vector<string>());
result[rows++].push_back(s);
} else {
result[ hmap[scopy] ].push_back(s);
}
}
return result;
}
};
int main() {
vector<string> dataIn;
dataIn.push_back("eat");
dataIn.push_back("tea");
dataIn.push_back("tan");
dataIn.push_back("ate");
dataIn.push_back("nat");
dataIn.push_back("bat");
Solution s;
vector<vector<string> > rs = s.groupAnagrams(dataIn);
for(vector<string> vec : rs) {
for(string s : vec)
cout << s << " ";
cout << endl;
}
return 0;
}
LeetCode 447. 回旋镖的数量 👀
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;
class Solution {
private:
int dis(const vector<int> pa, const vector<int> pb) {
return (pa[0] - pb[0]) * (pa[0] - pb[0]) +
(pa[1] - pb[1]) * (pa[1] - pb[1]);
}
public:
int numberOfBoomerangs(vector< vector<int> > &points) {
int res = 0;
for (int i = 0; i < points.size(); i++) {
unordered_map<int, int> record;
for (int j = 0; j < points.size(); j++)
if (j != i)
record[ dis(points[i], points[j]) ]++;
for(unordered_map<int, int>::iterator iter = record.begin(); iter != record.end(); iter++) {
if(iter->second >= 2)
res += (iter->second) * (iter->second - 1);
}
}
return res;
}
};
LeetCode 219. 存在重复元素 II
#include <iostream>
#include <vector>
#include <unordered_set>
using namespace std;
class Solution {
public:
bool containsNearbyDuplicate(vector<int> &nums, int k) {
unordered_set<int> record;
for (int i = 0; i < nums.size(); i++) {
if (record.find(nums[i]) != record.end())
return true;
record.insert(nums[i]);
if (record.size() == k + 1)
record.erase(nums[i - k]);
}
return false;
}
};
LeetCode 220. 存在重复元素 III
#include <iostream>
#include <set>
#include <vector>
using namespace std;
typedef long long LL;
class Solution {
public:
bool containsNearbyAlmostDuplicate(vector<int> &nums, int k, int t) {
set<long long> record;
for (int i = 0; i < nums.size(); i++) {
set<long long>::iterator fd = record.lower_bound((long long)nums[i] - (long long)t);
if (fd != record.end() && *fd <= (long long)nums[i] + (long long)t)
return true;
record.insert(nums[i]);
if (record.size() == k + 1)
record.erase(nums[i - k]);
}
return false;
}
};
LeetCode 206. 反转链表
#include <iostream>
#include <stdlib.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode* reverseList(ListNode *head) {
ListNode* pre = NULL;
ListNode* cur = head;
ListNode* nex = NULL;
if(head == NULL)
return NULL;
else nex = cur->next;
while (1) {
cur->next = pre;
if(nex == NULL) break;
pre = cur;
cur = nex;
nex = nex->next;
}
return cur;
}
};
ListNode* create(int n) {
ListNode* const head = (ListNode*) malloc(sizeof(ListNode));
head->val = 1;
ListNode *p = head;
ListNode *q;
for (int i = 2; i <= n; i++) {
q = (ListNode*) malloc(sizeof(ListNode));
q->val = i;
cout << q->val << endl;
p->next = q;
p = q;
}
p->next = NULL;
cout << "创建完成" << endl;
return head;
}
void printLink(ListNode* head) {
ListNode* p = head;
while(p != NULL) {
cout << p->val << "->";
p = p->next;
}
cout << endl;
}
int main() {
Solution s;
ListNode* Link2 = s.reverseList(NULL);
printLink(Link2);
return 0;
}
LeetCode 203. 移除链表元素
#include <iostream>
#include <stdlib.h>
using namespace std;
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode* removeElements(ListNode *head, int val) {
if(head == NULL) return NULL;
ListNode* pre = NULL;
ListNode* cur = head;
while(cur != NULL) {
if(cur->val == val) {
if(pre == NULL) {
cur = cur->next;
free(head);
head = cur;
} else {
ListNode* t = cur;
cur = cur->next;
pre->next = cur;
free(t);
}
} else {
pre = cur;
cur = cur->next;
}
}
return head;
}
};
ListNode* create(int n) {
ListNode *const head = (ListNode*) malloc(sizeof(ListNode));
head->val = 1;
ListNode *p = head;
ListNode *q;
for (int i = 2; i <= n; i++) {
q = (ListNode*) malloc(sizeof(ListNode));
q->val = i;
p->next = q;
p = q;
}
p->next = NULL;
cout << "创建完成" << endl;
return head;
}
void printLink(ListNode *head) {
ListNode *p = head;
while (p != NULL) {
cout << p->val << "->";
p = p->next;
}
cout << endl;
}
int main203() {
ListNode *linkList = create(10);
printLink(linkList);
Solution s;
linkList = s.removeElements(linkList, 1);
printLink(linkList);
return 0;
}
LeetCode 24. 两两交换链表中的节点
class Solution {
public:
ListNode* swapPairs(ListNode* head) {
ListNode* dummyHead = new ListNode(0);
dummyHead->next = head;
ListNode* p = dummyHead;
while (p->next && p->next->next) {
ListNode* node1 = p->next;
ListNode* node2 = node1->next;
ListNode* nex = node2->next;
node2->next = node1;
node1->next = nex;
p->next = node2;
p = node1;
}
head = dummyHead->next;
delete dummyHead;
return head;
}
};