实习面试前再完成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. 集合的遍历以及在Spring中的注入

    (一)遍历 list:foreach循环 set:foreach循环 map:先用set集合得到所有key值,然后循环key得到所有vale Map<String,String> map ...

  2. IIC协议理解(转)

    目录 IIC协议理解(转) 个人小结记录 (记一下这个就够了) 以下为转载记录 概述 概述 输出级 主设备与从设备 速率 时序 空闲状态 起始位与停止位 数据的有效性 数据的传送 工作过程 主设备向从 ...

  3. 我对SAP Business One 项目实施的理解

    一.什么是SAP: 大家都知道ERP是什么,ERP是企业资源计划管理系统.是指建立在信息技术基础上,集信息技术与先进管理思想于一身,以系统化的管理思想,为企业员工及决策层提供决策手段的管理平台.那么问 ...

  4. 我的 Erdos 数是 4

    我的 Erdos 数是 4. 呵呵. 图书馆开通了一个月的 mathscinet 数据库查询. 本来想买个 pde 的最新进展, 结果不能查询, 就算了.

  5. Dijkstra算法的C++实现

    Dijkstra算法是在图中寻找两顶点最短路径的算法.   下面以下图有向图为例,说明其基本思想. 上图为转化为邻接矩阵存储:     现在我要寻找1点到其他点的最短距离以及路径: a)1点到各点的距 ...

  6. mapState

    store.js import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ ...

  7. Web从入门到放弃<6>

     <1> Canvas. 1,灰度图: js: function showAsGray() { var imgNode = document.getElementById('img'); ...

  8. crowdstrike提供的应急响应工具

    下载链接 https://www.crowdstrike.com/resources/community-tools/ CROWDSTRIKE防病毒资源监视器 CrowdStrike Antiviru ...

  9. 使用pytesseract出现的问题

    dyld: Library not loaded: /usr/local/opt/jpeg/lib/libjpeg.8.dylib Referenced from: /usr/local/lib/li ...

  10. MySQL查询表中某个字段的重复数据

    1. 查询SQL表中某个字段的重复数据 SELECT user_name,COUNT(*) AS count FROM db_user_info GROUP BY user_name HAVING c ...