36. Valid Sudoku\434. Number of Segments in a String\344. Reverse String\345. Reverse Vowels of a St

本文解析了LeetCode上的四道字符串相关题目,包括有效数独验证、字符串段计数、字符串反转及元音字母反转等。每道题都提供了详细的题目描述与清晰的代码实现,帮助读者理解算法思想。

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

36. Valid Sudoku

题目描述

Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.

The Sudoku board could be partially filled, where empty cells are filled with the character ‘.’.

代码实现

class Solution {
public:
    bool isValidSudoku(vector<vector<char>>& board) {
        vector<map<char, int> > stro(27);
        const unsigned int num = 9; 
        for(int row = 0; row < num; row++) {
            for(int col = 0; col < num; col++) {
                char tmp = board[row][col];
                if(tmp != '.') {
                    if(stro[row].count(tmp)) return false; 
                    else stro[row][tmp] = 1;
                    if(stro[num + col].count(tmp)) return false; 
                    else stro[num + col][tmp] = 1;
                    if(stro[(num << 1) + (row/3)*3 + (col/3)].count(tmp)) return false; 
                    else stro[(num << 1) + (row/3)*3 + (col/3)][tmp] = 1;
                }
            }
        }
        return true;
    }
};

434. Number of Segments in a String

题目描述

Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters.

Please note that the string does not contain any non-printable characters.

Example:

Input: "Hello, my name is John"
Output: 5

代码实现

使用了stringstream实现。

class Solution {
public:
    int countSegments(string s) {
       stringstream ss(s); string tmp; int cnt = 0;
       while(ss >> tmp) cnt++; return cnt;
    }
};

344. Reverse String

题目描述

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = “hello”, return “olleh”.

代码实现

class Solution {
public:
    string reverseString(string s) {
        int lower = 0, higher = s.length() - 1;
        while(lower < higher) {
            char tmp = s[lower]; s[lower] = s[higher]; s[higher] = tmp;
            lower++, higher--;
        }
        return s;
    }
};

345. Reverse Vowels of a String

题目描述

Write a function that takes a string as input and reverse only the vowels of a string.

Example 1:
Given s = "hello", return "holle".

Example 2:
Given s = "leetcode", return "leotcede".

Note:
The vowels does not include the letter "y".

代码实现

class Solution {
public:
    bool isVowel(char tmp) {
        return (tmp == 'a' || tmp == 'e' || tmp == 'i' || tmp == 'o' || tmp == 'u' || tmp == 'A' || tmp == 'E' || tmp == 'I' || tmp == 'O' || tmp == 'U');
    }

    string reverseVowels(string s) {
        int lower = 0, higher = s.length() - 1;
        while(lower < higher) {
            while(!isVowel(s[lower]) && lower < higher) lower++;
            while(!isVowel(s[higher]) && lower < higher) higher--;
            if(lower >= higher) return s;
            else {
               char tmp = s[lower];
               s[lower] = s[higher];
               s[higher] = tmp;
            }
            lower++;
            higher--;
        }
        return s;
    }
};

字符串处理:http://blog.youkuaiyun.com/weixin_35929051/article/details/52502486

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值