/*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. 【BZOJ3563/BZOJ3569】DZY Loves Chinese I/II(随机化,线性基)

    [BZOJ3563/BZOJ3569]DZY Loves Chinese I/II(随机化,线性基) 题面 搞笑版本 正经版本 题面请自行观赏 注意细节. 题解 搞笑版本真的是用来搞笑的 所以我们来讲 ...

  2. 流媒体协议之RTSP客户端的实现20171014

    RtspClient是基于jrtplib实现的,目前仅支持h264格式,后续将不断迭代优化,加入对其他格式的支持,并且将实现RTSP的服务端. RtspClient的功能是接收服务端过来流,然后写入到 ...

  3. bzoj 1588 平衡树 splay

    1588: [HNOI2002]营业额统计 Time Limit: 5 Sec  Memory Limit: 162 MBSubmit: 15446  Solved: 6076[Submit][Sta ...

  4. 省选模拟赛 LYK loves string(string)

    题目描述 LYK喜欢字符串,它认为一个长度为n的字符串一定会有n*(n+1)/2个子串,但是这些子串是不一定全部都不同的,也就是说,不相同的子串可能没有那么多个.LYK认为,两个字符串不同当且仅当它们 ...

  5. ubuntu系统安装与卸载软件常用命令

    一.unbuntu下的软件安装方式 1.deb包的安装方式 deb是debian系Linux的包管理方式,ubuntu是属于debian系的Linux发行版,所以默认支持这种软件安装方式,当下载到一个 ...

  6. [DeeplearningAI笔记]卷积神经网络4.11一维和三维卷积

    4.4特殊应用:人脸识别和神经网络风格转换 觉得有用的话,欢迎一起讨论相互学习~Follow Me 4.11一维和三维卷积 二维和一维卷积 对于2D卷积来说,假设原始图像为\(14*14*3\)的三通 ...

  7. OpenCV---膨胀与腐蚀

    膨胀 腐蚀 一:膨胀实现dilate import cv2 as cv import numpy as np def dilate_demo(image): #膨胀 print(image.shape ...

  8. php 傻瓜式代码计算两个时间间隔

    $stamp = (strtotime($_POST['start'])-strtotime($_POST['end'])); $s = $stamp%60; //秒 $m_stamp= ($stam ...

  9. 「LibreOJ β Round #4」框架

    https://loj.ac/problem/527 题目描述 有一个n×m的矩形框架,但其中有些边被删除了.qmqmqm想知道剩余部分中还有多少完整的正方形.只有当一个正方形的每一条边均被保留下来, ...

  10. 字符串:SAM

    HDU4622:区间查询不同子串个数 用后缀自动机预处理出所有区间的不同子串个数 建立n次后缀自动机 #include <stdio.h> #include <string.h> ...