目录
1.按奇偶排序数组
给定一个非负整数数组 A
,返回一个由 A
的所有偶数元素组成的数组,后面跟 A
的所有奇数元素。
你可以返回满足此条件的任何数组作为答案。
示例:
输入:[3,1,2,4]
输出:[2,4,3,1]
输出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也会被接受。
/**
* Return an array of size *returnSize.
* Note: The returned array must be malloced, assume caller calls free().
*/
int* sortArrayByParity(int* A, int ASize, int* returnSize) {
int temp,p=0,i;
for(i=0;i<ASize;i++)
{
if(A[i]%2==0)
{
temp=A[i];
A[i]=A[p];
A[p]=temp;
p++;
}
}
*returnSize=ASize;
return A;
}
2.从尾到头打印链表
输入一个链表,按链表值从尾到头的顺序返回一个ArrayList。
/**
* struct ListNode {
* int val;
* struct ListNode *next;
* ListNode(int x) :
* val(x), next(NULL) {
* }
* };
*/
class Solution {
public:
vector<int> printListFromTailToHead(ListNode* head) {
vector<int> value;
if(head != NULL)
{
value.insert(value.begin(),head->val);
while(head->next != NULL)
{
value.insert(value.begin(),head->next->val);
head = head->next;
}
}
return value;
}
};
3.替换空格
请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
void replaceSpace(char *&str,int length) {
char *newstr;int flag=0,i=0,j=0;
for(i=0;i<length;i++)
if(str[i]==' ') flag++;
newstr=new char[length+flag*3];
for(i=0,j=0;i<length;i++,j++)
if(str[i]==' '){
newstr[j]='%';
newstr[j+1]='2';
newstr[j+2]='0';
j=j+2;
}
else
newstr[j]=str[i];
str=newstr;
}
4.二维数组中的查找
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
class Solution {
public:
bool Find(int target, vector<vector<int> > array) {
int flag=array[0].size();
for(int i=0;i<array.size();i++)
for(int j=0;j<flag;j++)
{
if(target==array[i][j]) return true;
if(target<array[i][j])
{
flag=j;
break;
}
}
return false;
}
};
5.六一儿童节
六一儿童节,老师带了很多好吃的巧克力到幼儿园。每块巧克力j的重量为w[j],对于每个小朋友i,当他分到的巧克力大小达到h[i] (即w[j]>=h[i]),他才会上去表演节目。老师的目标是将巧克力分发给孩子们,使得最多的小孩上台表演。可以保证每个w[i]> 0且不能将多块巧克力分给一个孩子或将一块分给多个孩子。
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
int n,m;
int *h,*w;
cin>>n;
h=new int[n];
for(int i=0;i<n;i++){
cin>>h[i];
}
cin>>m;
w=new int[m];
for(int j=0;j<m;j++){
cin>>w[j];
}
sort(h, h+n); sort(w, w+m);
int i = 0, j = 0, res = 0;
while(j < m && i < n){
if(h[i] <= w[j]) {res++; i++; j++;}
else j++;
}
cout<<res<<endl;
}
6.大整数相乘
有两个用字符串表示的非常大的大整数,算出他们的乘积,也是用字符串表示。不能用系统自带的大整数类型。
#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;
const int L=11000;
string mul(string,string);
int main(){
string x,y;
while(cin>>x>>y)
cout<<mul(x,y)<<endl;
}
string mul(string a,string b) {
string s;
int na[L],nb[L],nc[L],La=a.size(),Lb=b.size(),i,j;
fill(na,na+L,0);fill(nb,nb+L,0);fill(nc,nc+L,0);
for(i=La-1;i>=0;i--) na[La-i]=a[i]-'0';
for(i=Lb-1;i>=0;i--) nb[Lb-i]=b[i]-'0';
for(i=1;i<=La;i++)
for(j=1;j<=Lb;j++)
nc[i+j-1]+=na[i]*nb[j];
for(i=1;i<=La+Lb;i++)
nc[i+1]+=nc[i]/10,nc[i]%=10;
if(nc[La+Lb]) s+=nc[La+Lb]+'0';
for(i=La+Lb-1;i>=1;i--)
s+=nc[i]+'0';
return s;
}
7.最大乘积
给定一个无序数组,包含正数、负数和0,要求从中找出3个数的乘积,使得乘积最大,要求时间复杂度:O(n),空间复杂度:O(1)
#include<iostream>
using namespace std;
int main()
{
long long *a,c,d,result,temp,max,pre,ppre;
int i,n;
cin>>n;
a=new long long[n];
for(i=0;i<n;i++)
{
cin>>a[i];
}
for(int j=0;j<n;j++)
{
for(i=0;i<n-j-1;i++)
{
if(a[i]>a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
max=a[n-1];
pre=a[n-2];
ppre=a[n-3];
for(int j=0;j<n;j++)
{
for(i=0;i<n-j-1;i++)
{
if(a[i]<a[i+1])
{
temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;
}
}
}
c=max*pre*ppre;
d=a[n-1]*a[n-2]*max;
if(c>d)
cout<<c<<endl;
else
cout<<d<<endl;
}
8.两数之和
给定一个整数数组 nums
和一个目标值 target
,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* twoSum(int* nums, int numsSize, int target) {
static int a[2];
int temp;
for(int p=0;p<numsSize-1;p++)
{
temp=target-nums[p];
for(int q=p+1;q<numsSize;q++)
{
if(nums[q]==temp)
{
a[0]=p;a[1]=q; return a;
}
}
}
return 0;
}
9.数组中重复的数字
在一个长度为 n 的数组里的所有数字都在 0 到 n-1 的范围内。数组中某些数字是重复的,但不知道有几个数字是重复的,也不知道每个数字重复几次。请找出数组中任意一个重复的数字。Input: {2, 3, 1, 0, 2, 5} Output: 2。要求是时间复杂度 O(N),空间复杂度 O(1)。因此不能使用排序的方法,也不能使用额外的标记数组。
//对于这种数组元素在 [0, n-1] 范围内的问题,可以将值为 i 的元素调整到第 i 个位置上进行求解。
bool Solution(int* nums, int length, int* duplication){
for (int i = 0; i < length; i++) {
while (nums[i] != i) {
if (nums[i] == nums[nums[i]]) {
duplication[0] = nums[i];
cout<<duplication[0];
return true;
}
swap(nums[i],nums[nums[i]]);
}
}
return false;
}
int main(){
int arr[6] = {2, 3, 1, 0, 2, 5};
int *b=new int[6];
Solution(arr,6,b);
}
10.二维数组中的查找
给定一个二维数组,其每一行从左到右递增排序,从上到下也是递增排序。给定一个数,判断这个数是否在该二维数组中。要求时间复杂度 O(M + N),空间复杂度 O(1)
//二维数组中的查找.该二维数组中的一个数,它左边的数都比它小,下边的数都比它大。因此,从右上角开始查找.
bool Find(int target, int matrix[5][5],int rows,int cols) {
int r = 0, c = cols - 1; // 从右上角开始
while (r <= rows - 1 && c >= 0) {
if (target == matrix[r][c])
return true;
else if (target > matrix[r][c])
r++;
else
c--;
}
return false;
}
int main(){
int arr[5][5] = {{1, 4, 7, 11, 15},
{2, 5, 8, 12, 19},
{3, 6, 9, 16, 22},
{10, 13, 14, 17, 24},
{18, 21, 23, 26, 30}};
int target;
while(cin>>target){
if(Find(target,arr,5,5))
cout<<"True!"<<endl;
else
cout<<"False!"<<endl;
}
}