字符串

20、【剑指Offer学习】【面试题20:表示数值的字符串】

38、【剑指Offer学习】【面试题38:字符串的排列】

46、【剑指Offer学习】【面试题46:把数字翻译成字符串】

48、【剑指Offer学习】【面试题48:最长不含重复字符的子字符串】

50、【剑指Offer学习】【面试题50:字符串中第一个只出现一次的字符】

67、【剑指Offer学习】【面试题67:把字符串转换成整数】

20. 表示数值的字符串

    /*******************************************************************
    Copyright(c) 2016, Harry He
    All rights reserved.

    Distributed under the BSD license.
    (See accompanying file LICENSE.txt at
    https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
    *******************************************************************/

    //==================================================================
    // 《剑指Offer——名企面试官精讲典型编程题》代码
    // 作者:何海涛
    //==================================================================

    // 面试题20:表示数值的字符串
    // 题目:请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,
    // 字符串“+100”、“5e2”、“-123”、“3.1416”及“-1E-16”都表示数值,但“12e”、
    // “1a3.14”、“1.2.3”、“+-5”及“12e+5.4”都不是

    #include <stdio.h>

    bool scanUnsignedInteger(const char** str);
    bool scanInteger(const char** str);

    // &#x6570;&#x5B57;&#x7684;&#x683C;&#x5F0F;&#x53EF;&#x4EE5;&#x7528;A[.[B]][e|EC]&#x6216;&#x8005;.B[e|EC]&#x8868;&#x793A;&#xFF0C;&#x5176;&#x4E2D;A&#x548C;C&#x90FD;&#x662F;
    // &#x6574;&#x6570;&#xFF08;&#x53EF;&#x4EE5;&#x6709;&#x6B63;&#x8D1F;&#x53F7;&#xFF0C;&#x4E5F;&#x53EF;&#x4EE5;&#x6CA1;&#x6709;&#xFF09;&#xFF0C;&#x800C;B&#x662F;&#x4E00;&#x4E2A;&#x65E0;&#x7B26;&#x53F7;&#x6574;&#x6570;
    bool isNumeric(const char* str)
    {
        if(str == nullptr)
            return false;

        bool numeric = scanInteger(&str);

        // &#x5982;&#x679C;&#x51FA;&#x73B0;'.'&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x662F;&#x6570;&#x5B57;&#x7684;&#x5C0F;&#x6570;&#x90E8;&#x5206;
        if(*str == '.')
        {
            ++str;

            // &#x4E0B;&#x9762;&#x4E00;&#x884C;&#x4EE3;&#x7801;&#x7528;||&#x7684;&#x539F;&#x56E0;&#xFF1A;
            // 1. &#x5C0F;&#x6570;&#x53EF;&#x4EE5;&#x6CA1;&#x6709;&#x6574;&#x6570;&#x90E8;&#x5206;&#xFF0C;&#x4F8B;&#x5982;.123&#x7B49;&#x4E8E;0.123&#xFF1B;
            // 2. &#x5C0F;&#x6570;&#x70B9;&#x540E;&#x9762;&#x53EF;&#x4EE5;&#x6CA1;&#x6709;&#x6570;&#x5B57;&#xFF0C;&#x4F8B;&#x5982;233.&#x7B49;&#x4E8E;233.0&#xFF1B;
            // 3. &#x5F53;&#x7136;&#x5C0F;&#x6570;&#x70B9;&#x524D;&#x9762;&#x548C;&#x540E;&#x9762;&#x53EF;&#x4EE5;&#x6709;&#x6570;&#x5B57;&#xFF0C;&#x4F8B;&#x5982;233.666
            numeric = scanUnsignedInteger(&str) || numeric;
        }

        // &#x5982;&#x679C;&#x51FA;&#x73B0;'e'&#x6216;&#x8005;'E'&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x8DDF;&#x7740;&#x7684;&#x662F;&#x6570;&#x5B57;&#x7684;&#x6307;&#x6570;&#x90E8;&#x5206;
        if(*str == 'e' || *str == 'E')
        {
            ++str;

            // &#x4E0B;&#x9762;&#x4E00;&#x884C;&#x4EE3;&#x7801;&#x7528;&&&#x7684;&#x539F;&#x56E0;&#xFF1A;
            // 1. &#x5F53;e&#x6216;E&#x524D;&#x9762;&#x6CA1;&#x6709;&#x6570;&#x5B57;&#x65F6;&#xFF0C;&#x6574;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x4E0D;&#x80FD;&#x8868;&#x793A;&#x6570;&#x5B57;&#xFF0C;&#x4F8B;&#x5982;.e1&#x3001;e1&#xFF1B;
            // 2. &#x5F53;e&#x6216;E&#x540E;&#x9762;&#x6CA1;&#x6709;&#x6574;&#x6570;&#x65F6;&#xFF0C;&#x6574;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#x4E0D;&#x80FD;&#x8868;&#x793A;&#x6570;&#x5B57;&#xFF0C;&#x4F8B;&#x5982;12e&#x3001;12e+5.4
            numeric = numeric && scanInteger(&str);
        }

        return numeric && *str == '\0';
    }

    bool scanUnsignedInteger(const char** str)
    {
        const char* before = *str;
        while(**str != '\0' && **str >= '0' && **str <= '9') ++(*str); 当str中存在若干0-9的数字时,返回true return *str> before;
    }

    // &#x6574;&#x6570;&#x7684;&#x683C;&#x5F0F;&#x53EF;&#x4EE5;&#x7528;[+|-]B&#x8868;&#x793A;, &#x5176;&#x4E2D;B&#x4E3A;&#x65E0;&#x7B26;&#x53F7;&#x6574;&#x6570;
    bool scanInteger(const char** str)
    {
        if(**str == '+' || **str == '-')
            ++(*str);
        return scanUnsignedInteger(str);
    }

    // ====================&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;====================
    void Test(const char* testName, const char* str, bool expected)
    {
        if(testName != nullptr)
            printf("%s begins: ", testName);

        if(isNumeric(str) == expected)
            printf("Passed.\n");
        else
            printf("FAILED.\n");
    }

    int main(int argc, char* argv[])
    {
        Test("Test1", "100", true);
        Test("Test2", "123.45e+6", true);
        Test("Test3", "+500", true);
        Test("Test4", "5e2", true);
        Test("Test5", "3.1416", true);
        Test("Test6", "600.", true);
        Test("Test7", "-.123", true);
        Test("Test8", "-1E-16", true);
        Test("Test9", "1.79769313486232E+308", true);

        printf("\n\n");

        Test("Test10", "12e", false);
        Test("Test11", "1a3.14", false);
        Test("Test12", "1+23", false);
        Test("Test13", "1.2.3", false);
        Test("Test14", "+-5", false);
        Test("Test15", "12e+5.4", false);
        Test("Test16", ".", false);
        Test("Test17", ".e1", false);
        Test("Test18", "e1", false);
        Test("Test19", "+.", false);
        Test("Test20", "", false);
        Test("Test21", nullptr, false);

        return 0;
    }
