簡體   English   中英

我的javascript代碼返回最常見的字母有什么問題?

[英]Whats wrong with my javascript code to return most common letter?

我正在嘗試編寫一個解決方案,它將返回字符串中最常見的字母,以及該字母出現的次數。 我提出的解決方案是循環遍歷字符串,一次在一個名為letter的變量中保持一個字母,並將所有其他字母與此進行比較。 然后一旦字符串結束,循環將保持第二個字母並重復。

我得到的問題是它只是返回字符串中的第一個字母。 它得到的匹配數正確,但它忽略了其他字母有更多匹配的事實。

我究竟做錯了什么?

 var match = 0; var matchCount = 0; var letter = 0; var count = 0; indx1 = 0; indx2 = 0; function problemTen(a) { while (indx1 < a.length) { letter = a[indx1]; //hold the first letter in the string in the letter variable while (indx2 < a.length) { //now rotate through every other letter to compare if (a[indx2] == letter) { //compare count += 1; // if there is a match, add one to the count variable to hold the number of matches } indx2 += 1; //cycle through the rest of the string } if (matchCount === 0 || count > matchCount) { // if it's the first time around, or if this letter had more matches than the previous letter match = letter; // hold the letter in the match variable matchCount = count; //hold the number of matches in the count variable } indx1 += 1; //cycle through the first variable that you compare } return [match, matchCount]; //return results } console.log(problemTen("astrings")); 

編輯:這是我提出的解決方案。

function problemTen (a) {
  var match = 0;
  var matchCount = 0;
  var letter;

  for(var indx1 = 0; indx1<a.length; indx1++) {
    letter = a[indx1];              
    var count = 1;                  

    for(var indx2 = indx1 + 1; indx2<a.length; indx2++) {         
      if(a[indx2] == letter) {        
        count +=1;                 
      }
    }
    if(matchCount === 0 || count>matchCount) { 
      match = letter;                  
      matchCount =count;               
    }   
  }
  return [match, matchCount];       
}

console.log(problemTen("iamatinystring"));

也許檢查這個解決方案,它的時間復雜度是線性的,與你想要實現的相比,這是四倍:

function problemTen(a) {
  var array = a.split('');
  var max = 0;
  var letter;

  var counter = array.reduce(function(memo, item) {
    memo[item] = memo[item] ? memo[item] + 1 : 1;
    if (memo[item] > max) {
      max = memo[item];
      letter = item;
    }
    return memo;
  }, {});

  return [letter, max];
}

console.log(problemTen("astrings"));

如果你想堅持你的解決方案,問題是,當你再次運行外部while循環時,你沒有重置indx2和count變量,所以,indx2 = a.length已經,而內部while循環沒有再跑去計算字母。 添加這些重置,您的代碼應該是這樣的:

function problemTen(a) {
  var match = 0;
  var matchCount = 0
  var letter = 0;
  var count = 0;
  indx1 = 0;
  indx2 = 0;

  while (indx1 < a.length) {
    letter = a[indx1]; //hold the first letter in the string in the letter variable

    while (indx2 < a.length) { //now rotate through every other letter to compare
      if (a[indx2] === letter) { //compare 
        count += 1; // if there is a match, add one to the count variable to hold the number of matches
      }
      indx2 += 1; //cycle through the rest of the string  
    }
    if (matchCount === 0 || count > matchCount) { // if it’s the first time around,     or if this letter had more matches than the previous letter
      match = letter; // hold the letter in the match variable
      matchCount = count; //hold the number of matches in the count     variable
    }
    indx1 += 1; //cycle through the first variable that you compare

    // HERE WE RESET indx2 and match to 0
    indx2 = 0;
    match = 0;

  }
  return [match, matchCount]; //return results
}

console.log(problemTen("astrings"));

事實上,要實現O(n)時間復雜度,你不一定使用reduce,你也可以只使用一個循環,它肯定會快得多,我只是喜歡函數式編程,因為它帶來更好的可讀性:

function problemTen(a) {
  var max = 0;
  var letter;
  var counter = {};

  for (var i = 0, l = a.length; i<l; i++) {
    counter[a[i]] = counter[a[i]] ? counter[a[i]] + 1 : 1;
    if (counter[a[i]] > max) {
      max = counter[a[i]];
      letter = a[i];
    }
  }

  return [letter, max];
}

console.log(problemTen("astrings"));

希望這可以幫助。

您的任務是找到給定字符串中出現次數最多的(第一個)字符。 如果你不想像Ahmet Cetin的回答那樣使用額外的內存,你真的需要兩個循環。 你還需要當前字符及其出現次數的點,以及一個用於actaul頂部位置及其出現次數的四個變量。

這是一個for循環比你的while循環更適合的地方,但兩者都可以。

var teststring = "teststring";

function problemTen(s){
  var count = 0;      // counter for current character
  var last = 0;       // counter for actual highest character
  var character = ""; // current character
  var top = "";       // highest character

  for(var i = 0;i < s.length;i++){     // Loop over the full string
    character = s[i];                  // set current character
    for(var j = 0;j < s.length; j++){  // Loop over the full string
      if(s[j] === character){          // we found another occurence
        count++;                       // increment the counter
      }
    }
    if(last < count){   // the actual char. has more occ. than the last one
      last = count;     // set the top counter to the current counter
      top = character;  // set the top character to the current character
    }
    count = 0;        // reset the current counter
  }
  return [top,last];    // return the character and count of the to one
}

problemTen(teststring);

原樣,代碼只循環一次字符串。 為此,您應該在內部while循環之前將indx2重置為0。 完成后,您還必須在每個內循環處重置'count'變量。

優化是讓indx2從indx1 + 1開始,並在內循環的開頭有count = 1。

 var match = 0; var matchCount = 0; var letter = 0; var count = 0; indx1 = 0; indx2 = 0; function problemTen(a) { while (indx1 < a.length) { letter = a[indx1]; //hold the first letter in the string in the letter variable indx2 = 0; // <---- [sal] reset the inner loop starting point count = 0; // <---- [sal] reset the inner loop counter while (indx2 < a.length) { //now rotate through every other letter to compare if (a[indx2] == letter) { //compare count += 1; // if there is a match, add one to the count variable to hold the number of matches } console.log(letter, a[indx2], count); indx2 += 1; //cycle through the rest of the string } if (matchCount === 0 || count > matchCount) { // if it's the first time around, or if this letter had more matches than the previous letter match = letter; // hold the letter in the match variable matchCount = count; //hold the number of matches in the count variable } indx1 += 1; //cycle through the first variable that you compare } return [match, matchCount]; //return results } console.log(problemTen("astrings")); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM