STL容器位运算常用库函数
Posted 幽殇默
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了STL容器位运算常用库函数相关的知识,希望对你有一定的参考价值。
目录
67. 数字在排序数组中出现的次数
class Solution {
public:
int getNumberOfK(vector<int>& nums , int k) {
map<int,int> mp;
for(int i=0;i<nums.size();i++) mp[nums[i]]++;
return mp[k];
}
};
68. 0到n-1中缺失的数字
class Solution {
public:
int getMissingNumber(vector<int>& nums) {
for(int i=0;i<nums.size();i++)
if(i!=nums[i]) return i;
return nums.size();
}
};
class Solution {
public:
int getMissingNumber(vector<int>& nums) {
if(nums.empty()) return 0;
int l=0,r=nums.size()-1;
while(l<r)
{
int mid=l+r+1>>1;
if(nums[mid]<=mid) l=mid;
else r=mid-1;
}
if(nums[l]==l) l++;
return l;
}
};
32. 调整数组顺序使奇数位于偶数前面
class Solution {
public:
void reOrderArray(vector<int> &array) {
vector<int> ans,a;
for(int i=0;i<array.size();i++)
if(array[i]&1) ans.push_back(array[i]);
else a.push_back(array[i]);
for(int i=0;i<a.size();i++) ans.push_back(a[i]);
array=ans;
}
};
class Solution {
public:
void reOrderArray(vector<int> &array) {
int l=0,r=array.size()-1;
while(l<r)
{
while(array[l]&1) l++;
while(array[r]%2==0) r--;
if(l<r) swap(array[l],array[r]);
}
}
};
17. 从尾到头打印链表
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
vector<int> printListReversingly(ListNode* head) {
vector<int> ans;
while(head)
{
ans.push_back(head->val);
head=head->next;
}
reverse(ans.begin(),ans.end());
return ans;
}
};
20. 用两个栈实现队列
class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> ans,backup;
MyQueue() {
}
void copy(stack<int> &ans,stack<int> &backup)
{
while(ans.size()){
backup.push(ans.top());
ans.pop();
}
}
/** Push element x to the back of queue. */
void push(int x) {
ans.push(x);
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
copy(ans,backup);
int res=backup.top();
backup.pop();
copy(backup,ans);
return res;
}
/** Get the front element. */
int peek() {
copy(ans,backup);
int res=backup.top();
copy(backup,ans);
return res;
}
/** Returns whether the queue is empty. */
bool empty() {
return ans.empty();
}
};
/**
* Your MyQueue object will be instantiated and called as such:
* MyQueue obj = MyQueue();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.peek();
* bool param_4 = obj.empty();
*/
53. 最小的k个数
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
sort(input.begin(),input.end());
vector<int> ans;
for(int i=0;i<k;i++) ans.push_back(input[i]);
return ans;
}
};
75. 和为S的两个数字
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
map<int,bool> hush;
for(int i=0;i<nums.size();i++)
{
int t=target-nums[i];
if(hush[t]) return {nums[i],t};
hush[nums[i]]=true;
}
}
};
40. 顺时针打印矩阵
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix) {
if(matrix.empty()) return {};
vector<int> ans;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
int d=0;
int m=matrix[0].size();
int n=matrix.size();
int x=0,y=0;
bool a[n][m];
memset(a,0,sizeof a);
while(ans.size()<n*m)
{
ans.push_back(matrix[x][y]); a[x][y]=true;
int tempx=x+dx[d],tempy=y+dy[d];
if(tempx<0||tempx>=n||tempy<0||tempy>=m||a[tempx][tempy]) d=(d+1)%4;
x=x+dx[d],y=y+dy[d];
}
return ans;
}
};
51. 数字排列
class Solution {
public:
vector<vector<int>> permutation(vector<int>& nums) {
vector<vector<int>> ans;
sort(nums.begin(),nums.end());
do
{
ans.push_back(nums);
}while(next_permutation(nums.begin(),nums.end()));
return ans;
}
};
136. 邻值查找
#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<map>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
int n;
int main() {
map<int, int> nums;
int x;
scanf("%d", &n);
scanf("%d", &x);
nums[x] = 1;
for(int i = 2; i <= n; ++i){
scanf("%d", &x);
auto after = nums.lower_bound(x); // 找到排在x后面的那个数 可能不存在
if(after == nums.begin()){//最小的都比x大
cout <<abs(after->first - x)<< ' ' << after->second << endl;
}
else{
auto start = after;
--start; // start 排在x前面的那个数
if(after == nums.end()){ //最大的都比x小
cout << abs(start->first - x)<< ' ' << start->second << endl;
}
else{
auto choice = abs(start->first - x) <= abs(after->first - x) ? start : after;
cout << abs(choice->first - x) << ' ' << choice->second << endl;
}
}
nums[x] = i;
}
return 0;
}
26. 二进制中1的个数
class Solution {
public:
int lowbit(int n)
{
return n&(-n);
}
int NumberOf1(int n) {
int ans=0;
while(n)
{
n=n-lowbit(n);
ans++;
}
return ans;
}
};
862. 三元组排序
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct node
{
int a; double b; string c;
}Node[10005];
bool cmp(node m,node n)
{
return m.a<n.a;
}
int main(void)
{
int n; cin>>n;
for(int i=0;i<n;i++) cin>>Node[i].a>>Node[i].b>>Node[i].c;
sort(Node,Node+n,cmp);
for(int i=0;i<n;i++) printf("%d %.2lf %s\\n",Node[i].a,Node[i].b,Node[i].c.c_str());
return 0;
}
以上是关于STL容器位运算常用库函数的主要内容,如果未能解决你的问题,请参考以下文章