c++ STL容器、位运算与常用库函数的练习题
一、68. 0到n-1中缺失的数字
class Solution {
public:
int getMissingNumber(vector<int>& nums) {
//此题应用 哈希表
unordered_set<int> a;
for(int i=0;i<=nums.size();i++) a.insert(i); //先将所有的元素都承装到哈希表中
for(auto i:nums) a.erase(i); //去掉数组中存在的数据
return *a.begin();
}
};
此题之所以使用哈希表是因为,利用了哈希表作为容器的特性,可以用来承装数据,还因为哈希表中相应的函数,用起来很方便可以实现题目所需。
二、32. 调整数组顺序使奇数位于偶数前面
法一:
class Solution {
public:
void reOrderArray(vector<int> &array) {
//法一:
//先遍历,再插入
vector<int> res;
int k=0;
while(k<array.size())
{
if(array[k]%2==1) res.push_back(array[k]);
k++;
}
int m=0;
while(m<array.size())
{
if(array[m]%2==0) res.push_back(array[m]);
m++;
}
array=res; //!!!
}
};
法二:
双指针
为什么想到用双指针:就只需要考虑奇数和偶数两种,对应双指针。
class Solution {
public:
void reOrderArray(vector<int> &array) {
//法二:
//双指针算法:直接在原容器array上面进行操作
int i=0,j=array.size()-1; //一个指向头部,一个指向尾部
while(i<j)
{
while(i<j&&array[i]%2==1) i++;
while(i<j&&array[j]%2==0) j--;
if(i<j) swap(array[i],array[j]);
}
}
};
三、20. 用两个栈实现队列
class MyQueue {
public:
/** Initialize your data structure here. */
stack<int> s1,s2; //所要用到的两个栈
MyQueue() {
}
/** Push element x to the back of queue. */
void push(int x) {
s1.push(x); //将s1看做是主要存储栈,而s2看做是辅助栈
}
/** Removes the element from in front of queue and returns that element. */
int pop() {
while(s1.size()>1) s2.push(s1.top()),s1.pop();
int t=s1.top(); //上行代码一直在执行到s1.size()==1 为止,说明栈s1中只有一个元素,将其记录
s1.pop(); //将s1中的元素弹净,再回插
while(s2.size()) s1.push(s2.top()),s2.pop();
return t;
}
/** Get the front element. */
int peek() {
while(s1.size()>1) s2.push(s1.top()),s1.pop();
int t=s1.top(); //上行代码一直在执行到s1.size()==1 为止,说明栈s1中只有一个元素
while(s2.size()) s1.push(s2.top()),s2.pop();
return t;
}
/** Returns whether the queue is empty. */
bool empty() {
return s1.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();
*/
此题的意思就是,利用两个栈来模拟实现对对列中特定元素的操作。
四、75. 和为S的两个数字
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
//利用哈希表来做
unordered_set<int> S;
for(int x:nums)
{
if(S.count(target-x)) return {x,target-x};
S.insert(x);
}
}
};
此处使用哈希表的主要原因我觉得是:
利用哈希表作为数据集合,可以很方便地存储数据的特性。
五、51. 数字排列
class Solution {
public:
vector<vector<int>> permutation(vector<int>& nums) {
//利用一个很好用的函数
//next_permutation
sort(nums.begin(),nums.end());
vector<vector<int>> res;
do
{
res.push_back(nums);
}while(next_permutation(nums.begin(),nums.end()));
return res;
}
};
六、26. 二进制中1的个数
法一:
class Solution {
public:
int NumberOf1(int n) {
//法一:
int res=0;
for(int i=0;i<32;i++)
{
if(n>>i&1) res++;
}
return res;
}
};
法二:
class Solution {
public:
int NumberOf1(int n) {
//法二:
int res=0;
while(n) n-=n & -n,res++; //每减掉一个最后一位1,就计数一次
return res;
}
};
七、862. 三元组排序
//考察结构体的排序:
//之所以用结构体排序是因为:3个不同的变量不知道存储在哪种容器里面,只能使用结构体了
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
const int N=10010;
struct Data
{
int x;
double y;
string z;
//写明排序规则
bool operator<(const Data & t) const
{
return x<t.x; //从小到大排序
}
}a[N]; //定义一个对应数据类型的数组
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++) cin>>a[i].x>>a[i].y>>a[i].z;
sort(a,a+n); //利用sort排序
for(int i=0;i<n;i++)
printf("%d %.2lf %s\n",a[i].x,a[i].y,a[i].z.c_str()); //注意string在用%s输出时,要用.c_str()
return 0;
}
考察结构体的排序。