文章目录

  • 目录
    • 第41题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第42题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第43题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第44题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第45题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第46题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第47题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第48题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第49题:
      • 解题思路:
      • 代码实现:
        • c++
        • python
    • 第50题:
      • 解题思路:
      • 代码实现:
        • c++
        • python

目录

第41题:

输入一个递增排序的数组和一个数字S,在数组中查找两个数,使得他们的和正好是S,如果有多对数字的和等于S,输出两个数的乘积最小的。

解题思路:

  • 暴力法:就是双层循环遍历,这个方法就没有考虑到题目中递增排序的信息。
  • 使用两个指针,分别指向头和尾,当两个数相聚越远,则其乘机越小,所以按照头尾指针两边夹方式,第一次出现相等的两个数乘机肯定最小。

代码实现:

c++

vector<int> FindNumbersWithSum(vector<int> array,int sum) {vector<int> resArray;if(array.size() <= 1){return resArray;}int tempL=0;int tempR = array.size() - 1;while(tempL < tempR){if(array[tempL] + array[tempR] == sum){resArray.push_back(array[tempL]);resArray.push_back(array[tempR]);break;}else if(array[tempL] + array[tempR] < sum){tempL++;}else{tempR --;}}return resArray;
}

运行时间:3ms

占用内存:484k

python

# -*- coding:utf-8 -*-
class Solution:def FindNumbersWithSum(self, array, tsum):# write code heretempList = []for i in range(len(array)):for j in range(i,len(array)):if array[i] + array[j] == tsum:tempList.append([array[i] , array[j]])if tempList == []:return []tempData = tempList[0][0] * tempList[0][1]littleNum = tempList[0][0]bigNum = tempList[0][1]for item in tempList:if item[0] * item[1] < tempData:littleNum = item[0]bigNum = item[1]return littleNum,bigNum

运行时间:28ms

占用内存:5732k

第42题:

汇编语言中有一种移位指令叫做循环左移(ROL),现在有个简单的任务,就是用字符串模拟这个指令的运算结果。对于一个给定的字符序列S,请你把其循环左移K位后的序列输出。例如,字符序列S=”abcXYZdef”,要求输出循环左移3位后的结果,即“XYZdefabc”。是不是很简单?OK,搞定它!

解题思路:

  • 循环左移就像循环队列中的入队操作,只需要定义循环后的index = (index + n) % strLen即可。时间复杂度为O(N)

代码实现:

c++

class Solution {public:string LeftRotateString(string str, int n) {int strLen = str.length();string resStr(str);for(int i=0 ; i < strLen ; i++){resStr[i] = str[(i+n)%strLen];}return resStr;}
};

运行时间:3ms

占用内存:488k

python

# -*- coding:utf-8 -*-
class Solution:def LeftRotateString(self, s, n):# write code herestrLen = len(s)tempList = []for i in range(strLen):tempList.append(s[((i+n)%strLen)])return "".join(tempList)

运行时间:24ms

占用内存:5732k

第43题:

牛客最近来了一个新员工Fish,每天早晨总是会拿着一本英文杂志,写些句子在本子上。同事Cat对Fish写的内容颇感兴趣,有一天他向Fish借来翻看,但却读不懂它的意思。例如,“student. a am I”。后来才意识到,这家伙原来把句子单词的顺序翻转了,正确的句子应该是“I am a student.”。Cat对一一的翻转这些单词顺序可不在行,你能帮助他么?

解题思路:

  • 首先将字符串按照空格进行分割,并保存为数组,然后交换数组的前面和后面的数字,最好将数字转换为字符串。
  • 使用栈辅助

代码实现:

c++

class Solution {public:string ReverseSentence(string str) {string res = "", tmp = "";for(unsigned int i = 0; i < str.size(); ++i){if(str[i] == ' ') res = " " + tmp + res, tmp = "";else tmp += str[i];}if(tmp.size()) res = tmp + res;return res;}
};

运行时间:3ms

占用内存:604k

python

# -*- coding:utf-8 -*-
class Solution:def ReverseSentence(self, s):# write code herestrList = s.split(" ")for i in range(len(strList)/2):temp = strList[i]strList[i] = strList[len(strList) - 1 - i]strList[len(strList) - 1 - i] = tempreturn " ".join(strList)

运行时间:27ms

占用内存:5856k

第44题:

LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。

解题思路:

  • 对数组进行从小到大排序,然后统计0的个数,针对非0的数值需要满足三个条件:最大值与最小值的差值小于5 ; 不存在重复数字,数组的长度为5

代码实现:

c++

