剑指offer第五章
剑指offer第五章
1.数组中出现次数超过一半的数
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers)
{
int length=numbers.size();
int ans=;
if(numbers.empty())
return ;
sort(numbers.begin(),numbers.end());//排序
int midNum=numbers[length/];
int count=;//统计次数初始化
for(int i=;i<length;i++)
{
if(numbers[i]==midNum)
++count;//次数统计
}
if(count>length/)//进行判断,是否为要求
ans=midNum;
return ans;
}
};
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers)
{
if(numbers.empty()) return ;
// 遍历每个元素,并记录次数;若与前一个元素相同,则次数加1,否则次数减1
int result = numbers[];
int times = ; // 次数
for(int i=;i<numbers.size();++i)
{
if(times == )
{
// 更新result的值为当前元素,并置次数为1
result = numbers[i];
times = ;
}
else if(numbers[i] == result)
{
++times; // 相同则加1
}
else
{
--times; // 不同则减1
}
}
// 判断result是否符合条件,即出现次数大于数组长度的一半
times = ;
for(int i=;i<numbers.size();++i)
{
if(numbers[i] == result) ++times;
}
return (times > numbers.size()/) ? result : ;
}
2.最小的k个数
class Solution {
public:
vector<int> GetLeastNumbers_Solution(vector<int> input, int k)
{
int length=input.size();
vector<int> ans;
if(input.empty()||k>length)
return ans;
sort(input.begin(),input.end());
for(int i=;i<k;i++)
ans.push_back(input[i]);
return ans;
}
};
3.连续子数组的最大和
{6,-3,-2,7,-15,1,2,2},连续子向量的最大和为8(从第0个开始,到第3个为止)。你会不会被他忽悠住?(子向量的长度至少是1) 
class Solution {
public:
int FindGreatestSumOfSubArray(vector<int> array)
{
if (array.empty())
return ;
int temp1 = array[], max = array[];
for (int i = ; i < array.size(); i++)
{
if (temp1 + array[i] > array[i])
temp1 += array[i];
else
temp1 = array[i];
if (temp1 > max)
max = temp1;
}
return max;
}
};
4.从1到n整数1出现的次数
class Solution {
public:
int numberOf1(int n)
{
int number=;
while(n)
{
if(n%==)
{
number++;
}
n=n/;
}
return number;
}
int NumberOf1Between1AndN_Solution(int n)
{
int number=;//参数初始化
for(int i=;i<=n;++i)
number+=numberOf1(i);
return number;
}
};
5.把数组排成最小的数
class Solution {
public:
static bool cmp(int a,int b){
string A="";
string B="";
A+=to_string(a);
A+=to_string(b);
B+=to_string(b);
B+=to_string(a);
return A<B;
}
string PrintMinNumber(vector<int> numbers) {
string answer="";
sort(numbers.begin(),numbers.end(),cmp);
for(int i=;i<numbers.size();i++){
answer+=to_string(numbers[i]);
}
return answer;
}
};
6.丑数
分析:所谓一个数m是另一个数n的因子,是指n能被m整除,也就是n%m==0
class Solution {
public:
bool IsUglyNumber(int number)
{
while(number%==)
number/=;
while(number%==)
number/=;
while(number%==)
number/=;
if(number==)
return true;
else
return false;
}
int GetUglyNumber_Solution(int index)
{
if(index<=)
return ;
int number=;
int uglyFound=;
while(uglyFound<index)
{
++number;
if(IsUglyNumber(number))
{
++uglyFound;
}
}
return number;
}
};
思路2:创建数组保存已经找到的丑数,用空间换时间
class Solution {
public:
int GetUglyNumber_Solution(int index) {
if(index<=)
return ;
int *pUglyNumber=new int[index];
pUglyNumber[]=;
int NextUglyIndex=;
int *pMutiply2=pUglyNumber;
int *pMutiply3=pUglyNumber;
int *pMutiply5=pUglyNumber;
while(NextUglyIndex<index)
{
int min=Min(*pMutiply2*,*pMutiply3*,*pMutiply5*);
pUglyNumber[NextUglyIndex]=min;
while(*pMutiply2*<=pUglyNumber[NextUglyIndex]) //当前最大丑数pUglyNumber[NextUglyIndex]
pMutiply2++;
while(*pMutiply3*<=pUglyNumber[NextUglyIndex])
pMutiply3++;
while(*pMutiply5*<=pUglyNumber[NextUglyIndex])
pMutiply5++;
NextUglyIndex++;
}
int ugly=pUglyNumber[index-];
delete[]pUglyNumber;
return ugly;
}
int Min(int a,int b,int c)
{
int min=a;
if(min>b)
min=b;
if(min>c)
min=c;
return min;
}
};
7.第一个只出现一次的字符
class Solution {
public:
int FirstNotRepeatingChar(string str)
{
if ( str.length() == )
return -;
unsigned int hashTime[] = {};
for(int i =;i<str.length();++i)
hashTime[str[i]]++;
for(int i =;i<str.length();++i)
{
if(hashTime[str[i]] == )
return i;
}
return -;
}
};
8.数组中的逆序对
class Solution {
public:
int InversePairs(vector<int> d) {
int r = ;
for(int i = ; i < d.size(); ++i){
for(int j = ; j < i; ++j) if(d[j] > d[i]) ++r;
}
return r;
}
};
class Solution {
public:
int InversePairs(vector<int> data)
{
int length=data.size();
if(length<=)
return ;
vector<int> copy;
for(int i=;i<length;i++)
copy.push_back(data[i]);
long long count=InversePairsCore(data,copy,,length-);
return count%;
}
long long InversePairsCore(vector<int> &data,vector<int> ©,int start,int end)
{
if(start==end)
{
copy[start]=data[start];
return ;
}
int length=(end-start)/;
long long left=InversePairsCore(copy,data,start,start+length);
long long right=InversePairsCore(copy,data,start+length+,end);
int i=start+length;
int j=end;
int indexcopy=end;
long long count=;
while(i>=start&&j>=start+length+)
{
if(data[i]>data[j])
{
copy[indexcopy--]=data[i--];
count=count+j-start-length; //count=count+j-(start+length+1)+1;
}
else
{
copy[indexcopy--]=data[j--];
}
}
for(;i>=start;i--)
copy[indexcopy--]=data[i];
for(;j>=start+length+;j--)
copy[indexcopy--]=data[j];
return left+right+count;
}
};
9.两个链表的第一个公共点
/*
struct ListNode {
int val;
struct ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};*/
class Solution {
public:
ListNode* FindFirstCommonNode( ListNode *pHead1, ListNode *pHead2) {
ListNode *p1 = pHead1;
ListNode *p2 = pHead2;
while(p1!=p2)
{
if(p1==NULL)
p1=pHead2;
else
p1=p1->next;
if(p2==NULL)
p2=pHead1;
else
p2=p2->next;
}
return p1;
}
};
剑指offer第五章的更多相关文章
- 剑指offer第七章&第八章
剑指offer第七章&第八章 1.把字符串转换成整数 将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数. 数值为0或者字符串不是一个合法的数值则返回0 输入描述: 输入一个字符串 ...
- 剑指offer第六章
剑指offer第六章 1.数字在排序数组中出现的次数 统计一个数字在排序数组中出现的次数.例如输入排序数组{1,2,3,3,3,3,4,5}和数字3,由于3在数组中出现了4次,所以输出4 分析:思路1 ...
- 剑指offer第四章
剑指offer第四章 1.二叉树的镜像 二叉树的镜像:输入一个二叉树,输出它的镜像 分析:求树的镜像过程其实就是在遍历树的同时,交换非叶结点的左右子结点. 求镜像的过程:先前序遍历这棵树的每个结点,如 ...
- 剑指offer第三章
剑指offer第三章 1.数值的整数次方 给定一个double类型的浮点数base和int类型的整数exponent.求base的exponent次方. class Solution { public ...
- 《剑指Offer》第二章(一)题3-8
为春招实习做准备,记录一下<剑指Offer>里面的面试题 第二章 面试题3:数组之中的重复数字. 这个题吧,虽然不难,但是不知道为什么就是看了很久,可能很久没有做算法题了.最后面一句话说的 ...
- 《剑指Offer》第二章(一)题 9 -12
第二章 面试题9:用两个栈实现队列 题目:如面试题,给你两个栈, 实现队列的先进先出,即在队列头删除一个元素以及在队列的尾部添加一个元素 思路:这个题的分析感觉很巧妙,从一个具体的例子入手,找出其中的 ...
- 剑指offer—第三章高质量代码(o(1)时间删除链表节点)
题目:给定单向链表的头指针和一个节点指针,定义一个函数在O(1)时间删除该节点,链表节点与函数的定义如下:struct ListNode{int m_nValue;ListNode* m_pValue ...
- 剑指offer—第三章高质量的代码(按顺序打印从1到n位十进制数)
题目:输入一个数字n,按照顺序打印出1到最大n位十进制数,比如输入3,则打印出1,2,3直到最大的3位数999为止. 本题陷阱:没有考虑到大数的问题. 本题解题思路:将要打印的数字,看成字符串,不足位 ...
- 剑指offer—第三章高质量代码(数值的整数次方)
高质量的代码:容错处理能力,规范性,完整性.尽量展示代码的可扩展型和可维护性. 容错处理能力:特别的输入和处理,异常,资源回收. 规范性:清晰的书写,清晰的布局,合理的命名. 完整性:功能测试,边界测 ...
随机推荐
- MySQL设置某一字段默认为0,但是插入数据化却显示为null
1.MySQL字段默认的含义:在插入时不指定该字段的值:2.以mybatis举例,如果是插入实体,那么为空的字段就会插入空:3.如果不想mybatis等持久化工具插入空,可以尝试insertSelec ...
- hdu2196树形dp
有一棵树,找每个节点所能到达的最远距离是多少 dis[u][0]正向最大距离 dis[u][1]正向次大距离 dis[u][2]反向最大距离 先一边dfs求出每个节点的正向最大距离(就是 ...
- nyoj164——卡特兰数(待填坑)
题意:将1~2n个数按照顺时针排列好,用一条线将两个数字连接起来要求:线之间不能有交点,同一个点只允许被连一次. 最后问给出一个n,有多少种方式满足条件. 卡特兰数(列): 令h(0)=1,h(1)= ...
- IOS-简单动画
iOS那些简单的动画(不定期更新) 字数669 阅读1118 评论16 喜欢59 关于 Core Animation Core Animation是一组非常强大的动画处理API,使用它能做出很多优雅的 ...
- iOS开发图片加载的内存问题及优化方案
原创作者:Magic-Unique 原文地址:https://github.com/Magic-Unique/HXImage猿吧 - 资源共享论坛: http://www.coderbar.cn 做最 ...
- 【Raspberry Pi】 小问题汇总
注: 此系列为自己之前所搭建网站内容. 目前入手树莓派2,将遇到的一些琐碎的问题记录在此. 1. 更改时区 查看日期命令:date 输入sudo dpkg-reconfigure tzdata后按提示 ...
- day6-面向对象补充篇--类的特殊成员
先说明一下,今天的内容主要转自师兄张其高的博客http://www.cnblogs.com/zhangqigao/articles/6935221.html 前面我们讲了类的方法,有普通方法,就是我们 ...
- vue的slot的使用问题
slot的使用问题 根据官方文档来说,有三种使用方式 https://cn.vuejs.org/v2/guide/components.html#%E4%BD%BF%E7%94%A8%E6%8F%92 ...
- C++11_Type Traits
版权声明:本文为博主原创文章,未经博主允许不得转载. 识别变量的type id,返回true或false,举一个简单使用的例子 template <typename T> void typ ...
- 从头搭建一个React应用
node,webpack这些就不一一介绍怎么安装了,默认大家都知道. 一.npm install -g create-react-app 首先全局安装react的脚手架工具 create-react- ...