简体   繁体   English

似乎无法让它工作。 无法如我所愿地取出中位数

[英]Can't seem to make it work. Can't get the median out as I want

I can't get the median out.我拿不出中位数。 I want the median from the words.我想要单词的中位数。 Having a hard time getting the value out of the for loop.很难从for循环中获取价值。

public class MedianWord {
    static double medianWordLength(String words) {
        String[] parts = words.split(" ");
        int[] a;
        double median = 0;

        for (int i = 0; i < parts.length; i++) {
            a = new int[parts[i].length()];
            Arrays.sort(a);
            if (a.length % 2 == 0) {
                median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
            }
            else
                median = (double) a[a.length / 2];
        }
        return median;
    }
}

Each iteration of the loop overwrites the a array with a new array.循环的每次迭代都会用新数组覆盖a数组。 You need to break the problem into two parts - first, iterate over the parts array and convert it to an array a of lengths, and then find the median of that array:您需要将问题分成两部分 - 首先,遍历parts数组并将其转换为长度为a的数组,然后找到该数组的中位数:

static double medianWordLength(String words) {
    String[] parts = words.split(" ");
    int[] a = new int[parts.length];
    for (int i = 0; i < parts.length; i++) {
        a[i] = parts[i].length();
    }

    Arrays.sort(a);
    if (a.length % 2 == 0) {
        return ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
    }
    else {
        return a[a.length / 2];
    }
}

Side note:边注:
Converting the words to a sorted array of lengths could be done (arguably) more elegantly with streams:使用流可以(可以说)更优雅地将单词转换为长度排序的数组:

int[] a = Arrays.stream(words.split(" ")).mapToInt(String::length).sorted().toArray();

That should work fine:那应该可以正常工作:

static Integer getMedian(String sentence) {
        String[] str = sentence.split(" ");
        Integer[] strLen = new Integer[str.length];

        for (int i = 0; i < strLen.length; i++) {
            strLen[i] = str[i].length();
        }

        return strLen.length % 2 == 0?
                (strLen[strLen.length / 2] + strLen[strLen.length / 2 - 1]) / 2 : strLen[strLen.length / 2];
    }
public static double medianWordLength(String str) {
    int[] arr = Arrays.stream(str.split("\\s+"))
                      .mapToInt(String::length)
                      .sorted()
                      .toArray();

    int mid = arr.length / 2;
    double median = (double)arr[mid];

    return arr.length % 2 == 0 ? (median + arr[mid - 1]) / 2 : median;
}

First you keep sorting the array over and over again and then replacing the array within the loop.首先,您一遍又一遍地对数组进行排序,然后在循环中替换数组。 That is your main problem.那是你的主要问题。

But to facilitate the process, you just need one array and no for loop.但是为了方便这个过程,你只需要一个数组而不需要 for 循环。 Just sort the word array based on length of the words.只需根据单词的长度对单词数组进行排序。 I left some print statements in so you could see what's going on.我留下了一些打印语句,所以你可以看到发生了什么。 And if you split on one or more spaces you don't need to be as careful when you enter the string of words.如果您在一个或多个空格上拆分,则在输入字符串时不需要那么小心。

String quote = "To be or not to be that is the question";
System.out.println(medianWordLength(quote));
    
static double medianWordLength(String words) {
    String[] parts = words.split("\\s+");
    double median = 0;

    // sort the array based on length of the words
    Arrays.sort(parts,
            (a, b) -> Integer.compare(a.length(), b.length()));

    for (String v : parts) {
        System.out.println(v);
    }
    System.out.println("------------------");

    // get the "middle" index.
    int idx = parts.length / 2;

    // adjust index based on array size
    if (parts.length % 2 == 0) {
        System.out.println(parts[idx-1] + " " + parts[idx]);
        median = (parts[idx-1].length() + parts[idx].length())
                / 2.;
    } else {
        System.out.println(parts[idx]);
        median = parts[idx + 1].length();
    }
    return median;
}

Prints印刷

To
be
or
to
be
is
not
the
that
question
----------
be is
2.0

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM