杂乱的code
/*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的更多相关文章
- Code First :使用Entity. Framework编程(3) ----转发 收藏
第三章 对属性使用约定和配置 在第2章,对Code First的约定以及如何通过配置覆写默认约定行为进行了大致的介绍.学习了如何使用Data Annotations进行配置,也学习了如何使用Fluen ...
- Intellij IDEA 配置 Code Style
前言 昨天自说自话,闲扯了界面设计和代码规范.设计确实需要一些经验,也不一定能取悦所有人.而代码规范却是程序员所起码应当做到的,多人协作中,杂乱的代码就好像批阅潦草的作文,可读性极差. 然而这是个懒人 ...
- Visual Studio Code 代理设置
Visual Studio Code (简称 VS Code)是由微软研发的一款免费.开源的跨平台文本(代码)编辑器,在十多年的编程经历中,我使用过非常多的的代码编辑器(包括 IDE),例如 Fron ...
- 我们是怎么做Code Review的
前几天看了<Code Review 程序员的寄望与哀伤>,想到我们团队开展Code Review也有2年了,结果还算比较满意,有些经验应该可以和大家一起分享.探讨.我们为什么要推行Code ...
- Code Review 程序员的寄望与哀伤
一个程序员,他写完了代码,在测试环境通过了测试,然后他把它发布到了线上生产环境,但很快就发现在生产环境上出了问题,有潜在的 bug. 事后分析,是生产环境的一些微妙差异,使得这种 bug 场景在线下测 ...
- 从Script到Code Blocks、Code Behind到MVC、MVP、MVVM
刚过去的周五(3-14)例行地主持了技术会议,主题正好是<UI层的设计模式——从Script.Code Behind到MVC.MVP.MVVM>,是前一天晚上才定的,中午花了半小时准备了下 ...
- 在Visual Studio Code中配置GO开发环境
一.GO语言安装 详情查看:GO语言下载.安装.配置 二.GoLang插件介绍 对于Visual Studio Code开发工具,有一款优秀的GoLang插件,它的主页为:https://github ...
- 代码的坏味道(14)——重复代码(Duplicate Code)
坏味道--重复代码(Duplicate Code) 重复代码堪称为代码坏味道之首.消除重复代码总是有利无害的. 特征 两个代码片段看上去几乎一样. 问题原因 重复代码通常发生在多个程序员同时在同一程序 ...
- http status code
属于转载 http status code:200:成功,服务器已成功处理了请求,通常这表示服务器提供了请求的网页 404:未找到,服务器未找到 201-206都表示服务器成功处理了请求的状态代码,说 ...
随机推荐
- 【刷题】BZOJ 1468 Tree
Description 给你一棵TREE,以及这棵树上边的距离.问有多少对点它们两者间的距离小于等于K Input N(n<=40000) 接下来n-1行边描述管道,按照题目中写的输入 接下来是 ...
- 【arc075F】Mirrored
Portal --> arc075_f Solution 一开始抱着"我有信仰爆搜就可以过"的心态写了一个爆搜.. 但是因为..剪枝和枚举方式不够优秀愉快T掉了q ...
- poj 3261 后缀数组 可重叠的 k 次最长重复子串
Milk Patterns Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 16430 Accepted: 7252 Ca ...
- UVA - 1262 数学
UVA - 1262 题意: 有两个6*5 的大写字母组成的矩阵,需要找出满足条件的字典序第k小的密码:密码中每个字母在两个矩阵的对应的同一列中都出现过 代码: // 先处理出来每一列可以取的字母,例 ...
- 解决vue路由与锚点冲突
传统的锚点定位会与vue-router中的路由设置存在冲突,解决方法自定义锚点跳转: html: <ul> <li><a href="" @click ...
- background(css复合写法)
1. 背景-background========================================================== 单个属性的写法 .sample1 { /*背景颜色 ...
- 关于java 获取 html select标签 下拉框 option 文本内容 隐藏域
在HTML中从多选下拉框中提取已选中选项的文本内容到后台,被这个问题难倒了. demo.jsp文件 <select id="selecttype" name"typ ...
- hdu 4055 Number String
Number String http://acm.hdu.edu.cn/showproblem.php?pid=4055 Time Limit: 10000/5000 MS (Java/Others) ...
- 2017.5.27 NOIP模拟赛(hzwer2014-5-16 NOIP模拟赛)
期望得分:100+100+60+30=290 实际得分:100+20+60+0=180 当务之急:提高一次正确率 Problem 1 双色球(ball.cpp/c/pas) [题目描述] 机房来了新一 ...
- Windows下的MySQL删除data文件夹后……
MySQL删除data文件夹后,怎么都无法启动了,出现错误: 150106 9:28:43 [Note] Plugin 'FEDERATED' is disabled. wampmysqld: Tab ...