zoukankan      html  css  js  c++  java
  • Java实现 LeetCode 383 赎金信

    383. 赎金信

    给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串,判断第一个字符串ransom能不能由第二个字符串magazines里面的字符构成。如果可以构成,返回 true ;否则返回 false。

    (题目说明:为了不暴露赎金信字迹,要从杂志上搜索各个需要的字母,组成单词来表达意思。)

    注意:

    你可以假设两个字符串均只含有小写字母。

    canConstruct(“a”, “b”) -> false
    canConstruct(“aa”, “ab”) -> false
    canConstruct(“aa”, “aab”) -> true

    PS:

    String.indexof(a,b)
    从b索引开始找a字符

    class Solution {
       public boolean canConstruct(String ransomNote, String magazine) {
            if (ransomNote.length() > magazine.length()) {
                return false;
            }
            int[] chars = new int[26];
            for (int i = 0; i < ransomNote.length(); i++) {
                char c = ransomNote.charAt(i);
                int index = magazine.indexOf(c, chars[c - 'a']);
                if (index == -1) {
                    return false;
                }
                chars[c - 'a'] = index + 1;
            }
            return true;
        }
    }
    
  • 相关阅读:
    LeetCode 72. Edit Distance
    LeetCode 71. Simplify Path
    LeetCode 70. Climbing Stairs
    LeetCode 69. Sqrt(x)
    Ubuntu系统测评
    itchat 爬了爬自己的微信通讯录
    logistic回归模型
    多元线性回归模型
    可乐鸡翅制作难点
    梯度下降算法&线性回归算法
  • 原文地址:https://www.cnblogs.com/a1439775520/p/13075778.html
Copyright © 2011-2022 走看看