</=></stdio.h>

38. 面试题38:字符串的排列

            /*******************************************************************
            Copyright(c) 2016, Harry He
            All rights reserved.

            Distributed under the BSD license.
            (See accompanying file LICENSE.txt at
            https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
            *******************************************************************/

            //==================================================================
            // &#x300A;&#x5251;&#x6307;Offer&#x2014;&#x2014;&#x540D;&#x4F01;&#x9762;&#x8BD5;&#x5B98;&#x7CBE;&#x8BB2;&#x5178;&#x578B;&#x7F16;&#x7A0B;&#x9898;&#x300B;&#x4EE3;&#x7801;
            // &#x4F5C;&#x8005;&#xFF1A;&#x4F55;&#x6D77;&#x6D9B;
            //==================================================================

            // &#x9762;&#x8BD5;&#x9898;38&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x6392;&#x5217;
            // &#x9898;&#x76EE;&#xFF1A;&#x8F93;&#x5165;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x6253;&#x5370;&#x51FA;&#x8BE5;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x5B57;&#x7B26;&#x7684;&#x6240;&#x6709;&#x6392;&#x5217;&#x3002;&#x4F8B;&#x5982;&#x8F93;&#x5165;&#x5B57;&#x7B26;&#x4E32;abc&#xFF0C;
            // &#x5219;&#x6253;&#x5370;&#x51FA;&#x7531;&#x5B57;&#x7B26;a&#x3001;b&#x3001;c&#x6240;&#x80FD;&#x6392;&#x5217;&#x51FA;&#x6765;&#x7684;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x4E32;abc&#x3001;acb&#x3001;bac&#x3001;bca&#x3001;cab&#x548C;cba&#x3002;

            #include <cstdio>

            void Permutation(char* pStr, char* pBegin);

            void Permutation(char* pStr)
            {
                if(pStr == nullptr)
                    return;

                Permutation(pStr, pStr);
            }

            void Permutation(char* pStr, char* pBegin)
            {
                if(*pBegin == '\0')
                {
                    printf("%s\n", pStr);
                }
                else
                {
                    for(char* pCh = pBegin; *pCh != '\0'; ++ pCh)
                    {
                        char temp = *pCh;
                        *pCh = *pBegin;
                        *pBegin = temp;

                        Permutation(pStr, pBegin + 1);

                        temp = *pCh;
                        *pCh = *pBegin;
                        *pBegin = temp;
                    }
                }
            }

            // ====================&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;====================
            void Test(char* pStr)
            {
                if(pStr == nullptr)
                    printf("Test for nullptr begins:\n");
                else
                    printf("Test for %s begins:\n", pStr);

                Permutation(pStr);

                printf("\n");
            }

            int main(int argc, char* argv[])
            {
                Test(nullptr);

                char string1[] = "";
                Test(string1);

                char string2[] = "a";
                Test(string2);

                char string3[] = "ab";
                Test(string3);

                char string4[] = "abc";
                Test(string4);

                return 0;
            }
