几个面试会让你实现的函数

1. atoi

需要考虑以下特殊情况:

  • 1.空字符串或者字符串长度为空
  • 2.错误输出标示(用一个全局变量实现)
  • 3.加号和减号
  • 4.其他非法字符
  • 5.溢出
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;

class Solution {

private:
    
    enum Status{valid = 0, invalid = 1};
    Status g_status = invalid;
    
public:
    
    long long core(const char* cstr, int minus) {
        long long num = 0;
        
        while (*cstr != '\0') {
            if (*cstr <= '9' && *cstr >= '0') {
                num = num * 10 + minus * (*cstr - '0');
                if ((minus > 0 && num > 0x7FFFFFFF) || (minus < 0 && num < (signed int)0x80000000)) {
                    num = 0;
                    return num;
                }
                
                cstr++;
            }
            else {
                num = 0;
                return num;
            }
        }
        
        g_status = valid;
        return num;
    }
    
    int StrToInt(string str) {
        g_status = invalid;
        long long num = 0;
        
        const char* cstr = str.c_str();
        
        if(cstr != NULL && *cstr != '\0') {
            // 1. get minus
            int minus = 1;
            if(*cstr == '+')
                cstr++;
            else if (*cstr == '-') {
                minus = -1;
                cstr++;
            }
            
            // get num
            if (*cstr != '\0')
                num = core(cstr, minus);
        }
        
        return (int)num;
    }
    
};

2. strcpy

需要注意的地方:

  • 1.参数源字符串加const
  • 2.对源地址和目的地址加非0断言
  • 3.为了实现链式操作,将目的地址返回
char * strcpy( char *strDest, const char *strSrc )
    {
        assert( (strDest != NULL) && (strSrc != NULL) );
        char *address = strDest;
        while( (*strDest++ = * strSrc++) != '\0' );
        return address; 
    }

// 或者下面这种实现也行
char* strcpy1(char *strDest, const char* strSrc)
{
       assert(strSrc != NULL );
       assert(strDest != NULL);
       int i;
       char *address = strDest;
 
    for(i = 0; strSrc[i] != '\0'; i++)
              strDest[i] = strSrc[i];
       strDest[i] = '\0';
 
       return address;
}

3. strlen

size_t  strlen (const char * str)
{
        const char *eos = str;
        while( *eos++ ) ;
        return( eos - str - 1 );
}

4. strstr

在str1里寻找是否有str2子串,若有,则返回str2在str1里的第一个指针,若没有,则返回NULL

#include 
#include 
using namespace std;


const char* StrStr(const char* str, const char* sub_str) {
    assert(str != NULL && sub_str != NULL);
    
    for (int i = 0; str[i] != '\0'; i++) {
        int tmp = i;
        int j = 0;
        while (str[i++] == sub_str[j++]) {
            if(sub_str[j] == '\0')
                return &str[tmp];
        }
        i = tmp;
    }
    
    return NULL;
}

int main()
{
    const char *str1 = "wangyang";
    const char *str2 = "ang";
    const char *res = StrStr(str1, str2);
    
    if(res != NULL)
            cout<

5. Memcpy

不用考虑内存重叠

void* memcpy(void* dst, const void* src, size_t size) {
        assert(dst != NULL && src != NULL);
        
        char *dst_tmp = (char*)dst;
        char *src_temp = (char*)src;
        
        while(size--) {
            *dst_tmp = *src_temp;
            dst_tmp++;
            src_temp++;
        }
        
        return dst;
    }

6. Memmove

需要考虑内存重叠, 但两种重叠只需考虑一种

void* memmove(void* dst, const void* src, size_t size) {
        assert(dst != NULL && src != NULL);
        
        char *dst_tmp = (char*)dst;
        char *src_tmp = (char*)src;
        
        if(src_tmp < dst_tmp && dst_tmp < src_tmp + size) {
            dst_tmp = dst_tmp + size - 1;
            src_tmp = src_tmp + size - 1;
            
            while (size -- ) {
                *dst_tmp = *src_tmp;
                dst_tmp--;
                src_tmp--;
            }
        }
        else {
            while(size--) {
                *dst_tmp = *src_tmp;
                dst_tmp++;
                src_tmp++;
            }
        }
        
        return dst;
    }

7. 判断链表是否有环

class Solution {
public:
    bool hasCycle(ListNode *head) {
        ListNode* p1 = head;
        ListNode* p2 = head;
        
        while (p2 && p2->next)
        {
            p1 = p1->next;
            p2 = p2->next->next;
            
            if (p1 == p2)
                return true;
        }
        
        return false;
    }
};

8. 快速排序

class Solution {
public:
    void quickSort(vector& arr, int left, int right) {
        if(left < right) {
            int mid = partition(arr, left, right);
            quickSort(arr, left, mid - 1);
            quickSort(arr, mid + 1, right);
        }
    }
    
private:
    int partition(vector& arr, int left, int right) {
        int l = left;
        int r = right;
        
        int length = right - left + 1;
        int randIndex = left + (rand() % length);
        swap(arr[randIndex], arr[left]);
        
        
        int target = arr[l];
        
        while(l < r) {
            while(l < r && arr[r] >= target) {
                r--;
            }
            
            if(l < r){
                arr[l] = arr[r];
                l++; 
            }
            
            while(l < r && arr[l] <= target) {
                l++;
            }
            
            if(l < r) {
                arr[r] = arr[l];
                r--;
            }
        }
        
        arr[l] = target;
        
        return l;
    }
};

9. 单例模式

class Singleton {
private:
    Singleton(){};
    static const Singleton* m_instance;
public:
    static const Singleton* getInstance() {
        return m_instance;
    }
       
};

const Singleton* Singleton::m_instance = new Singleton;

你可能感兴趣的:(几个面试会让你实现的函数)