/*o(n)的堆化方法*/

void myjust(vector<int>& A,int i)
{
int l=i*2+1;
int r=i*2+2;
int minn=i;
if(l<A.size()&&A[l]<A[minn])
minn=l;
if(r<A.size()&&A[r]<A[minn])
minn=r;
if(minn!=i)
{
swap(A[minn],A[i]);
myjust(A,minn);
}
}

void heapify(vector<int> &A) {
int lens=A.size();
if(lens==0) return;
for(int i=lens/2;i>=0;i--)
{
myjust(A,i);
}
}

求sqrt(double x)

键树

class Trie {
public: struct TrieNode {
bool isEnd;
map<char, TrieNode*> myMap;
TrieNode()
{
isEnd = false;
}
}; TrieNode* root;
/** Initialize your data structure here. */
Trie() {
root = new TrieNode();
} /** Inserts a word into the trie. */
void insert(string word) {
TrieNode* current = root;
for (int i = 0; i < word.length(); i++)
{
if (current->myMap.find(word[i]) != current->myMap.end())
{
current = current->myMap[word[i]];
if (i == word.length() - 1)
current->isEnd = true;
}
else
{
TrieNode* tmp = new TrieNode();
tmp->isEnd = (i == word.length() - 1 ? true : false);
current->myMap[word[i]] = tmp;
current = tmp;
}
}
} /** Returns if the word is in the trie. */
bool search(string word) {
TrieNode* current = root;
for (int i = 0; i < word.length(); i++)
{
if (current->myMap.find(word[i]) != current->myMap.end())
{
current = current->myMap[word[i]];
}
else
{
return false;
}
}
return current->isEnd == true ? true : false;
} /** Returns if there is any word in the trie that starts with the given prefix. */
bool startsWith(string prefix) {
TrieNode* current = root;
for (int i = 0; i < prefix.length(); i++)
{
if (current->myMap.find(prefix[i]) != current->myMap.end())
{
current = current->myMap[prefix[i]];
}
else
{
return false;
}
}
return true;
}
};

//寻找前k大小的数

int getIdx(vector<int>& nums,int left,int right)
{
int tmp=nums[left];
while(left<right)
{
while (left<right&&nums[right]>=tmp)
right--;
nums[left]=nums[right];
while (left<right&&nums[left]<tmp)
left++;
nums[right]=nums[left];
}
nums[left]=tmp;
return left;
} vector<int> GetLeastNumbers_Solution(vector<int> input, int k) { vector<int> ans;
if(input.size()<k||k<=0||input.size()==0)
{
return ans;
}
int idx=-1;
int left=0,right=input.size()-1;
while (idx!=k-1)
{
idx=getIdx(input,left,right);
if(idx>k-1)
{
right=idx-1;
}
else
{
left=idx+1;
}
}
for(int i=0;i<k;i++)
{
ans.push_back(input[i]);
}
return ans;
}

  

  

杂乱的code的更多相关文章

  1. Code First :使用Entity. Framework编程(3) ----转发 收藏

    第三章 对属性使用约定和配置 在第2章,对Code First的约定以及如何通过配置覆写默认约定行为进行了大致的介绍.学习了如何使用Data Annotations进行配置,也学习了如何使用Fluen ...

  2. Intellij IDEA 配置 Code Style

    前言 昨天自说自话,闲扯了界面设计和代码规范.设计确实需要一些经验,也不一定能取悦所有人.而代码规范却是程序员所起码应当做到的,多人协作中,杂乱的代码就好像批阅潦草的作文,可读性极差. 然而这是个懒人 ...

  3. Visual Studio Code 代理设置

    Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...

  4. 我们是怎么做Code Review的

    前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...

  5. Code Review 程序员的寄望与哀伤

    一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...

  6. 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM

    刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...

  7. 在Visual Studio Code中配置GO开发环境

    一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github ...

  8. 代码的坏味道(14)——重复代码(Duplicate Code)

    坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...

  9. http status code

    属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...

随机推荐

  1. 【刷题】HDU 1695 GCD

    Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...

  2. 【比赛】HNOI2018 毒瘤

    虚树+dp 直接看zlttttt的强大题解 zlttttt的题解看这里 #include<bits/stdc++.h> #define ui unsigned int #define ll ...

  3. C中 ->运算符说明

    岁数大了,记忆力不好!这里记下,以后忘了来查! ->运算符. 访问结构中的成员 用 点“.”运算符 Ex: typedef struct st { char a; int b; } st; 定义 ...

  4. OpenCV图像变换(仿射变换与透视变换)

    仿射变换(affine transform)与透视变换(perspective transform)在图像还原.图像局部变化处理方面有重要意义.通常,在2D平面中,仿射变换的应用较多,而在3D平面中, ...

  5. maven使用ss代理

    把maven安装目录下的conf/settings.xml复制一份到~/.m2/下 在<proxies>标签中添加 <proxy> <id>socks5</i ...

  6. js 时间戳 转化

    new Date((1524142795*1000)).toJSON().slice(11,16)

  7. python的StringIO模块

    StringIO经常被用来作字符串的缓存,因为StringIO的一些接口和文件操作是一致的,也就是说同样的代码,可以同时当成文件操作或者StringIO操作. 一.StringIO中的常用方法 1.r ...

  8. u3d图片转视频

    c#代码://将截图生成视频public static void createVideo(){ ProcessStartInfo psi = new ProcessStartInfo(); psi.F ...

  9. 深入浅出CSS(一):line-height与vertical-align的性质

    [测试代码] <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...

  10. JAVA多线程提高六:java5线程并发库的应用_线程池

    前面我们对并发有了一定的认识,并且知道如何创建线程,创建线程主要依靠的是Thread 的类来完成的,那么有什么缺陷呢?如何解决? 一.对比new Threadnew Thread的弊端 a. 每次ne ...