1169. Invalid Transactions
给定一串交易记录,每条记录由[name, time, amount, city]组成,若t[i].amount>1000
或(t[i].name = t[j].name && t[i].city != t[j].amount && abs(t[i].time - t[j].time) <= 60)
,则该条记录是不合法的,输出所有不合法的记录
直接把每条string串分割成四个子串,两层for循环判断该条记录是否合法,不合法则保存
struct trans
{
string name, city;
int time, amount;
int strToint(string s)
{
int res = 0;
for (char c : s)
res = res * 10 + c - '0';
return res;
}
trans(vector<string> v)
{
name = v[0], city = v.back();
time = strToint(v[1]);
amount = strToint(v[2]);
}
};
class Solution {
public:
vector<string> invalidTransactions(vector<string>& transactions) {
if (transactions.size() == 0) return {};
vector<string> res;
vector<trans> vt;
for (auto i : transactions)
{
int pre = 0;
vector<string> tmp;
for (int j = 0; j < i.length(); ++j)
{
if (i[j] == ',')
{
tmp.emplace_back(i.substr(pre, j - pre));
pre = j + 1;
}
}
tmp.emplace_back(i.substr(pre));
vt.emplace_back(trans(tmp));
}
for (int i = 0; i < vt.size(); ++i)
{
if (vt[i].amount > 1000)
{
trans t = vt[i];
string str = t.name + "," + to_string(t.time) + "," + to_string(t.amount) + "," + t.city;
res.emplace_back(str);
}
else
{
int valid = 1;
for (int j = 0; j < vt.size(); ++j)
{
if (i == j) continue;
if (vt[i].name == vt[j].name && vt[i].city != vt[j].city && abs(vt[i].time - vt[j].time) <= 60)
{
valid = 0;
break;
}
}
if (valid == 0)
{
trans t = vt[i];
string str = t.name + "," + to_string(t.time) + "," + to_string(t.amount) + "," + t.city;
res.emplace_back(str);
}
}
}
return res;
}
};
1170. Compare Strings by Frequency of the Smallest Character
f(x)表示字符串x中最小字符在x中出现的次数,给定两个字符串数组,queries和words,对于每个queries[i],求出words中f(words[j]) > f(queries[i])
的字符串的个数
先用一个哈希表存储words中最小元素出现的次数,然后循环遍历queries的每个字符串,与哈希表循环比较
class Solution {
public:
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
int num = words.size();
vector<int> store(num, 0);
map<char, int> hash;
int pos = 0;
for(string s: words)
{
hash.clear();
for(char c: s) hash[c]++;
store[pos++] = hash.begin()->second;
}
vector<int> res;
for(string q: queries)
{
int com = 0;
hash.clear();
for(char c: q) hash[c]++;
com = hash.begin()->second;
int cnt = 0;
for(int i = 0; i < num; ++i)
if(store[i] > com) cnt++;
res.emplace_back(cnt);
}
return res;
}
};
1171. Remove Zero Sum Consecutive Nodes from Linked List
给定一个链表,将其中和为0的字串删除,返回删除后的链表
用一个哈希表存储到链表第i位的和,及其对应的节点,若节点已存在,则令上一节点的next指针指向当前节点的next节点
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* removeZeroSumSublists(ListNode* head) {
if (head == NULL) return head;
while (head)
{
if (head->val == 0) head = head->next;
else break;
}
ListNode* ln = new ListNode(-1);
ln->next = head;
int sum = 0;
unordered_map<int, ListNode*> um;
um[0] = ln;
while (head)
{
sum += head->val;
if(um.find(sum) != um.end())
{
ListNode* pre = um[sum];
ListNode* cur = pre;
int tmp = sum;
while(pre != cur)
{
pre = pre->next;
tmp += pre->val;
if(pre != cur) um.erase(tmp);
}
cur->next = head->next;
}
else
{
um[sum] = head;
}
head = head->next;
}
return ln->next;
}
};
1172. Dinner Plate Stacks
自定义一个类似于栈的数据结构,初始化为capacity,表示每个栈的容量,若当前栈容量大于capacity,则新建一个新的栈,并实现:
push(val)
,在所有栈的可容纳空间内push进一个val,比如若capacity为2, 第0个栈已满,第1个栈为空,则在第1个栈中push新的val,若第0个和第1个栈均只有一个(第0个栈有可能因为popatstack函数,导致没满)则在第0个栈push新的val
pop()
,将所有栈的最后一个元素pop出来
popAtStack(index)
,将第index个栈的最后一个元素pop出来
用哈希表存index对应的元素,set存当前可容纳栈的最小index
若之前未曾popatstack,则set为空;若操作过该函数,则set存放对应的index,若该index被push满,则将其删除,直至set为空,则继续push进所有栈的末尾
class DinnerPlates {
public:
DinnerPlates(int capacity) {
this->capacity = capacity;
cur = 0;
}
void push(int val) {
int ins = cur;
if (!pos.empty())
{
ins = *(pos.begin());
if (miv[ins].size() + 1 == capacity) pos.erase(pos.begin());
}
miv[ins].emplace_back(val);
if (ins == cur && miv[cur].size() == capacity) cur++;
}
int pop() {
if (cur == 0 && miv[0].size() == 0) return -1;
if (miv[cur].size() == 0) cur--;
int res = miv[cur].back();
miv[cur].pop_back();
return res;
}
int popAtStack(int index) {
if (miv[index].size() == 0) return -1;
int res = miv[index].back();
miv[index].pop_back();
pos.emplace(index);
return res;
}
private:
map<int, vector<int>> miv;
set<int> pos;
int cur, capacity;
};
/**
* Your DinnerPlates object will be instantiated and called as such:
* DinnerPlates* obj = new DinnerPlates(capacity);
* obj->push(val);
* int param_2 = obj->pop();
* int param_3 = obj->popAtStack(index);
*/