杂乱的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都表示服务器成功处理了请求的状态代码,说 ...
随机推荐
- Astronauts UVALive - 3713(2-SAT)
大白书例题 #include <iostream> #include <cstdio> #include <sstream> #include <cstrin ...
- 【BZOJ3625/CF438E】小朋友和二叉树(多项式求逆,多项式开方)
[BZOJ3625/CF438E]小朋友和二叉树(多项式求逆,多项式开方) 题面 BZOJ CodeForces 大致题意: 对于每个数出现的次数对应的多项式\(A(x)\) 求\[f(x)=\fra ...
- 【bzoj3796】Mushroom追妹纸
Portal -->bzoj3796 Description 给出字符串s1.s2.s3,找出一个字符串w,满足: 1.w是s1的子串: 2.w是s2的子串: 3.s3不是w的子串. 求w的 ...
- spoj 694 705 不相同的子串的个数
http://www.spoj.com/problems/SUBST1/ SUBST1 - New Distinct Substrings #suffix-array-8 Given a string ...
- [Z3001] connection to database 'zabbix' failed: [1045] Access denied for user 'zabbix'@'localhost' (using password: YES)
在配置了zabbix服务端后,发现:“zabbix server is running”的Value值是“no”, 用:netstat -atnlp|grep 10051 发现没有出现zabbix_s ...
- Socket初识2
一.Socket一些概念 sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 1.1 参数1:Socket Families(地址簇) / ...
- linux 文件IO
1.文件描述符 (1)文件描述符的本质是一个数字,这个数字本质上是进程表中文件描述符表的一个表项,进程通过文件描述符作为index去索引查表得到文件表指针,再间接访问得到这个文件对应的文件表.(2)文 ...
- 查看MySQL日志数据binlog文件
binlog介绍 binlog,即二进制日志,它记录了数据库上的所有改变. 改变数据库的SQL语句执行结束时,将在binlog的末尾写入一条记录,同时通知语句解析器,语句执行完毕. binlog格式 ...
- [吴恩达机器学习笔记]13聚类K-means
13.聚类 觉得有用的话,欢迎一起讨论相互学习~Follow Me 13.1无监督学习简介 从监督学习到无监督学习 在一个典型的监督学习中,我们有一个有标签的训练集,我们的目标是找到能够区分正样本和负 ...
- Java BLOB 数据的插入与读取 操作
package com.lw.database; import java.io.FileInputStream; import java.io.FileOutputStream; import jav ...