简体   繁体   English

回文算法的时空复杂度

[英]Time and Space Complexity of Palindrome Algorithm

What is the time and space complexity of the following code for finding a palindrome in Java:以下代码在 Java 中寻找回文的时间和空间复杂度是多少:

    public static boolean isPalindrome(String str) {
        return str.equals(new StringBuilder(str).reverse().toString());
    }

I know reverse() is O(n).我知道 reverse() 是 O(n)。 toString() is also O(n). toString() 也是 O(n)。 equals is also O(n).等于也是 O(n)。 Does this mean that this code is O(n)?这是否意味着这段代码是 O(n)?

As for space complexity, a StringBuilder needs to be created.至于空间复杂度,需要创建一个StringBuilder。 I'm not sure what happens to it after it gets converted into a string.我不确定它被转换成字符串后会发生什么。 Does Java allocate space for the StringBuilder that was converted into a string, then forget about the StringBuilder (allowing it to be picked up by the garbage collector)? Java 是否为转换为字符串的 StringBuilder 分配空间,然后忘记 StringBuilder(允许它被垃圾收集器拾取)?

Thanks!谢谢!

---------- EDIT ------------- - - - - - 编辑 - - - - - - -

I want to know more about what is created on the stack after isPalindrome is called.我想知道更多关于调用 isPalindrome 后在堆栈上创建的内容。 How efficient is it space-wise compared to, say,与说相比,它在空间方面的效率如何

Thanks.谢谢。 I still don't really understand how efficient space-wise this code is though?我仍然不太了解这段代码在空间方面的效率如何? What exactly will be created on the stack, And how efficient is it (in terms of space) compared to, say,究竟将在堆栈上创建什么,以及它的效率(在空间方面)与,比方说,

public boolean palindrome2(String str) {    
        int n = str.length();
        for( int i = 0; i < n/2; i++ )
            if (str.charAt(i) != str.charAt(n-i-1)) return false;
        return true;    
}

Assuming all these operations are really O(n), I'd say you are right: O(n) + O(n) + O(n) = 3 * O(n) (or O(3n), if you will), which then falls to O(n) category. 假设所有这些运算实际上都是O(n),我想您是对的:O(n)+ O(n)+ O(n)= 3 * O(n)(或O(3n),如果您愿意),则属于O(n)类别。

What comes to the StringBuilder - you create an anonymous instance and after the method isPalindrome ends, there are no references to the object as its scope is local only for this method. StringBuilder -创建一个匿名实例,在方法isPalindrome结束后,由于该对象的作用域仅是此方法的本地对象,因此没有对该对象的引用。 So yes, it will eventually be processed by garbage collector. 所以是的,它最终将由垃圾收集器处理。 Most likely not immediately, but I think that is none of your concern anyway. 最有可能不是立即,但我认为这与您无关。

public static boolean isPalindrome(String argument){    
    //remove special character    
    String done = argument.trim().replaceAll("[^a-zA-Z]+", " ");   
    //remove white spaces   
    String result = done.replaceAll("\\s", "");   
    //assign result string to argument   
    argument = result;   
    //convert string to a char then into a List of Charaters
    ArrayList<Character> characterList = (ArrayList<Character>) result.chars().mapToObj(c -> (char)c).collect(Collectors.toList());     
    //Using Collections reverse character List   
    Collections.reverse(characterList);
    //convert reversed List int a String    
    String reversedStr  = characterList.stream().map(String::valueOf).collect(Collectors.joining());   
    //Compare argument with reversedStr then return true or false   
    if(argument.equalsIgnoreCase(reversedStr))   
        return true;       
 public static boolean isPalindrome(String argument){
        //remove special character
        String done = argument.trim().replaceAll("[^a-zA-Z]+", " ");
        //remove white spaces
        String result = done.replaceAll("\\s", "");
        //assign result string to argument
        argument = result;
        //convert string to a char then into a List of Charaters
        ArrayList<Character> characterList = (ArrayList<Character>) result.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
        //Using Collections reverse character List
        Collections.reverse(characterList);
        //convert reversed List int a String 
        String reversedStr  = characterList.stream().map(String::valueOf).collect(Collectors.joining());
        //Compare argument with reversedStr then return true or false
        if(argument.equalsIgnoreCase(reversedStr))
            return true;
        
        return false;
    }
    return false;   
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 回文递归算法的时间复杂度 - Time Complexity of the Palindrome recursive algorithm 这个算法的时间和空间复杂度是多少? - What is the time and space complexity of this algorithm? 这种置换算法的时间和空间复杂度 - the time and space complexity of this permutation algorithm ||的递归算法的时间复杂度和空间复杂度是多少? 操作员? - What is the time complexity and space complexity of a recursive algorithm with the || operator? 查找Anagrams的算法的时间复杂度和空间复杂度是多少? - What is the time complexity and space complexity of this algorithm to find Anagrams? Java 算法:分离奇偶数(时空复杂度) - Java Algorithm: Segregate Odd Even Numbers (time-space complexity) 如何确定旋转字符串算法的时空复杂度 - How to determine the Time and Space complexity of rotate string algorithm 第n个具有有效时间复杂度的二元回文 - nth Binary palindrome with efficient time complexity 在时间复杂度 O(n) 和空间复杂度 O(1) 中查找数组中重复元素的最有效算法是什么? - What is the most efficient algorithm to find repeated elements in an array in time complexity O(n) and space complexity O(1)? 如何为下面的新快速排序算法找到确切的时间复杂度和空间复杂度 - How to find exact Time complexity and Space Complexity for below new Quick sort algorithm
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM