实习面试前再完成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)的更多相关文章

  1. 【LeetCode】201. Bitwise AND of Numbers Range 解题报告(Python)

    [LeetCode]201. Bitwise AND of Numbers Range 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/prob ...

  2. Leetcode 记录(101~200)

    Now, I want to just use English to explain the problem, it's about two month before the interview, s ...

  3. LeetCode记录(1)——Array

    1.Two Sum naive 4.Median of Two Sorted Arrays 找两个已排序数组的中位数 直接数可以过,但很蠢,O(m+n)时间 class Solution { publ ...

  4. Leetcode 记录(1~100)

    5.回文串 几种方法: 暴力:枚举每一个字串,判断是否为回文串,复杂度O(n^3),暴力月莫不可取 dp:区间dp思想,O(n^2) 中心扩展:找每一个字符,然后往两边扩展,O(n^2) manach ...

  5. LeetCode记录之9——Palindrome Number

    LeetCode真是个好东西,本来闲了一下午不想看书,感觉太荒废时间了就来刷一道题.能力有限,先把easy的题目给刷完. Determine whether an integer is a palin ...

  6. LeetCode记录之7——Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 Note:The ...

  7. LeetCode(201) Bitwise AND of Numbers Range

    题目 Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numb ...

  8. Leetcode 周赛#201 题解

    1545 找出第N个二进制字符串的第K位 #分治 题目链接 题意 给定正整数\(n(\leq 20)\)与\(k\),二进制串\(S_n\)形成规则有: \(S_1 = "0"\) ...

  9. c++刷leetcode记录

    #include<iostream> #include<sstream> #include<vector> std::vector<int> split ...

随机推荐

  1. Harbo1.5.2离线搭建

    环境说明 操作系统版本:Centos7.5 docker版本:docker-ce 17.03.2 harbor版本:v1.5.2 docker-compose:  1.22.0 基础环境搭建 系统优化 ...

  2. java 为什么wait(),notify(),notifyAll()必须在同步(Synchronized)方法/代码块中调用?

    wait()作用:该方法用来将当前线程置入休眠状态,直到接到通知或被中断为止.条件:在调用wait()之前,线程必须要获得该对象的对象级别锁,即只能在同步方法或同步块中调用wait()方法.进入wai ...

  3. PLSQL Developer 远程连接Oracle数据库

    下载.安装.激活PLSQL Developer,本例使用 PLSQL Developer 13 (64 bit). 下载Oracle即时客户端 Instant Client,本例使用11gR2 链接: ...

  4. [物理学与PDEs]第3章习题7 快、慢及Alfv\'en 特征速度的比较

    证明: 当 $H_1\neq 0$ 及 $H_2^2+H_3^2\neq 0$ 时, 快.慢及 Alfv\'en 特征速度 $C_f$, $C_s$ 及 $C_a$ 满足 $$\bex 0<C_ ...

  5. mysql 以数组的形式插入更新表

    在项目中用php+mysql,需要插入数据,但是数据库表字段又很长,如果用常规的insert into table (c) values ('dd')的话,将是很长的sql语句,而且一不小心,就弄错了 ...

  6. 深入浅出UE4网络

    UE4中的官方文档结构比较混乱,且有部分错误,不方便学习.笔者试图通过本文,整理出一篇关于UE4网络的文章,方便朋友们对UE4中的网络同步部分的认识,并有进一步理解.如有讲得不清楚明白的地方,还望批评 ...

  7. 409 javascript if and while表达式

    数组定义.特点.运算符:算术运算 ++ --(自减 自加) 赋值运算发 =比较:!= == === 逻辑运算 有 && || ! 正则表达式 修饰符 i:用来表示 g:很少演示(在第一 ...

  8. Docker: repository, image, container

    1. 查看image: docker images 2. 查看信息: docker info 3. 搜索image: docker search [image_name], 比如: docker se ...

  9. Laravel 多数据库配置及查询操作

    laravel文档好像没有写得很详细 https://docs.golaravel.com/docs/5.3/database/ Using Multiple Database Connections ...

  10. SpringBoot系列二:SpringBoot自动配置原理

    主程序类的注解 @SpringBootApplication 注解,它其实是个组合注解,源码如下: @Target({ElementType.TYPE}) @Retention(RetentionPo ...