编程之旅-Day5

Day5的学习涵盖了剑指Offer的面试题,包括旋转数组中的最小数字和替换空格,还涉及华为机试题如0交换排序,以及华为校招勇敢星实习生笔试题目的整数翻转求和和掷骰子问题。提供了相关解题思路和正确代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Day5-学习内容:

(1)剑指Offer

面试题11:旋转数组中的最小数字

面试题5:替换空格

(2)华为机试题-0交换排序

(3)华为2018届校招勇敢星实习生招聘笔试

例1.整数翻转求和

例2.掷骰子

(4)拓展练习:华为机试题

 

1.剑指Offer

面试题11:旋转数组中的最小数字

题目描述:

把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 输入一个非减排序的数组的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1,2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 NOTE:给出的所有元素都大于0,若数组大小为0,请返回0。

书上解法:

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        if(rotateArray.empty()==0){
            return 0;
        }
        int index1=0;
        int index2=rotateArray.size()-1;
        int indexmid=index1;
        while(rotateArray[index1]>=rotateArray[index2]){
            if(index2-index1==1){
                indexmid=index2;
                break;
            }
            indexmid=(index2-index1)/2;
            if(rotateArray[index1]==rotateArray[index2]&&rotateArray[indexmid]==rotateArray[index1]){
                int result=rotateArray[index1];
                for(int i=index1+1;i<=index2;i++){
                    if(result>rotateArray[i]){
                        result=rotateArray[i];
                    }
                }
                return result;
            }
            if(rotateArray[indexmid]>=rotateArray[index1]){
                index1=indexmid;
            }
            else if(rotateArray[indexmid]<=rotateArray[index2]){
                index2=indexmid;
            }
        }
        return rotateArray[indexmid];
        
    }
};

正确解法:(已调试通过!)

class Solution {
public:
    int minNumberInRotateArray(vector<int> rotateArray) {
        if (rotateArray.empty()) return 0;
        int left = 0, right = rotateArray.size() - 1;
        while (left < right) {
            //确认子数组是否是类似1,1,2,4,5,..,7的非递减数组
            if (rotateArray[left] < rotateArray[right]) return rotateArray[left];
             
            int mid = left + (right - left) / 2;
            //如果左半数组为有序数组
            if (rotateArray[left] < rotateArray[mid])
                left = mid + 1;
            //如果右半数组为有序数组
            else if (rotateArray[mid] < rotateArray[right])
                right = mid;
            //否则,rotateArray[left] == rotateArray[mid] == rotateArray[right]
            else {
                ++left;
            }
        }
        return rotateArray[left];
    }
};

 

面试题5:替换空格

题目描述:

请实现一个函数,将一个字符串中的每个空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。

正确解法:(已调试通过!)

class Solution {
public:
    void replaceSpace(char *str,int length) {
        if(str==NULL||length<=0){
            return;
        }
        int origlen=0;
        int numberofblank=0;
        int i=0;
        while(str[i]!='\0'){
            ++origlen;
            if(str[i]==' '){
                ++numberofblank;
            }
            ++i;
        }
        int newlen=origlen+numberofblank*2;
        char *origptr=str+origlen;
        char *newptr=str+newlen;
        if(newlen+1>length){
            return;
        }
        while(origptr<newptr){
            if(*origptr==' '){
                *newptr--='0';
                *newptr--='2';
                *newptr--='%';
            }
            else{
                *newptr--=*origptr;
            }
            --origptr;
        }
    }
};

注意:书上是字符数组,上述代码定义的是指针,复制的时候有区别需用指针操作进行替换。

 

2.华为机试题-0交换排序

题目描述:长度为n的数组乱序存放着0至n-1,现在只能进行0与其他数的交换,完成以下函数。

思路:

循环交换。先将0与处于i位置的数字进行交换,即swapWithZero(array, len, array[i]);此时0处于i位置,然后交换0与数字i,即可让数字i归位到i位置。

正确答案:

public void sort(int[] array, int len) {
    for(int i = len - 1; i > 0; i--) {
        if(array[i] == i) //判断是否处于正确位置
            continue;
        //对未处于正确位置的数字进行下面两步交换
        swapWithZero(array, len, array[i]);//交换0与i位置的数字
        swapWithZero(array, len, i);//交换0与数字i
    }
}

参考:https://blog.csdn.net/universe_ant/article/details/76959290

 

3.华为2018届校招勇敢星实习生招聘笔试

参考:https://blog.csdn.net/Jacky_chenjp/article/details/73433824

例1.整数翻转求和

