Leetcode 记录(201~300)
实习面试前再完成100题,争取能匀速解释清楚题
204. Count Primes
素数筛
class Solution {
public:
int countPrimes(int n) {
if(n<=) return ;
n=n-;
vector<int> prime(n+,);
for(int i=;i<=n;i++)
{
if(!prime[i])prime[++prime[]]=i;
for(int j=;j<=prime[]&&prime[j]<=n/i;j++)
{
prime[prime[j]*i]=;
if(i%prime[j]==) break;
}
}
return prime[];
}
};
207 210 无向图判断环
topo排序
class Solution {
public:
vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites) {
int n=prerequisites.size();
vector<vector<int> > g(numCourses+);
vector<int> in(numCourses+,);
vector<int> ans;
vector<int> ans1;
pair<int,int> w;
for(int i=;i<n;i++){
w=prerequisites[i];
g[w.second].push_back(w.first);
in[w.first]++;
}
queue<int> q;
for(int i=;i<numCourses;i++){
if(in[i]==) q.push(i);
}
int now;
while(!q.empty()){
now=q.front();
q.pop();
ans.push_back(now);
for(int i=;i<g[now].size();i++){
in[g[now][i]]--;
if(in[g[now][i]]==) q.push(g[now][i]);
}
}
for(int i=;i<numCourses;i++){
if(in[i]!=) return NULL;
}
return ans;
}
};
208 Tire树,直接抄了一份模板
class TrieNode
{
public:
TrieNode *next[];
bool is_word; // Initialize your data structure here.
TrieNode(bool b = false)
{
memset(next, , sizeof(next));
is_word = b;
}
}; class Trie
{
TrieNode *root;
public:
Trie()
{
root = new TrieNode();
} // Inserts a word into the trie.
void insert(string s)
{
TrieNode *p = root;
for(int i = ; i < s.size(); ++ i)
{
if(p -> next[s[i] - 'a'] == NULL)
p -> next[s[i] - 'a'] = new TrieNode();
p = p -> next[s[i] - 'a'];
}
p -> is_word = true;
} // Returns if the word is in the trie.
bool search(string key)
{
TrieNode *p = find(key);
return p != NULL && p -> is_word;
} // Returns if there is any word in the trie
// that starts with the given prefix.
bool startsWith(string prefix)
{
return find(prefix) != NULL;
} private:
TrieNode* find(string key)
{
TrieNode *p = root;
for(int i = ; i < key.size() && p != NULL; ++ i)
p = p -> next[key[i] - 'a'];
return p;
}
};
Leetcode 记录(201~300)的更多相关文章
- 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)
[LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...
- Leetcode 记录(101~200)
Now, I want to just use English to explain the problem, it's about two month before the interview, s ...
- LeetCode记录(1)——Array
1.Two Sum naive 4.Median of Two Sorted Arrays 找两个已排序数组的中位数 直接数可以过,但很蠢,O(m+n)时间 class Solution { publ ...
- Leetcode 记录(1~100)
5.回文串 几种方法: 暴力:枚举每一个字串,判断是否为回文串,复杂度O(n^3),暴力月莫不可取 dp:区间dp思想,O(n^2) 中心扩展:找每一个字符,然后往两边扩展,O(n^2) manach ...
- LeetCode记录之9——Palindrome Number
LeetCode真是个好东西,本来闲了一下午不想看书,感觉太荒废时间了就来刷一道题.能力有限,先把easy的题目给刷完. Determine whether an integer is a palin ...
- LeetCode记录之7——Reverse Integer
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The ...
- LeetCode(201) Bitwise AND of Numbers Range
题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numb ...
- Leetcode 周赛#201 题解
1545 找出第N个二进制字符串的第K位 #分治 题目链接 题意 给定正整数\(n(\leq 20)\)与\(k\),二进制串\(S_n\)形成规则有: \(S_1 = "0"\) ...
- c++刷leetcode记录
#include<iostream> #include<sstream> #include<vector> std::vector<int> split ...
随机推荐
- 柳叶刀重磅出击!全外显子测序在胎儿结构异常的评估Whole-exome sequencing in the evaluation of fetal structural anomalies: a prospective cohort study
柳叶刀发表的文献解读:Whole-exome sequencing in the evaluation of fetal structural anomalies: a prospective coh ...
- input输入框自动获取焦点
只要在该input标签后添加autofocus="autofocus"即可 代码实例: <html> <head></head> <bod ...
- Mybatis的应用2 使用mybits+SpringBoot完成第一个查询的demo(随后加增加,更新,删除)
首先在mapper下面新建一个mysql.xml mysql.xml <?xml version="1.0" encoding="UTF-8" ?> ...
- ECShop安装及错误修复
ecshop 商城的安装及出现错误的解决 听语音 | 浏览:600 | 更新:2016-03-04 16:02 | 标签:错误 安装 电子商务 1 2 3 4 5 6 7 分步阅读 昨天第一次安装ec ...
- C# 数独算法——LINQ+委托
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Sing ...
- 一文学会matplotlib
matplotlib基础 “““ 假设一天中每隔两个小时(range(2,26,2))的气温(℃)分别是[15,13,14.5,17,20,25,26,26,27,22,18,15] 用matplot ...
- Windows 环境下的 protoc 安装(转)
如果是为了编译hadoop2.8.0源码,必须使用2.5.0版本的protobuf,安装方法同下 1. 下载需要的安装包:https://github.com/google/protobuf/rele ...
- saltstack主机管理项目:主机管理项目需求分析(一)
1.场景: 我现在又一台裸机要实现一下任务 2.配置管理: 1.装上nginx,mysql 2.nginx用我指定的配置文件 3.mysql用户 4.设置一个默认的数据库访问权限 5.启动mysql ...
- linux中使用gdb调试程序
ref:https://blog.csdn.net/tenfyguo/article/details/8159176 一,什么是coredump 我们经常听到大家说到程序core掉了,需要定位解决, ...
- [Luogu P3295][SCOI 2016]萌萌哒
先说下暴力做法,如果[l1,r1]和[l2,r2]子串相等等价于两个区间内每个数对应相等.那么可以用并查集暴力维护,把对应相等的数的位置维护到同一个集合里去,最后答案其实就是把每个集合可以放的数个数乘 ...