杂乱的code
2024-08-31 03:28:57
/*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;
}
最新文章
- linux-用命令形式聊天的常用命令
- 小技巧找出一个php的cron脚本出问题的代码行
- Redhat系统网络配置
- POJ解题经验交流
- overlay-3
- My集合框架第六弹 左式堆
- Codeforces Gym 100531D Digits 暴力
- HDU 5965 扫雷 【模拟】 (2016年中国大学生程序设计竞赛(合肥))
- nyist 303序号互换(数学推理)
- android相关内容
- i/10和i取最后两位的精妙算法(前方高能)
- JavaScript监控页面input输入整数且只能输入2位小数
- 记录一次mongodb因网络问题导致shard节点异常
- 20165234 《Java程序设计》第三周学习总结
- Luogu3232 HNOI2013 游走 高斯消元、期望、贪心
- python 使用 matplotlib.pyplot来画柱状图和饼图
- VS2010使用Release进行调试的三个必须设置选项
- zabbix修改Template OS Linux模版使已使用内存(Used memory)更准确
- numpy中loadtxt 的用法
- SVN 使用笔记
热门文章
- atcoder 2017Code festival C ——D题 Yet Another Palindrome Partitioning(思维+dp)
- 51nod 1317 相似字符串对(容斥原理+思维)
- 解题:JLOI 2016 侦查守卫
- git使用经验(一)
- Python3 字典 fromkeys()方法
- Linux之系统信息操作20170330
- 14.Android UiAutomator 图像处理
- 基于javaWeb阶段下的Cookie和Session总结
- Ant Design 通过 WeekPicker 获取一周的起止时间
- 用shell获取目录/文件夹/文件的时间戳