class Solution {public:bool IsContinuous( vector<int> numbers ) {if(numbers.size() !=5 ){return false;}sort(numbers.begin(),numbers.end());int zeroCount = 0;for(int i = 0;i<numbers.size() ; i++){if(numbers[i] == 0) zeroCount ++;}for(int i = zeroCount;i< numbers.size()-1 ; i++){if(numbers[i+1] - numbers[i] == 0){return false;}}if(numbers[numbers.size()-1] - numbers[zeroCount] < 5) return true;else return false;   }
};

运行时间:4ms

占用内存:604k

python

# -*- coding:utf-8 -*-
class Solution:def isReat(self,dataList):for i in range(len(dataList)-1):if (dataList[i+1] - dataList[i]) == 0:return Truereturn Falsedef IsContinuous(self, numbers):# write code hereif len(numbers) != 5:return Falsenumbers.sort()zeroCount = 0for i in numbers:if i==0:zeroCount += 1if self.isReat(numbers[zeroCount:]):return Falseif numbers[-1] - numbers[zeroCount] < 5:return Trueelse:return False

运行时间:30ms

占用内存:5852k

第45题:

每年六一儿童节,牛客都会准备一些小礼物去看望孤儿院的小朋友,今年亦是如此。HF作为牛客的资深元老,自然也准备了一些小游戏。其中,有个游戏是这样的:首先,让小朋友们围成一个大圈。然后,他随机指定一个数m,让编号为0的小朋友开始报数。每次喊到m-1的那个小朋友要出列唱首歌,然后可以在礼品箱中任意的挑选礼物,并且不再回到圈中,从他的下一个小朋友开始,继续0…m-1报数…这样下去…直到剩下最后一个小朋友,可以不用表演,并且拿到牛客名贵的“名侦探柯南”典藏版(名额有限哦!!_)。请你试着想下,哪个小朋友会得到这份礼品呢?(注:小朋友的编号是从0到n-1)

解题思路:

  • 根据公式out = (start + m) % n - 1,将小朋友看成一个数组,每一选中一个人就删除数组中的一个数字,同时移动数组中的一些元素,然后重新赋值start , n ,最后判断数组只有一个元素的时候,返回此时数组的值便可。

代码实现:

c++

class Solution {public:int LastRemaining_Solution(int n, int m){if(n==0 || m==0) return -1;vector<int> dataList;for(int i =0 ;i < n ; i++) dataList.push_back(i);int start = 0;  //vec.erase(vec.begin()+idx);while(dataList.size() > 1){int outIndex = ((start+m)%dataList.size()) - 1;if(outIndex < 0) outIndex = outIndex + dataList.size();start = outIndex;dataList.erase(dataList.begin()+outIndex);}return dataList[0];}
};

运行时间:4ms

占用内存:504k

python

# -*- coding:utf-8 -*-
class Solution:def LastRemaining_Solution(self, n, m):# write code hereif n==0 or m==0:return -1start = 0 tempList = [i for i in range(n)]while len(tempList) > 1:out = ((start + m) % len(tempList)) - 1if out < 0:out = out + len(tempList)start = outdel tempList[out]return tempList[0]

运行时间:28ms

占用内存:5752k

第46题:

求1+2+3+…+n,要求不能使用乘除法、for、while、if、else、switch、case等关键字及条件判断语句(A?B:C)。

解题思路:

  • 由于本题中规定不能用乘除法,循环和选择,所以自然想到的是用递归和位操作来计算了。但是由于没法用if判断,所以递归的终止条件是一个棘手的问题,于是我们在想到了短路求值原理。
    需利用逻辑与的短路特性实现递归终止。 2.当n==0时,(n>0)&&((sum+=Sum_Solution(n-1))>0)只执行前面的判断,为false,然后直接返回0;
    3.当n>0时,执行sum+=Sum_Solution(n-1),实现递归计算Sum_Solution(n)。

代码实现:

c++

class Solution {public:int Sum_Solution(int n) {int ans = n;ans && (ans += Sum_Solution(n - 1));return ans;}
};

运行时间:4ms

占用内存:480k

python

# -*- coding:utf-8 -*-

第47题:

写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。

解题思路:

