zoukankan      html  css  js  c++  java
  • Lc面试题0109字符串轮转

    package com.leetcode.leetcode.licm;
    
    /**
     * @description: 面试题 01.09. 字符串轮转
     * 字符串轮转。给定两个字符串s1和s2,请编写代码检查s2是否为s1旋转而成(比如,waterbottle是erbottlewat旋转后的字符串)。
     * <p>
     * 示例1:
     * <p>
     * 输入:s1 = "waterbottle", s2 = "erbottlewat"
     * 输出:True
     * 示例2:
     * <p>
     * 输入:s1 = "aa", s2 = "aba"
     * 输出:False
     * 提示:
     * <p>
     * 字符串长度在[0, 100000]范围内。
     * 说明:
     * <p>
     * 你能只调用一次检查子串的方法吗?
     * @author: licm
     * @create: 2021-07-16 09:24
     **/
    public class Lc面试题0109字符串轮转 {
    
    //    /**
    //     * 方法1:哈希表 超时
    //     *
    //     * @param s1
    //     * @param s2
    //     * @return
    //     */
    //    public static boolean isFlipedString(String s1, String s2) {
    //        if (s1.length() != s2.length()) {
    //            return false;
    //        }
    //
    //        int[] res = new int[26];
    //        for (int i = 0; i < s1.length(); i++) {
    //            res[s1.charAt(i) - 'a']++;
    //        }
    //        for (int i = 0; i < s2.length(); i++) {
    //            res[s2.charAt(i) - 'a']--;
    //        }
    //        for (int i = 0; i < res.length; i++) {
    //            if (res[i] < 0) {
    //                return false;
    //            }
    //        }
    //
    //        return true;
    //
    //    }
    
        /**
         * 方法2 String的 indexof
         * <p>
         * 因为s2是是旋转的字符串,所以s2+s2必然会有一个完整的s1存在,在用indexof匹配是否出现就可以了
         *
         * @param s1
         * @param s2
         * @return
         */
        public static boolean isFlipedString(String s1, String s2) {
            return s1.length() == s2.length() && (s2 + s2).indexOf(s1) > -1;
        }
    
        public static void main(String[] args) {
            String s1 = "waterbottle";
            String s2 = "erbottlewat";
            System.out.println(isFlipedString(s1, s2));
    
        }
    }
    
    
    不会,我可以学;落后,我可以追赶;跌倒,我可以站起来!
  • 相关阅读:
    python用于web题里写解密脚本
    改变checkbox和radio的默认样式
    div内元素垂直居中
    icheck.js插件
    glyphicons字形图标
    没有内容的span元素下掉问题
    临界区保护
    信号量的使用&生产者消费者问题
    空闲线程和钩子函数
    线程的时间片轮询调度
  • 原文地址:https://www.cnblogs.com/xiaoshahai/p/15019229.html
Copyright © 2011-2022 走看看