精華區beta Marginalman 關於我們 聯絡資訊
2559. Count Vowel Strings in Ranges Prefix Sum,用一個陣列儲存每個index符合條件的words數量 再將queries查詢條件裡對應的index相減 private readonly char[] vowels = new char[]{'a','e','i','o','u'}; public int[] VowelStrings(string[] words, int[][] queries) { var checkList = new int[words.Length+1]; var i = 1; foreach (var word in words) { if (vowels.Contains(word[0]) && vowels.Contains(word[word.Length-1])) { checkList[i] = checkList[i-1] + 1; } else { checkList[i] = checkList[i-1]; } i++; } var j = 0; var result = new int[queries.Length]; foreach (var query in queries) { result[j] = checkList[query[1]+1] - checkList[query[0]]; j++; } return result; } -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 111.249.70.234 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1735839612.A.834.html