程序:

int reverse(int x) {
      int a = 0;
      do {
        a = a * 10 + (x % 10);
        x /= 10;
      } while(x > 0);
      return a;
    }

    int reverseAdd(int a, int b) {
      if (a < 1 || a > 70000 || b < 1 || b > 70000) {
        return -1;
      }
      return reverse(a) + reverse(b);
    }

    #include "stdio.h"
    int main() {
      int a, b;
      scanf("%d,%d", &a, &b);
      printf("%d\n", reverseAdd(a, b));
      return 0;
    }

例2.掷骰子

https://blog.csdn.net/jocelyn92/article/details/69943043

https://blog.csdn.net/Jacky_chenjp/article/details/73433824

正确代码:

#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
#include <iostream>
 
int state[6] = {1,2,3,4,5,6};//存放最终状态的数组
 
int stateL = 1;    
int stateR = 2;
int stateF = 3;
int stateB = 4;
int stateU = 5;
int stateD = 6;
 
using namespace std;
 
int main(int argc, char *argv[])
{
    char str[50];
    int numCount = 0;
    cin.getline(str,50);
    for (int i = 0; i < 50; i++)
    {
        if (str[i] >0x40 && str[i]<0x53)
        {
            numCount++;
        }
 
        else
        {
            break;
        }
    }
    for (int j = 0; j < numCount; j++)
    {
        switch (str[j])
        {
        case 'L':
            ActionL();
            break;
        case 'R':
            ActionR();
            break;
        case 'F':
            ActionF();
            break;
        case 'B':
            ActionB();
            break;
        case 'A':
            ActionA();
            break;
        case 'C':
            ActionC();
            break;
        default:
            break;
        }
    }
    cout << state[0] << state[1] << state[2] << state[3] << state[4] << state[5];
 
    return 0;
}
 
void ActionL(void)
{
    int temp1 = 0;
    int temp2 = 0;
 
    temp1 = stateL;
    temp2 = stateR;
 
    stateL = stateU;
    stateR = stateD;
 
    stateD = temp1;
    stateU = temp2;
 
    state[0] = stateL;
    state[1] = stateR;
    state[2] = stateF;
    state[3] = stateB;
    state[4] = stateU;
    state[5] = stateD;
}
 
void ActionR(void)
{
    int temp1 = 0;
    int temp2 = 0;
 
    temp1 = stateL;
    temp2 = stateR;
 
    stateL = stateD;
    stateR = stateU;
 
    stateD = temp2;
    stateU = temp1;
 
    state[0] = stateL;
    state[1] = stateR;
    state[2] = stateF;
    state[3] = stateB;
    state[4] = stateU;
    state[5] = stateD;
}
 
void ActionF(void)
{
    int temp1 = 0;
    int temp2 = 0;
 
    temp1 = stateF;
    temp2 = stateB;
 
    stateF = stateU;
    stateB = stateD;
 
    stateD = temp1;
    stateU = temp2;
 
    state[0] = stateL;
    state[1] = stateR;
    state[2] = stateF;
    state[3] = stateB;
    state[4] = stateU;
    state[5] = stateD;
}
 
void ActionB(void)
{
    int temp1 = 0;
    int temp2 = 0;
 
    temp1 = stateF;
    temp2 = stateB;

    stateF = stateD;
    stateB = stateU;
 
    stateD = temp2;
    stateU = temp1;
 
    state[0] = stateL;
    state[1] = stateR;
    state[2] = stateF;
    state[3] = stateB;
    state[4] = stateU;
    state[5] = stateD;
}
 
void ActionA(void)
{
    int temp1 = 0;
    int temp2 = 0;
 
    temp1 = stateF;
    temp2 = stateB;
 
    stateF = stateL;
    stateB = stateR;
 
    stateL = temp2;
    stateR = temp1;
 
    state[0] = stateL;
    state[1] = stateR;
    state[2] = stateF;
    state[3] = stateB;
    state[4] = stateU;
    state[5] = stateD;
}
 
void ActionC(void)
{
    int temp1 = 0;
    int temp2 = 0;
 
    temp1 = stateF;
    temp2 = stateB;
 
    stateF = stateR;
    stateB = stateL;
 
    stateL = temp1;
    stateR = temp2;
 
    state[0] = stateL;
    state[1] = stateR;
    state[2] = stateF;
    state[3] = stateB;
    state[4] = stateU;
    state[5] = stateD;
}

4.拓展练习:华为机试题

https://blog.csdn.net/zhangxiao93/article/details/73744394

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值