862. 三元组排序 - AcWing题库
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct Triple{
int x;
float y;
string z;
};
bool Compare(Triple a,Triple b){
return a.x<b.x;
}
int main(){
int N;
cin>>N;
Triple arr[N];
for(int i=0;i<N;i++){
cin>>arr[i].x>>arr[i].y>>arr[i].z;
}
sort(arr,arr+N,Compare);
for(int i=0;i<N;i++){
printf("%d %.2f %s \n",arr[i].x,arr[i].y,arr[i].z.c_str());
//注意printf()的用法
//用s.c_str()将string转化为char *s
}
}
67. 数字在排序数组中出现的次数 - AcWing题库
//for each
class Solution {
public:
int getNumberOfK(vector<int>& nums , int k) {
int cnt=0;
for(int x:nums){
if(x==k){
cnt++;
}
}
return cnt;
}
};
//迭代器
class Solution {
public:
int getNumberOfK(vector<int>& nums , int k) {
int cnt=0;
vector<int>::iterator it;
for(it=nums.begin();it!=nums.end();it++){
if(*it==k) cnt++;
}
return cnt;
}
};
// *a.eng()=a[n];
// *a.back()=a[n-1];
// *a.begin()=*a.front()=a[0];
32. 调整数组顺序使奇数位于偶数前面 - AcWing题库
class Solution {
public:
void reOrderArray(vector<int> &array) {
int l=0,r=array.size()-1;
//array.size();
while(l<r){
while((l<r)&&array[l]%2==1) l++;
while((l<r)&&array[r]%2==0) r--;
if(l<r) swap(array[l],array[r]);
}
}
};
17. 从尾到头打印链表 - AcWing题库
class Solution {
public:
vector<int> printListReversingly(ListNode* head) {
vector<int> res;
while(head){
res.push_back(head->val);
head=head->next;
}
return vector<int>(res.rbegin(),res.rend());
//rbegin(),rend()反向迭代器;
}
};
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
int n=input.size();
sort(input.begin(),input.end());
vector<int> res;
for(int i=0;i<k;i++){
res.push_back(input[i]);
}
return res;
}
};
class Solution {
public:
vector<int> getLeastNumbers_Solution(vector<int> input, int k) {
int n=input.size();
sort(input.begin(),input.end());
vector<int> res;
vector<int>::iterator it;
for(it=input.begin();it<input.begin()+k;it++){//不能写it<k
res.push_back(*it);
}
return res;
}
};
vector也可以用a[i]
75. 和为S的两个数字 - AcWing题库
class Solution {
public:
vector<int> findNumbersWithSum(vector<int>& nums, int target) {
set<int> s;
for(int i=0;i<nums.size();i++){
int find=target-nums[i];
if(s.count(find)!=0) return vector<int>{find,nums[i]};
//int n=target-nums[i];
//if(s.find(n)!=s.end()) return vector<int>{n,nums[i]};
s.insert(nums[i]);
}
return vector<int>();
}
};
标签:arr,nums,int,基础,Solution,语法,vector,input,Acwing
From: https://www.cnblogs.com/coloratura/p/17138957.html