  • 根据题意来看是通过位操作实现两个整数的加法。

代码实现:

c++

class Solution {public:int Add(int num1, int num2){int x = num1 ^ num2;int y = num1 & num2;while(y!=0){y = y << 1;int temp = x;x = x^y;y = temp & y;}return x;}
};

运行时间:3ms

占用内存:396k

python

# -*- coding:utf-8 -*-
class Solution:def Add(self, num1, num2):# write code hereMAX = 0X7FFFFFFFMASK = 0XFFFFFFFFans = num1while num2 !=0:ans = (num1 ^ num2) & MASKnum2 = ((num1 & num2)<<1) & MASKnum1 = ansreturn ans if ans<=MAX else ~(ans ^ MASK)

运行时间:28ms

占用内存:5848k

第48题:

将一个字符串转换成一个整数(实现Integer.valueOf(string)的功能,但是string不符合数字要求时返回0),要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。

解题思路:

  • 将字符串转换为字符串数组,然后通过强制类型转换将字符转换为数字,然后根据字符的索引,求和得到十进制的表示,但是得对一些特殊情况做处理。

代码实现:

c++

int StrToInt(string str) {int lenStr = str.length();if(lenStr == 0){return 0;}char dataFlag = '0';vector<int>  tempArray;if(str[0] == '+' || str[0]=='-'){dataFlag = str[0];for(int i = 1 ;i < lenStr ; i++){if(int(str[i]) < 48 || int(str[i]) > 57) return 0;else tempArray.push_back(int(str[i])-48);}}else{for(int i =0 ; i < lenStr ; i++){if(int(str[i]) < 48 || int(str[i]) > 57) return 0;else tempArray.push_back(int(str[i])-48);}}int sum = 0;if(dataFlag == '+' || dataFlag == '0'){for(int i = 0 ; i < tempArray.size() ; i++){sum += tempArray[i] * pow(10,tempArray.size() - 1 - i);}return sum;}else if (dataFlag == '-'){for(int i = 0 ; i < tempArray.size() ; i++){sum += tempArray[i] * pow(10,tempArray.size() - 1 - i);}return -sum;}
}

python

# -*- coding:utf-8 -*-
class Solution:def StrToInt(self, s):# write code heretempStrList = list(s)if tempStrList == []:return 0for dataIndex in range(len(tempStrList)):if tempStrList[dataIndex].isalpha():if (tempStrList[dataIndex]=="+" or tempStrList[dataIndex]=="-") and dataIndex == 0:passelse:return 0sum = 0if tempStrList[0] == "+":for i in range(1,len(tempStrList)):sum += int(tempStrList[i]) * (10**(len(tempStrList)-i-1))return sumelif tempStrList[0] == "-":for i in range(1,len(tempStrList)):sum += int(tempStrList[i]) * (10**(len(tempStrList)-i-1))return -sumelse:for i in range(len(tempStrList)):sum += int(tempStrList[i]) * (10**(len(tempStrList)-i-1))return sum

运行时间:31ms

占用内存:5728k

第49题:

在一个长度为n的数组里的所有数字都在0到n-1的范围内。 数组中某些数字是重复的,但不知道有几个数字是重复的。也不知道每个数字重复几次。请找出数组中任意一个重复的数字。 例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

解题思路:

  • 使用2个指针,一个大指针和一个小指针,然后比较两个值的大小,重复则直接退出。
    

代码实现:

c++

class Solution {public:// Parameters://        numbers:     an array of integers//        length:      the length of array numbers//        duplication: (Output) the duplicated number in the array number// Return value:       true if the input is valid, and there are some duplications in the array number//                     otherwise falsebool duplicate(int numbers[], int length, int* duplication) {if(length<=1) return false;int little = 0 , big = 1;for(int i = 0 ;i < length ; i++){for(int j = i + 1 ; j < length ; j++){if(numbers[i] == numbers[j]){*duplication = numbers[i];return true;}}}return false;}
};

运行时间:3ms

占用内存:468k

python

# -*- coding:utf-8 -*-

第50题:

给定一个数组A[0,1,…,n-1],请构建一个数组B[0,1,…,n-1],其中B中的元素B[i]=A[0]A[1]…*A[i-1]A[i+1]…*A[n-1]。不能使用除法。

解题思路:

  • 参考网上:
    B[i]的值可以看作下图的矩阵中每行的乘积。
    下三角用连乘可以很容求得,上三角,从下向上也是连乘。
    因此我们的思路就很清晰了,先算下三角中的连乘,即我们先算出B[i]中的一部分,然后倒过来按上三角中的分布规律,把另一部分也乘进去

代码实现:

c++

class Solution {public:vector<int> multiply(const vector<int>& A) {int n=A.size();vector<int> b(n);int ret=1;for(int i=0;i<n;ret*=A[i++]){b[i]=ret;}ret=1;for(int i=n-1;i>=0;ret*=A[i--]){b[i]*=ret;}return b;}
};

运行时间:4ms

占用内存:376k

python

# -*- coding:utf-8 -*-

剑指offer(刷题41-50)--c++,Python版本相关推荐

