剑指offer(刷题41-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 here
        tempList = []
        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 here
        strLen = 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 here
        strList = s.split(" ")
        for i in range(len(strList)/2):
            temp = strList[i]
            strList[i] = strList[len(strList) - 1 - i]
            strList[len(strList) - 1 - i] = temp
        return " ".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 True
        return False

    def IsContinuous(self, numbers):
        # write code here
        if len(numbers) != 5:
            return False
        numbers.sort()
        zeroCount = 0

        for i in numbers:
            if i==0:
                zeroCount += 1
        if self.isReat(numbers[zeroCount:]):
            return False

        if numbers[-1] - numbers[zeroCount] < 5:
            return True
        else:
            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 here
        if n==0 or m==0:
            return -1
        start = 0 
        tempList = [i for i in range(n)]
        while len(tempList) > 1:
            out = ((start + m) % len(tempList)) - 1
            if out < 0:
                out = out + len(tempList)
            start = out
            del 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 here
        MAX = 0X7FFFFFFF
        MASK = 0XFFFFFFFF
        
        ans = num1
        while num2 !=0:
            ans = (num1 ^ num2) & MASK
            num2 = ((num1 & num2)<<1) & MASK
            num1 = ans
        return 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 here
        tempStrList = list(s)
        if tempStrList == []:
            return 0
        for dataIndex in range(len(tempStrList)):
            if tempStrList[dataIndex].isalpha():
                if (tempStrList[dataIndex]=="+" or tempStrList[dataIndex]=="-") and dataIndex == 0:
                    pass
                else:
                    return 0
        sum = 0
        if tempStrList[0] == "+":
            for i in range(1,len(tempStrList)):
                sum += int(tempStrList[i]) * (10**(len(tempStrList)-i-1))
            return sum
        elif tempStrList[0] == "-":
            for i in range(1,len(tempStrList)):
                sum += int(tempStrList[i]) * (10**(len(tempStrList)-i-1))
            return -sum
        else:
            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 false
    bool 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 -*-

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值