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. 从尾到头打印链表
题目地址
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 :
stack< int > ans, backup;
MyQueue ( ) {
}
void copy ( stack< int > & ans, stack< int > & backup)
{
while ( ans. size ( ) ) {
backup. push ( ans. top ( ) ) ;
ans. pop ( ) ;
}
}
void push ( int x) {
ans. push ( x) ;
}
int pop ( ) {
copy ( ans, backup) ;
int res= backup. top ( ) ;
backup. pop ( ) ;
copy ( backup, ans) ;
return res;
}
int peek ( ) {
copy ( ans, backup) ;
int res= backup. top ( ) ;
copy ( backup, ans) ;
return res;
}
bool empty ( ) {
return ans. 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) ;
if ( after == nums. begin ( ) ) {
cout << abs ( after-> first - x) << ' ' << after-> second << endl;
}
else {
auto start = after;
-- start;
if ( after == nums. end ( ) ) {
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 ;
}