剑指offer

2018-03-02  本文已影响0人  我是谁的小超人
class Solution {
public:
    bool Find(int target, vector<vector<int> > array) {
        if(array.size()!=0)
        {
            int row = 0;
            int col = array[0].size() - 1;
            while(row<array.size() && col>= 0)
            {
                if(array[row][col]==target)
                    return true;
                 else if(array[row][col]>target)
                     --col;
                 else
                     ++row;
            }
        }
        return false;
    }
};
class Solution {
public:
    void replaceSpace(char *str,int length) {
        if(str == NULL || length <= 0)
            return;
        int originalLength = 0;
        int numOfBlank = 0;
        for(int i = 0;str[i] != '\0';++i)
        {
            ++originalLength;
            if(str[i]==' ')
                ++numOfBlank;
        }
        int newLength = originalLength + 2*numOfBlank;
        if(newLength > length)
            return;
        int indexOfOriginal = originalLength;
        int indexOfNew = newLength;
        while(indexOfOriginal >= 0 && indexOfNew > indexOfOriginal)
        {
            if(str[indexOfOriginal] == ' ')
            {
                str[indexOfNew --] = '0';
                str[indexOfNew --] = '2';
                str[indexOfNew --] = '%';
            }
            else
            {
                str[indexOfNew --] = str[indexOfOriginal];
            }
            -- indexOfOriginal;
        }
             
    }
};
//i++是后缀运算,就是先取出i的值,i再+1;++i是前缀运算,就是i先+1,再取i的值。
上一篇下一篇

猜你喜欢

热点阅读