</cstdio>

46. 面试题46:把数字翻译成字符串

            /*******************************************************************
            Copyright(c) 2016, Harry He
            All rights reserved.

            Distributed under the BSD license.
            (See accompanying file LICENSE.txt at
            https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
            *******************************************************************/

            //==================================================================
            // &#x300A;&#x5251;&#x6307;Offer&#x2014;&#x2014;&#x540D;&#x4F01;&#x9762;&#x8BD5;&#x5B98;&#x7CBE;&#x8BB2;&#x5178;&#x578B;&#x7F16;&#x7A0B;&#x9898;&#x300B;&#x4EE3;&#x7801;
            // &#x4F5C;&#x8005;&#xFF1A;&#x4F55;&#x6D77;&#x6D9B;
            //==================================================================

            // &#x9762;&#x8BD5;&#x9898;46&#xFF1A;&#x628A;&#x6570;&#x5B57;&#x7FFB;&#x8BD1;&#x6210;&#x5B57;&#x7B26;&#x4E32;
            // &#x9898;&#x76EE;&#xFF1A;&#x7ED9;&#x5B9A;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#xFF0C;&#x6211;&#x4EEC;&#x6309;&#x7167;&#x5982;&#x4E0B;&#x89C4;&#x5219;&#x628A;&#x5B83;&#x7FFB;&#x8BD1;&#x4E3A;&#x5B57;&#x7B26;&#x4E32;&#xFF1A;0&#x7FFB;&#x8BD1;&#x6210;"a"&#xFF0C;1&#x7FFB;
            // &#x8BD1;&#x6210;"b"&#xFF0C;&#x2026;&#x2026;&#xFF0C;11&#x7FFB;&#x8BD1;&#x6210;"l"&#xFF0C;&#x2026;&#x2026;&#xFF0C;25&#x7FFB;&#x8BD1;&#x6210;"z"&#x3002;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x53EF;&#x80FD;&#x6709;&#x591A;&#x4E2A;&#x7FFB;&#x8BD1;&#x3002;&#x4F8B;
            // &#x5982;12258&#x6709;5&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x7FFB;&#x8BD1;&#xFF0C;&#x5B83;&#x4EEC;&#x5206;&#x522B;&#x662F;"bccfi"&#x3001;"bwfi"&#x3001;"bczi"&#x3001;"mcfi"&#x548C;
            // "mzi"&#x3002;&#x8BF7;&#x7F16;&#x7A0B;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7528;&#x6765;&#x8BA1;&#x7B97;&#x4E00;&#x4E2A;&#x6570;&#x5B57;&#x6709;&#x591A;&#x5C11;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x7FFB;&#x8BD1;&#x65B9;&#x6CD5;&#x3002;

            #include <string>
            #include <iostream>

            using namespace std;

            int GetTranslationCount(const string& number);

            int GetTranslationCount(int number)
            {
                if(number < 0)
                    return 0;

                string numberInString = to_string(number);
                return GetTranslationCount(numberInString);
            }

            int GetTranslationCount(const string& number)
            {
                int length = number.length();
                int* counts = new int[length];
                int count = 0;

                for(int i = length - 1; i >= 0; --i)
                {
                    count = 0;
                     if(i < length - 1)
                           count = counts[i + 1];
                     else
                           count = 1;

                    if(i < length - 1)
                    {
                        int digit1 = number[i] - '0';
                        int digit2 = number[i + 1] - '0';
                        int converted = digit1 * 10 + digit2;
                        if(converted >= 10 && converted <= 25) { if(i < length - 2) count +="counts[i" 2]; else } counts[i]="count;" delete[] counts; return count; =="==================&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;====================" void test(const string& testname, int number, expected) if(gettranslationcount(number)="=" cout << testname " passed." endl; failed." test1() number="0;" expected="1;" test("test1", expected); test2() test("test2", test3() test("test3", test4() test("test4", test5() test("test5", test6() test("test6", test7() test("test7", test8() test("test8", test9() test("test9", main(int argc, char* argv[]) test1(); test2(); test3(); test4(); test5(); test6(); test7(); test8(); test9(); 0; code></=></iostream></string>

48. 面试题48:最长不含重复字符的子字符串

        /*******************************************************************
        Copyright(c) 2016, Harry He
        All rights reserved.

        Distributed under the BSD license.
        (See accompanying file LICENSE.txt at
        https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
        *******************************************************************/

        //==================================================================
        // &#x300A;&#x5251;&#x6307;Offer&#x2014;&#x2014;&#x540D;&#x4F01;&#x9762;&#x8BD5;&#x5B98;&#x7CBE;&#x8BB2;&#x5178;&#x578B;&#x7F16;&#x7A0B;&#x9898;&#x300B;&#x4EE3;&#x7801;
        // &#x4F5C;&#x8005;&#xFF1A;&#x4F55;&#x6D77;&#x6D9B;
        //==================================================================

        // &#x9762;&#x8BD5;&#x9898;48&#xFF1A;&#x6700;&#x957F;&#x4E0D;&#x542B;&#x91CD;&#x590D;&#x5B57;&#x7B26;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;
        // &#x9898;&#x76EE;&#xFF1A;&#x8BF7;&#x4ECE;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x627E;&#x51FA;&#x4E00;&#x4E2A;&#x6700;&#x957F;&#x7684;&#x4E0D;&#x5305;&#x542B;&#x91CD;&#x590D;&#x5B57;&#x7B26;&#x7684;&#x5B50;&#x5B57;&#x7B26;&#x4E32;&#xFF0C;&#x8BA1;&#x7B97;&#x8BE5;&#x6700;&#x957F;&#x5B50;
        // &#x5B57;&#x7B26;&#x4E32;&#x7684;&#x957F;&#x5EA6;&#x3002;&#x5047;&#x8BBE;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x53EA;&#x5305;&#x542B;&#x4ECE;'a'&#x5230;'z'&#x7684;&#x5B57;&#x7B26;&#x3002;

        #include <string>
        #include <iostream>

        // &#x65B9;&#x6CD5;&#x4E00;&#xFF1A;&#x86EE;&#x529B;&#x6CD5;
        bool hasDuplication(const std::string& str, int position[]);

        int longestSubstringWithoutDuplication_1(const std::string& str)
        {
            int longest = 0;
            int* position = new int[26];
            for(int start = 0; start < str.length(); ++start)
            {
                for(int end = start; end < str.length(); ++end)
                {
                    int count = end - start + 1;
                    const std::string& substring = str.substr(start, count);
                    if(!hasDuplication(substring, position))
                    {
                        if(count > longest)
                            longest = count;
                    }
                    else
                        break;

                }
            }

            delete[] position;
            return longest;
        }

        bool hasDuplication(const std::string& str, int position[])
        {
            for(int i = 0; i < 26; ++i)
                position[i] = -1;

            for(int i = 0; i < str.length(); ++i)
            {
                int indexInPosition = str[i] - 'a';
                if(position[indexInPosition] >= 0)
                    return true;

                position[indexInPosition] = indexInPosition;
            }

            return false;
        }

        // &#x65B9;&#x6CD5;&#x4E00;&#xFF1A;&#x52A8;&#x6001;&#x89C4;&#x5212;
        int longestSubstringWithoutDuplication_2(const std::string& str)
        {
            int curLength = 0;
            int maxLength = 0;

            int* position = new int[26];
            for(int i = 0; i < 26; ++i)
                position[i] = -1;

            for(int i = 0; i < str.length(); ++i)
            {
                int prevIndex = position[str[i] - 'a'];
                if(prevIndex < 0 || i - prevIndex > curLength)
                    ++curLength;
                else
                {
                    if(curLength > maxLength)
                        maxLength = curLength;

                    curLength = i - prevIndex;
                }
                position[str[i] - 'a'] = i;
            }

            if(curLength > maxLength)
                maxLength = curLength;

            delete[] position;
            return maxLength;
        }

        // ====================&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;====================
        void testSolution1(const std::string& input, int expected)
        {
            int output = longestSubstringWithoutDuplication_1(input);
            if(output == expected)
                std::cout << "Solution 1 passed, with input: " << input << std::endl;
            else
                std::cout << "Solution 1 FAILED, with input: " << input << std::endl;
        }

        void testSolution2(const std::string& input, int expected)
        {
            int output = longestSubstringWithoutDuplication_2(input);
            if(output == expected)
                std::cout << "Solution 2 passed, with input: " << input << std::endl;
            else
                std::cout << "Solution 2 FAILED, with input: " << input << std::endl;
        }

        void test(const std::string& input, int expected)
        {
            testSolution1(input, expected);
            testSolution2(input, expected);
        }

        void test1()
        {
            const std::string input = "abcacfrar";
            int expected = 4;
            test(input, expected);
        }

        void test2()
        {
            const std::string input = "acfrarabc";
            int expected = 4;
            test(input, expected);
        }

        void test3()
        {
            const std::string input = "arabcacfr";
            int expected = 4;
            test(input, expected);
        }

        void test4()
        {
            const std::string input = "aaaa";
            int expected = 1;
            test(input, expected);
        }

        void test5()
        {
            const std::string input = "abcdefg";
            int expected = 7;
            test(input, expected);
        }

        void test6()
        {
            const std::string input = "aaabbbccc";
            int expected = 2;
            test(input, expected);
        }

        void test7()
        {
            const std::string input = "abcdcba";
            int expected = 4;
            test(input, expected);
        }

        void test8()
        {
            const std::string input = "abcdaef";
            int expected = 6;
            test(input, expected);
        }

        void test9()
        {
            const std::string input = "a";
            int expected = 1;
            test(input, expected);
        }

        void test10()
        {
            const std::string input = "";
            int expected = 0;
            test(input, expected);
        }

        int main(int argc, char* argv[])
        {
            test1();
            test2();
            test3();
            test4();
            test5();
            test6();
            test7();
            test8();
            test9();
            test10();

            return 0;
        }
</iostream></string>

50. 面试题50:字符串中第一个只出现一次的字符

        /*******************************************************************
        Copyright(c) 2016, Harry He
        All rights reserved.

        Distributed under the BSD license.
        (See accompanying file LICENSE.txt at
        https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
        *******************************************************************/

        //==================================================================
        // &#x300A;&#x5251;&#x6307;Offer&#x2014;&#x2014;&#x540D;&#x4F01;&#x9762;&#x8BD5;&#x5B98;&#x7CBE;&#x8BB2;&#x5178;&#x578B;&#x7F16;&#x7A0B;&#x9898;&#x300B;&#x4EE3;&#x7801;
        // &#x4F5C;&#x8005;&#xFF1A;&#x4F55;&#x6D77;&#x6D9B;
        //==================================================================

        // &#x9762;&#x8BD5;&#x9898;50&#xFF08;&#x4E00;&#xFF09;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;
        // &#x9898;&#x76EE;&#xFF1A;&#x5728;&#x5B57;&#x7B26;&#x4E32;&#x4E2D;&#x627E;&#x51FA;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;&#x3002;&#x5982;&#x8F93;&#x5165;"abaccdeff"&#xFF0C;&#x5219;&#x8F93;&#x51FA;
        // 'b'&#x3002;

        #include <cstdio>
        #include <string>

        char FirstNotRepeatingChar(const char* pString)
        {
            if(pString == nullptr)
                return '\0';

            const int tableSize = 256;
            unsigned int hashTable[tableSize];
            for(unsigned int i = 0; i < tableSize; ++i)
                hashTable[i] = 0;

            const char* pHashKey = pString;
            while(*(pHashKey) != '\0')
                hashTable[*(pHashKey++)] ++;

            pHashKey = pString;
            while(*pHashKey != '\0')
            {
                if(hashTable[*pHashKey] == 1)
                    return *pHashKey;

                pHashKey++;
            }

            return '\0';
        }

        // ====================&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;====================
        void Test(const char* pString, char expected)
        {
            if(FirstNotRepeatingChar(pString) == expected)
                printf("Test passed.\n");
            else
                printf("Test failed.\n");
        }

        int main(int argc, char* argv[])
        {
            // &#x5E38;&#x89C4;&#x8F93;&#x5165;&#x6D4B;&#x8BD5;&#xFF0C;&#x5B58;&#x5728;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;
            Test("google", 'l');

            // &#x5E38;&#x89C4;&#x8F93;&#x5165;&#x6D4B;&#x8BD5;&#xFF0C;&#x4E0D;&#x5B58;&#x5728;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;
            Test("aabccdbd", '\0');

            // &#x5E38;&#x89C4;&#x8F93;&#x5165;&#x6D4B;&#x8BD5;&#xFF0C;&#x6240;&#x6709;&#x5B57;&#x7B26;&#x90FD;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;
            Test("abcdefg", 'a');

            // &#x9C81;&#x68D2;&#x6027;&#x6D4B;&#x8BD5;&#xFF0C;&#x8F93;&#x5165;nullptr
            Test(nullptr, '\0');

            return 0;
        }
</string></cstdio>
        /*******************************************************************
        Copyright(c) 2016, Harry He
        All rights reserved.

        Distributed under the BSD license.
        (See accompanying file LICENSE.txt at
        https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
        *******************************************************************/

        //==================================================================
        // &#x300A;&#x5251;&#x6307;Offer&#x2014;&#x2014;&#x540D;&#x4F01;&#x9762;&#x8BD5;&#x5B98;&#x7CBE;&#x8BB2;&#x5178;&#x578B;&#x7F16;&#x7A0B;&#x9898;&#x300B;&#x4EE3;&#x7801;
        // &#x4F5C;&#x8005;&#xFF1A;&#x4F55;&#x6D77;&#x6D9B;
        //==================================================================

        // &#x9762;&#x8BD5;&#x9898;50&#xFF08;&#x4E8C;&#xFF09;&#xFF1A;&#x5B57;&#x7B26;&#x6D41;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;
        // &#x9898;&#x76EE;&#xFF1A;&#x8BF7;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x51FD;&#x6570;&#x7528;&#x6765;&#x627E;&#x51FA;&#x5B57;&#x7B26;&#x6D41;&#x4E2D;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x5F53;&#x4ECE;
        // &#x5B57;&#x7B26;&#x6D41;&#x4E2D;&#x53EA;&#x8BFB;&#x51FA;&#x524D;&#x4E24;&#x4E2A;&#x5B57;&#x7B26;"go"&#x65F6;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;&#x662F;'g'&#x3002;&#x5F53;&#x4ECE;&#x8BE5;&#x5B57;
        // &#x7B26;&#x6D41;&#x4E2D;&#x8BFB;&#x51FA;&#x524D;&#x516D;&#x4E2A;&#x5B57;&#x7B26;"google"&#x65F6;&#xFF0C;&#x7B2C;&#x4E00;&#x4E2A;&#x53EA;&#x51FA;&#x73B0;&#x4E00;&#x6B21;&#x7684;&#x5B57;&#x7B26;&#x662F;'l'&#x3002;

        #include <cstdio>
        #include <vector>
        #include <limits>

        using namespace std;

        class CharStatistics
        {
        public:
            CharStatistics() : index(0)
            {
                for(int i = 0; i < 256; ++i)
                    occurrence[i] = -1;
            }

            void Insert(char ch)
            {
                if(occurrence[ch] == -1)
                    occurrence[ch] = index;
                else if(occurrence[ch] >= 0)
                    occurrence[ch] = -2;

                index++;
            }

            char FirstAppearingOnce()
            {
                char ch = '\0';
                int minIndex = numeric_limits<int>::max();
                for(int i = 0; i < 256; ++i)
                {
                    if(occurrence[i] >= 0 && occurrence[i] < minIndex)
                    {
                        ch = (char) i;
                        minIndex = occurrence[i];
                    }
                }

                return ch;
            }

        private:
            // occurrence[i]: A character with ASCII value i;
            // occurrence[i] = -1: The character has not found;
            // occurrence[i] = -2: The character has been found for mutlple times
            // occurrence[i] >= 0: The character has been found only once
            int occurrence[256];
            int index;
        };

        // ====================&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;====================
        void Test(const char* testName, CharStatistics chars, char expected)
        {
            if(testName != nullptr)
                printf("%s begins: ", testName);

            if(chars.FirstAppearingOnce() == expected)
                printf("Passed.\n");
            else
                printf("FAILED.\n");
        }

        int main(int argc, char* argv[])
        {
            CharStatistics chars;

            Test("Test1", chars, '\0');

            chars.Insert('g');
            Test("Test2", chars, 'g');

            chars.Insert('o');
            Test("Test3", chars, 'g');

            chars.Insert('o');
            Test("Test4", chars, 'g');

            chars.Insert('g');
            Test("Test5", chars, '\0');

            chars.Insert('l');
            Test("Test6", chars, 'l');

            chars.Insert('e');
            Test("Test7", chars, 'l');

            return 0;
        }
</int></limits></vector></cstdio>

67. 面试题67:把字符串转换成整数

        /*******************************************************************
        Copyright(c) 2016, Harry He
        All rights reserved.

        Distributed under the BSD license.
        (See accompanying file LICENSE.txt at
        https://github.com/zhedahht/CodingInterviewChinese2/blob/master/LICENSE.txt)
        *******************************************************************/

        //==================================================================
        // &#x300A;&#x5251;&#x6307;Offer&#x2014;&#x2014;&#x540D;&#x4F01;&#x9762;&#x8BD5;&#x5B98;&#x7CBE;&#x8BB2;&#x5178;&#x578B;&#x7F16;&#x7A0B;&#x9898;&#x300B;&#x4EE3;&#x7801;
        // &#x4F5C;&#x8005;&#xFF1A;&#x4F55;&#x6D77;&#x6D9B;
        //==================================================================

        // &#x9762;&#x8BD5;&#x9898;67&#xFF1A;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x6574;&#x6570;
        // &#x9898;&#x76EE;&#xFF1A;&#x8BF7;&#x4F60;&#x5199;&#x4E00;&#x4E2A;&#x51FD;&#x6570;StrToInt&#xFF0C;&#x5B9E;&#x73B0;&#x628A;&#x5B57;&#x7B26;&#x4E32;&#x8F6C;&#x6362;&#x6210;&#x6574;&#x6570;&#x8FD9;&#x4E2A;&#x529F;&#x80FD;&#x3002;&#x5F53;&#x7136;&#xFF0C;&#x4E0D;
        // &#x80FD;&#x4F7F;&#x7528;atoi&#x6216;&#x8005;&#x5176;&#x4ED6;&#x7C7B;&#x4F3C;&#x7684;&#x5E93;&#x51FD;&#x6570;&#x3002;

        #include <cstdio>

        long long StrToIntCore(const char* str, bool minus);

        enum Status {kValid = 0, kInvalid};
        int g_nStatus = kValid;

        int StrToInt(const char* str)
        {
            g_nStatus = kInvalid;
            long long num = 0;

            if(str != nullptr && *str != '\0')
            {
                bool minus = false;
                if(*str == '+')
                    str ++;
                else if(*str == '-')
                {
                    str ++;
                    minus = true;
                }

                if(*str != '\0')
                    num = StrToIntCore(str, minus);
            }

            return (int)num;
        }

        long long StrToIntCore(const char* digit, bool minus)
        {
            long long num = 0;

            while(*digit != '\0')
            {
                if(*digit >= '0' && *digit <= 10 '9') { int flag="minus" ? -1 : 1; num="num" * + (*digit - '0'); if((!minus &&> 0x7FFFFFFF)
                        || (minus && num < (signed int)0x80000000))
                    {
                        num = 0;
                        break;
                    }

                    digit++;
                }
                else
                {
                    num = 0;
                    break;
                }
            }

            if(*digit == '\0')
                g_nStatus = kValid;

            return num;
        }

        // ====================&#x6D4B;&#x8BD5;&#x4EE3;&#x7801;====================
        void Test(const char* string)
        {
            int result = StrToInt(string);
            if(result == 0 && g_nStatus == kInvalid)
                printf("the input %s is invalid.\n", string);
            else
                printf("number for %s is: %d.\n", string, result);
        }

        int main(int argc, char* argv[])
        {
            Test(nullptr);

            Test("");

            Test("123");

            Test("+123");

            Test("-123");

            Test("1a33");

            Test("+0");

            Test("-0");

            //&#x6709;&#x6548;&#x7684;&#x6700;&#x5927;&#x6B63;&#x6574;&#x6570;, 0x7FFFFFFF
            Test("+2147483647");

            Test("-2147483647");

            Test("+2147483648");

            //&#x6709;&#x6548;&#x7684;&#x6700;&#x5C0F;&#x8D1F;&#x6574;&#x6570;, 0x80000000
            Test("-2147483648");

            Test("+2147483649");

            Test("-2147483649");

            Test("+");

            Test("-");

            return 0;
        }
</=></cstdio>

Original: https://www.cnblogs.com/agui125/p/12024893.html
Author: 风御之举
Title: 字符串

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/607477/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • Pytorch 中 tensor的维度拼接

    torch.stack() 和 torch.cat() 都可以按照指定的维度进行拼接,但是两者也有区别,torch.satck() 是 增加新的维度进行堆叠,即其维度拼接后会增加一…

    Linux 2023年6月7日
    099
  • Android进阶技术之——一文吃透Android的消息机制

    前言 为什么要老药换新汤 作为Android中 至关重要 的机制之一,十多年来,分析它的文章不断,大量的内容已经被挖掘过了。所以: 已经对这一机制熟稔于心的读者,在这篇文章中,看不…

    Linux 2023年6月13日
    097
  • 个人学习记录-Cpp基础-成员初始化列表

    https://blog.csdn.net/XIONGXING_xx/article/details/115553291https://blog.csdn.net/W_Y2010/…

    Linux 2023年6月6日
    084
  • Docker学习笔记

    镜像下载、域名解析、时间同步请点击阿里云开源镜像站 Docker概述 Docker学习链接 官网链接:Home – Docker Docker与虚拟机比较 虚拟化技术 …

    Linux 2023年5月27日
    089
  • Markdown 常用语法精讲

    标题 (# 跟标题名称一定要留空格) 一级标题 二级标题 三级标题 四级标题 五级标题 六级标题 缩进 (使用) 这是缩进四个空格文本 (源码: 这是缩进四个空格文本) 强调/加粗…

    Linux 2023年6月7日
    0123
  • 一篇文章学会shell脚本

    一、Shell传递参数 运行: 二、Shell数组 运行: 三、Shell运算符 1、算术运算符 注意:条件表达式要放在方括号之间,并且要有空格,例如: [$a==$b] 是错误的…

    Linux 2023年5月28日
    086
  • Identity Server 4客户端认证控制访问API(一)

    一、说明 我们将定义一个api和要访问它的客户端,客户端将在identityser上请求访问令牌,并使用访问令牌调用api 二、项目结构与准备 1、创建项目QuickStartId…

    Linux 2023年6月13日
    093
  • 冒泡排序

    404. 抱歉,您访问的资源不存在。 可能是网址有误,或者对应的内容被删除,或者处于私有状态。 代码改变世界,联系邮箱 contact@cnblogs.com 园子的商业化努力-困…

    Linux 2023年6月8日
    0102
  • Linux快速安装流量监控工具(实用版)

    前言: Linux流量监控工具,在此我推荐两种分别为: 1、nload(推荐)因为个人看着舒服点😂 2、iftop 你可以选择上面两种中的任何一种。下面是这两个版本的简介和安装教程…

    Linux 2023年5月27日
    087
  • 如何使用yum来下载RPM包而不进行安装

    yum是基于Red Hat的系统(如CentOS、Fedora、RHEl)上的默认包管理器。使用yum,你可以安装或者更新一个RPM包,并且他会自动解决包依赖关系。但是如果你只想将…

    Linux 2023年6月6日
    0157
  • Flink Table Api & SQL 初体验,Blink的使用

    概述 Flink具有Table API和SQL-用于统一流和批处理。 Table API是用于Scala和Java的语言集成查询API,它允许以非常直观的方式组合来自关系运算符(例…

    Linux 2023年6月7日
    0113
  • Unicode、UTF-8、UTF-16 终于懂了

    计算机起源于美国,上个世纪,他们对英语字符与二进制位之间的关系做了统一规定,并制定了一套字符编码规则,这套编码规则被称为ASCII编码 ASCII 编码一共定义了128个字符的编码…

    Linux 2023年6月13日
    0103
  • 【socket】在Linux下socket温度上报–客户端

    socket通信客户端 socket函数 * 代码实现 socket函数 int socket(int domain,int type,int protocol); 参数: dom…

    Linux 2023年6月13日
    0106
  • Linux 目录挂载服务

    Linux 服务器挂载文件目录通常有三种形式,手动挂载、自动挂载、Autofs 自动挂载,下面对这三个挂载做一下介绍,接受一下这三个区别以及使用场景: 准备服务器和客户端: ser…

    Linux 2023年6月6日
    092
  • Redis 配置文件

    http://blog.csdn.net/tonysz126/article/details/8280696/ 2.1 Redis配置文件 为了对Redis的系统实现有一个直接的认…

    Linux 2023年5月28日
    0103
  • PYTORCH: 60分钟 | 训练一个分类器

    你已经知道怎样定义神经网络,计算损失和更新网络权重。现在你可能会想, 那么,数据呢? 通常,当你需要解决有关图像、文本或音频数据的问题,你可以使用python标准库加载数据并转换为…

    Linux 2023年6月16日
    0181
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球