每日5题Day2 - LeetCode 6 - 10

每一步向前都是向自己的梦想更近一步,坚持不懈,勇往直前!

第一题:6. Z 字形变换 - 力扣(LeetCode)

class Solution {
    public String convert(String s, int numRows) {
        //特殊情况的判定
        if(numRows < 2){
            return s;
        }
        //我们专门为每一行放一个list
        List<StringBuilder> rows = new ArrayList<>();
        for(int i = 0; i < numRows; i++){
            //对于每一行我们给一个sb
            rows.add(new StringBuilder());
        }
        //下面是模拟过程,不断的上下,使得行数在模拟字符串的顺序遍历顺序上下跳动
        int i = 0, flag = -1;
        for(char ch : s.toCharArray()){
            //每次我们都把当前取出来的字母放进对应行的sb中
            rows.get(i).append(ch);
            //注意两个方向的转变
            if(i == 0 || i == numRows - 1){
                flag = -flag;
            }
            i += flag;
        }
        //最后的结果sb,我们对于原来rows,我们分别把每一行的元素放进来,因为题目要求最后返回1从左到右 2从上到下的字母序列
        StringBuilder res = new StringBuilder();
        for(StringBuilder row : rows){
            res.append(row);
        }
        //注意以字符串形式来返回
        return res.toString();
    }
}

第二题:7. 整数反转 - 力扣(LeetCode)

class Solution {
    public int reverse(int x) {
        //转为字符串
        String str = String.valueOf(x);
        //判断第一个字符是否为正数
        int flag = str.charAt(0) == '-' ? -1 : 1;
        StringBuilder sb = new StringBuilder();
        //不同范围进行一个逆序
        if (flag == 1) {
            for (int i = str.length() - 1; i >= 0; i--) {
                sb.append(str.charAt(i));
            }
        } else {
            for (int i = str.length() - 1; i > 0; i--) {
                sb.append(str.charAt(i));
            }
        }
        //使用try catch来避免超范围
        try {
            return Integer.parseInt(sb.toString()) * flag;
        } catch (NumberFormatException e) {
            return 0;
        }
    }
}

第三题:8. 字符串转换整数 (atoi) - 力扣(LeetCode)

class Solution {
    public int myAtoi(String s) {
        if (s == null || s.length() == 0) {
            return 0;
        }
        int index = 0;
        // 跳过前导空格
        while (index < s.length() && s.charAt(index) == ' ') {
            index++;
        }
        // 判断符号
        int sign = 1;
        if (index < s.length() && (s.charAt(index) == '+' || s.charAt(index) == '-')) {
            sign = s.charAt(index) == '-' ? -1 : 1;
            index++;
        }
        // 读取数字部分
        long result = 0;
        while (index < s.length() && Character.isDigit(s.charAt(index))) {
            //数字的形式 把原来的*10,现在的转为数字加上去
            result = result * 10 + (s.charAt(index) - '0');
            if (result > Integer.MAX_VALUE) {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }
            index++;
        }
        return (int) (sign * result);
    }
}

第四题:9. 回文数 - 力扣(LeetCode)

class Solution {
    public boolean isPalindrome(int x) {
        //转为字符串
        String str = String.valueOf(x);
        int l = 0, r = str.length() - 1;
        //从两边遍历过,但凡有不相等的直接返回false
        while(l < r){
            if(str.charAt(l) != str.charAt(r)){
                return false;
            }
            l++;
            r--;
        }
        //遍历结束,没出问题返回true
        return true;
    }
}

 第五题:10. 正则表达式匹配 - 力扣(LeetCode)

class Solution {
    public boolean isMatch(String s, String p) {
        // 初始化字符串s和p的长度,加1是因为dp数组的索引从0开始
        int m = s.length() + 1, n = p.length() + 1;
        
        // 创建一个二维布尔数组dp,用于存储匹配状态
        boolean[][] dp = new boolean[m][n];
        
        // 初始化dp数组的第一行第一个元素为true,表示空字符串与空模式匹配
        dp[0][0] = true;
        
        // 初始化dp数组中模式p中包含星号(*)的情况
        // 只有当模式p中的字符是星号(*)时,我们才考虑空字符串s与模式p的匹配
        for(int j = 2; j < n; j += 2)
            dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
        
        // 遍历字符串s和模式p
        for(int i = 1; i < m; i++) {
            for(int j = 1; j < n; j++) {
                // 当模式p中的当前字符是星号(*)时
                // 有两种情况:星号(*)表示前面的元素可以出现0次或多次
                // 1. 星号(*)前面的字符没有在字符串s中匹配(即dp[i][j - 2])
                // 2. 星号(*)前面的字符在字符串s中匹配了一次(即dp[i - 1][j]),并且当前字符s.charAt(i - 1)与模式p中星号(*)前面的字符匹配,或者星号(*)前面的字符是点(.)
                dp[i][j] = p.charAt(j - 1) == '*' ?
                    dp[i][j - 2] || dp[i - 1][j] && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') :
                    // 当模式p中的当前字符不是星号(*)时
                    // 需要模式p中当前字符与字符串s中的当前字符匹配,或者模式p中的当前字符是点(.)(表示匹配任何单个字符)
                    // 同时,还需要之前的状态dp[i - 1][j - 1]为true(即前面的子串和模式匹配)
                    dp[i - 1][j - 1] && (p.charAt(j - 1) == '.' || s.charAt(i - 1) == p.charAt(j - 1));
            }
        }
        
        // 返回dp数组的最后一个元素,即字符串s和模式p的匹配结果
        return dp[m - 1][n - 1];
    }
}

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面是电话号码的字母组合问的 C++ 代码实现: ``` class Solution { public: vector<string> letterCombinations(string digits) { unordered_map<char, string> mapping = { {'2', "abc"}, {'3', "def"}, {'4', "ghi"}, {'5', "jkl"}, {'6', "mno"}, {'7', "pqrs"}, {'8', "tuv"}, {'9', "wxyz"} }; vector<string> res; if (digits.empty()) { return res; } string combination; backtrack(res, combination, digits, 0, mapping); return res; } void backtrack(vector<string>& res, string& combination, string& digits, int index, unordered_map<char, string>& mapping) { if (index == digits.size()) { res.push_back(combination); return; } for (char c : mapping[digits[index]]) { combination.push_back(c); backtrack(res, combination, digits, index + 1, mapping); combination.pop_back(); } } }; ``` 其中 `letterCombinations` 函数用来生成所有的字母组合,`backtrack` 函数用来进行回溯操作。在 `letterCombinations` 函数中,首先根据数字字符和字母的映射关系创建了一个 `unordered_map` 对象 `mapping`。然后定义了一个空字符串 `combination` 和一个空 vector `res` 来保存最终结果。最后调用了 `backtrack` 函数来生成所有的字母组合。在 `backtrack` 函数中,首先判断是否达到了数字字符串的末尾,如果是,则将当前的 `combination` 字符串保存到 `res` 中。否则,遍历当前数字字符所能表示的所有字母,依次加入到 `combination` 字符串中,然后递归调用 `backtrack` 函数,添加下一个数字字符所能表示的字母。递归完成后,需要将 `combination` 字符串还原到上一个状态,以便进行下一次回溯。最终返回 `res` 数组即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

别看了真C不了一点

打赏者皆为义父

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值