颠倒单词的顺序——时间复杂度?

发布于 2024-11-05 12:11:16 字数 1152 浏览 1 评论 0原文

输入:“我的名字是普里塔姆” 输出:“Pritam is Name My”

到目前为止我已经写了这个,但是我对时间复杂度有点困惑

public string ReverseWordsInAString(string str)
    {
        char[] temp = str.ToCharArray();
        int startIndex = 0;
        int endIndex = str.Length - 1;
        temp = ReverseString(temp, startIndex, endIndex);
        endIndex = 0;
        foreach (char c in temp)
        {
            if(c == ' ')
            {
                temp = ReverseString(temp, startIndex, endIndex-1);
                startIndex = endIndex + 1;
            }
            if (endIndex == str.Length-1)
            {
                temp = ReverseString(temp, startIndex, endIndex);
            }
            endIndex++;
        }
        str = new string(temp);
        return str;
    }

    public char[] ReverseString(char[] chr, int start, int end)
    {
        while (start < end)
        {
            char temp = chr[start];
            chr[start] = chr[end];
            chr[end] = temp;
            start++;
            end--;
        }
        return chr;
    }

当我从 for 循环调用 ReverseString 方法时,我认为它不再是 O(n) 解决方案。如果我错了,请纠正我。大家有没有更好的解决办法。

Input: "My Name is Pritam"
Output: "Pritam is Name My"

I have written this so far, but I'm bit confused with time complexity

public string ReverseWordsInAString(string str)
    {
        char[] temp = str.ToCharArray();
        int startIndex = 0;
        int endIndex = str.Length - 1;
        temp = ReverseString(temp, startIndex, endIndex);
        endIndex = 0;
        foreach (char c in temp)
        {
            if(c == ' ')
            {
                temp = ReverseString(temp, startIndex, endIndex-1);
                startIndex = endIndex + 1;
            }
            if (endIndex == str.Length-1)
            {
                temp = ReverseString(temp, startIndex, endIndex);
            }
            endIndex++;
        }
        str = new string(temp);
        return str;
    }

    public char[] ReverseString(char[] chr, int start, int end)
    {
        while (start < end)
        {
            char temp = chr[start];
            chr[start] = chr[end];
            chr[end] = temp;
            start++;
            end--;
        }
        return chr;
    }

When I call ReverseString method from a for loop I think it no more a O(n) solution. Please correct me if I'm wrong. Does anyone have any better solution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

柏拉图鍀咏恒 2024-11-12 12:11:16

爪哇语

String str= "My Name is Pritam";
String arr[] = str.split(" ");
for(int i = arr.length-1 ; i >=0 ; i--){
   System.out.println(arr[i]);
}

in Java

String str= "My Name is Pritam";
String arr[] = str.split(" ");
for(int i = arr.length-1 ; i >=0 ; i--){
   System.out.println(arr[i]);
}
折戟 2024-11-12 12:11:16

您的代码是O(n)。您可以通过查看每个元素涉及的交换次数来看到这一点,即 2(一次用于整个字符串的初始反转,第二次用于逐字反转)。此外,foreach 循环对每个元素精确地迭代一次。

Your code is O(n). You can see this by looking at the number of swaps each element is involved in, which is 2 (once for the initial reverse of the entire string, second for the word-wise reversal). In addition the foreach loop iterates over each element exactly once.

雪化雨蝶 2024-11-12 12:11:16

在红宝石中:

sentence = "My name is Pritam"
print sentence.split(" ").reverse.join(" ")

In Ruby:

sentence = "My name is Pritam"
print sentence.split(" ").reverse.join(" ")
零崎曲识 2024-11-12 12:11:16

在C中;

char *s = "My Name is Pritam", *t = s + strlen(s), *end = strchr(s,' ')-1;
while( t != end )
{
  *(t = strrchr(t,' ')) = '\0';
  printf( "%s ", --t+2 );
}
printf( "%s", s );

In C;

char *s = "My Name is Pritam", *t = s + strlen(s), *end = strchr(s,' ')-1;
while( t != end )
{
  *(t = strrchr(t,' ')) = '\0';
  printf( "%s ", --t+2 );
}
printf( "%s", s );
空宴 2024-11-12 12:11:16

可能重复,我不久前曾问过类似的问题。但我收到的回复非常有趣,实际上它确实改变了对复杂性的思考方式;).... 时间复杂度

Possible duplicate, I had asked a similar question some time back. But the responses I received were very interesting, actually it did change the way complexity should be thought about ;).... Time Complexity

白衬杉格子梦 2024-11-12 12:11:16

分割字符串并将其压入堆栈。从堆栈中弹出元素并将其添加到新字符串中。需要额外的空间,但只是发布,因为它可能是实现字符串反转的简单方法

public class StringReverse {

public static void main (String args[]){

    String input = "My name is Pritam";

    Stack<String> stack  = new Stack<String>();

    String[] strings= input.split(" ");

    for(String str :strings){
        stack.push(str);
    }

    String reverse = "" ;

    while(!stack.isEmpty()){
        reverse = reverse + " " + stack.pop();
    }

    System.out.println(reverse);
}

}

Split the string and push it to a stack . Pop elements from stack and add it to a new string. Requires extra space,but just posted because it could be a easy way to implement string reverse

public class StringReverse {

public static void main (String args[]){

    String input = "My name is Pritam";

    Stack<String> stack  = new Stack<String>();

    String[] strings= input.split(" ");

    for(String str :strings){
        stack.push(str);
    }

    String reverse = "" ;

    while(!stack.isEmpty()){
        reverse = reverse + " " + stack.pop();
    }

    System.out.println(reverse);
}

}

音盲 2024-11-12 12:11:16

在Python中,

sentence = "My name is Pritam"
' '.join(sentence.split(" ")[::-1])

in Python,

sentence = "My name is Pritam"
' '.join(sentence.split(" ")[::-1])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文