退役老人现在连leetcode都不会做了 = =

今天早上做了leetcode第三题题目看错了,加上比赛中间还在调投稿的实验,一心二用直接gg

总结下教训就是 本渣现在做题连题目都看不清就开始做。开始写题之前应当把样例过一遍,然后自己再造1-2个例子,然后再开始做

A题:统计素数的个数(素数筛或者sqrt(n)判断都可以),然后分别计算count!

class Solution {
public:
int numPrimeArrangements(int n) {
vector<int> has(n + 5, 0);
int cntPrime = 0;
const int MOD = 1e9 + 7;
for(int i = 2; i <= n; ++i) {
if(has[i] == 0) cntPrime ++;
for(int j = i * 2; j <= n; j += i) {
has[j] = 1;
}
}
int result = 1;
for(int i = 1; i <= n - cntPrime; ++i) {
result = 1ll * i * result % MOD;
}
for(int i = 1; i <= cntPrime; ++i) {
result = 1ll * i * result % MOD;
}
return result;
}
};

B题:滚动求和

class Solution {
public:
int dietPlanPerformance(vector<int>& calories, int k, int lower, int upper) {
long long sum = 0;
for(int i = 0; i < k; ++i) {
sum += calories[i];
}
int result = 0;
for(int i = k - 1, len = calories.size(); i < len; ++i) {
if(sum < lower) result --;
else if(sum > upper) result ++;
if(i != len - 1) {
sum -= calories[i - k + 1];
sum += calories[i + 1];
}
}
return result;
}
};

C题:我之前把题意看成整个字符串满足是回文,这个复杂很多。。。。

正确解法是判断区间的每种字母个数,我们知道偶数是可以直接配对的(放对称位置就好了),统计字母个数为奇数的,奇数的需要改一半就好了

class Solution {
private:
vector<int> has[26];
int get(int id, int fr, int to) {
if(fr > to) return 0;
if(fr == 0) return has[id][to];
else return has[id][to] - has[id][fr - 1];
}
public:
vector<bool> canMakePaliQueries(string s, vector<vector<int>>& queries) { int slen = s.length();
for(int i = 0; i < 26; ++i) has[i].clear(); /***statistic character count*****/
for(int i = 0; i < 26; ++i) {
for(int j = 0; j < slen; ++j) {
has[i].push_back(0);
}
}
for(int i = 0; i < slen; ++i) {
has[s[i] - 'a'][i] ++;
}
for(int i = 0; i < 26; ++i) {
for(int j = 1; j < slen; ++j) {
has[i][j] += has[i][j-1];
}
} vector<bool> result;
for(int i = 0, len = queries.size(); i < len; ++i) {
int left = queries[i][0]; int right = queries[i][1]; int k = queries[i][2]; int cnt = 0;
for(int j = 0; j < 26; ++j) {
int t1 = get(j, left, right);
if(t1 % 2 == 1) {
cnt ++;
}
}
cnt /= 2;
if(cnt <= k) result.push_back(true);
else result.push_back(false);
} return result;
}
};

D题:基本思想就是暴力查询,首先我们知道,字符串中的字母排列前后和出现次数都是无所谓的(除了puzzle的第一个字母的问题),这个是可以进行处理的

除此之外有两种加速的方法

方法1: 位压缩为26位,直接通过数位判断word是否是满足puzzle

方法2: 对word建立字典树,让puzzle在字典树中进行dfs,这里每个puzzle大约会有7!的查询复杂度

下面两个代码分别对应这两种方案,我是直接discuss的大佬里面爬下来了

class Solution {
vector<int> base;
void born(vector<string>& words){
for(auto& s: words){
set<char> tmp;
int bit = 0;
for(auto c:s){
tmp.insert(c);
bit = bit | (1<<(c-'a'));
}
if(tmp.size() >7)continue;
base.push_back(bit);
}
}
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
vector<int> ans;
born(words); for(auto& s: puzzles){
int num = 0;
int bit = 0;
for(auto c: s){
bit = bit | (1<<(c-'a'));
}
int firstBit = 1 << (s[0] - 'a');
for(auto v: base){
if((v & bit) == v && ((firstBit & v) == firstBit)){
num++;
}
}
ans.push_back(num);
} return ans;
}
};
const int ALPHABET_SIZE = 26;

/* The structure of a trie node */
struct TrieNode
{
struct TrieNode* children[ALPHABET_SIZE];
int count = 0;
}; /* Creates a new trie node and returns the pointer */
struct TrieNode* getNode()
{
struct TrieNode* newNode = new TrieNode; for(int i=0; i<ALPHABET_SIZE; i++)
newNode->children[i] = nullptr; newNode->count = 0; return newNode;
} /* Inserts the given string to the collection */
void insert(struct TrieNode* root, string str)
{
struct TrieNode* pCrawl = root; for(int i=0; i<str.length(); i++)
{
int index = str[i]-'a'; if(!pCrawl->children[index])
pCrawl->children[index] = getNode(); pCrawl = pCrawl->children[index];
} pCrawl->count = (pCrawl->count + 1);
} /* Returns the count of strings which are valid */
int search(struct TrieNode* root, string str, bool firstSeen, char firstLetter)
{
if(!root)
return 0; int count = 0; if(firstSeen)
count += root->count; for(int i=0; i<str.length(); i++)
{
int index = str[i] - 'a'; if(str[i] == firstLetter)
count += search(root->children[index], str, true, firstLetter);
else
count += search(root->children[index], str, firstSeen, firstLetter);
} return count;
} class Solution
{
public:
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles);
}; vector<int> Solution :: findNumOfValidWords(vector<string>& words, vector<string>& puzzles)
{
struct TrieNode* root = getNode(); for(auto str : words)
{
set<char> temp;
temp.insert(str.begin(), str.end()); string sorted = "";
for(auto ele : temp)
sorted += ele; insert(root, sorted);
} vector<int> count;
for(auto puzzle : puzzles)
{
char firstLetter = puzzle[0];
sort(puzzle.begin(), puzzle.end());
count.push_back(search(root, puzzle, false, firstLetter));
} return count; }

Leetcode Weekly Contest 152的更多相关文章

  1. LeetCode Weekly Contest 8

    LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...

  2. leetcode weekly contest 43

    leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...

  3. LeetCode Weekly Contest 23

    LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...

  4. Leetcode Weekly Contest 86

    Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...

  5. LeetCode Weekly Contest

    链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...

  6. 【LeetCode Weekly Contest 26 Q4】Split Array with Equal Sum

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/split-array-with-equal-sum/ ...

  7. 【LeetCode Weekly Contest 26 Q3】Friend Circles

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/friend-circles/ [题意] 告诉你任意两个 ...

  8. 【LeetCode Weekly Contest 26 Q2】Longest Uncommon Subsequence II

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

  9. 【LeetCode Weekly Contest 26 Q1】Longest Uncommon Subsequence I

    [题目链接]:https://leetcode.com/contest/leetcode-weekly-contest-26/problems/longest-uncommon-subsequence ...

随机推荐

  1. 初学者的linux - 基本知识篇

    1.Linux系统结构 Linux是一套免费使用和自由传播的类Unix操作系统,它是一种倒树结构. “/”就是系统的顶级目录,称作根目录,“/bin,/root,/home,/etc.."这 ...

  2. 【MySQL】服务无法启动(Mac)

    如图所示: 点击 Start MySQL Server 没反应-- 终端输入 mysql 命令时报错如下: ERROR 2002 (HY000): Can't connect to local MyS ...

  3. spark shuffle的写操作之准备工作

    前言 在前三篇文章中,spark 源码分析之十九 -- DAG的生成和Stage的划分 剖析了DAG的构建和Stage的划分,spark 源码分析之二十 -- Stage的提交 剖析了TaskSet任 ...

  4. jmh源码解析-整体架构

    我理解的jmh运行架构图 生成字节码,字节码负责维护测试的状态和调用被测试的方法 默认在fork的进程中进行测试,可以配置多个fork进程,以减少误差 通过线程池,提交每个迭代的测试任务,任务执行后, ...

  5. Mybatis学习笔记之---多表查询(1)

    Mybatis多表查询(1) (一)举例(用户和账户) 一个用户可以有多个账户 一个账户只能属于一个用户(多个账户也可以属于同一个用户) (二)步骤 1.建立两张表:用户表,账户表,让用户表和账户表之 ...

  6. 洛谷 P3195 [HNOI2008]玩具装箱TOY

    题意简述 有n个物体,第i个长度为ci 将n个物体分为若干组,每组必须连续 如果把i到j的物品分到一组,则该组长度为 \( j - i + \sum\limits_{k = i}^{j}ck \) 求 ...

  7. 普通Apache的安装与卸载

    Apache安装与卸载ctrl+F快捷查找 1.下载apache 64位解压 官网:http://httpd.apache.org/ 文件使用记事本或者sublime2.修改 打开apache目录下的 ...

  8. 【I'm Telling the Truth】【HDU - 3729】 【匈牙利算法,DFS】

    思路 题意:该题主要说几个同学分别说出自己的名次所处区间,最后输出可能存在的未说谎的人数及对应的学生编号,而且要求字典序最大. 思路:刚刚接触匈牙利算法,了解的还不太清楚,附一个专门讲解匈牙利算法的博 ...

  9. Linux 目录递归赋权,解决 Linux权限不够

    如你要操作一个目录下的文件时,系统提示 “权限不够”,可用以下方法解决. 如 test 文件目录. 1.用root账号登陆系统. 2.输入如下命令: chmod 777 test -R 这样访问.修改 ...

  10. 纯数据结构Java实现(0/11)(开篇)

    为嘛要写 本来按照我的风格,其实很不喜欢去写这些细节的东西,因为笔记上直接带过了. 本来按照我的风格,如果要写,那也是直接上来就干,根本不解释这些大纲,参考依据. 本来按照我的风格,不想太显山露水,但 ...