  1. 剑指offer(刷题51-60)--c++,Python版本

    文章目录 目录 第51题: 解题思路: 代码实现: c++ python 第52题: 解题思路: 代码实现: c++ python 第53题: 解题思路: 代码实现: c++ python 第54题: ...

  2. 剑指offer(刷题61-65)--c++,Python版本

    文章目录 目录 第61题: 解题思路: 代码实现: c++ python 第62题: 解题思路: 代码实现: c++ python 第63题: 解题思路: 代码实现: c++ python 第64题: ...

  3. 剑指offer(刷题31-40)--c++,Python版本

    文章目录 目录 第31 题: 解题思路: 代码实现: c++ python 第32题: 解题思路: 代码实现: c++ python 第33题: 解题思路: 代码实现: c++ python 第34题 ...

  4. 剑指offer(刷题21-30)--c++,Python版本

    文章目录 目录 第 21题: 解题思路: 代码实现: c++ python 第22 题: 解题思路: 代码实现: c++ python 第23 题: 解题思路: 代码实现: c++ python 第2 ...

  5. 原 剑指offer(刷题11-20)--c++,Python版本

    文章目录 目录 第11题: 解题思路: 代码实现: c++ python 第12题: 解题思路: 代码实现: c++ python 第13 题: 解题思路: 代码实现: c++ python 第 14 ...

  6. 剑指offer(刷题1-10)--c++,Python版本

    文章目录 目录 第一题: 解题思路: 代码实现: c++ 顺序查找 二分查找 Python 第二题: 解题思路: 代码实现: c++ python 第三题: 解题思路: 代码实现: c++ 使用栈辅助 ...

  7. 【LeetCode 剑指offer刷题】矩阵题1:4 有序矩阵中的查找( 74. Search a 2D Matrix )(系列)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 74. Search a 2D Matrix Write an efficient algorithm that s ...

  8. 【LeetCode 剑指offer刷题】树题6:28 对称二叉树(101. Symmetric Tree)

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 101. Symmetric Tree /**  * Definition for a binary tree no ...

  9. 【LeetCode 剑指offer刷题】数组题2:57 有序数组中和为s的两个数(167 Two Sum II - Input array is sorted)...

    [LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 57 有序数组中和为s的两个数 题目描述 输入一个递增排序的数组和一个数字S,在数组中查找两个数,是的他们的和正好是 ...

最新文章

  1. 半导体群聚、虚拟垂直、整合
  2. 接近WinHEC 2008
  3. 20160208.CCPP体系具体解释(0018天)
  4. Android7.1修改系统默认多媒体音量大小
  5. 读书推荐:2017 第一期
  6. Linux系统中/dev/mtd与/dev/mtdblock的区别
  7. Linux/Unix下tar命令详解
  8. 《四世同堂》金句摘抄(十一)
  9. 百度编辑器回显js报错Uncaught SyntaxError: Invalid or unexpected token
  10. Go语言第一深坑 - interface 与 nil 的比较 (转)
  11. 单片机音频谱曲软件_单片机音乐代码转换工具(Music Encode)
  12. 做自己最好的生活大师
  13. 我叫mt4公会攻城战服务器维护中,公会攻城战也要讲战术《我叫MT4》攻城战策略解析...
  14. MyBatis 常见面试题有哪些?
  15. 我的 OneNote 入门心得
  16. 【武忠祥高等数学基础课笔记】第二章 导数与微分
  17. c盘越来越大怎么清理?清理C:\Windows\System32\DriverStore\FileRepository
  18. 几种数据源的配置方式
  19. 陈力:传智播客古代 珍宝币 泡泡龙游戏开发第29讲:PHP排序和查找
  20. 32套企业店铺展示微信小程序源码模板集合

热门文章

  1. php 5.6连接sqlserver,wamp php5.6连接sqlserver
  2. 【转】.Net中的异步编程总结
  3. 【转】对ASP.NET程序员非常有用的85个工具
  4. ABP入门系列(3)——领域层定义仓储并实现
  5. 【转】3.1SharePoint服务器端对象模型 之 访问文件和文件夹(Part 1)
  6. linux 自动连接无限,hotplug应用实例:自动连接无线网
  7. 基于matlab的数字下变频器的设计与仿真应用,基于MATLAB的数字下变频器的设计与仿真应用.pdf...
  8. stl源码剖析_STL源码剖析 阅读笔记(二)allocator
  9. 流水灯verilog实验原理_IC设计实例解析之“流水线技术”
  10. HashMap和ConcurrentHashMap