classSolution { funclengthOfLongestSubstring(_s: String) -> Int { var charIndex = [Character: Int]() var maxLength =0 var start =0 for (i, char) in s.enumerated() { iflet index = charIndex[char], index >= start { start = index +1 } else { maxLength =max(maxLength, i - start +1) } charIndex[char] = i } return maxLength } }
结果
执行用时 : 16 ms, 击败 62.76% 使用 Swift 的用户
内存消耗 : 15.63 MB, 击败 5.14% 使用 Swift 的用户
Kotlin
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
classSolution { funlengthOfLongestSubstring(s: String): Int { val charIndex = mutableMapOf<Char, Int>() var maxLength = 0 var start = 0 for (i in s.indices) { val char = s[i] if (charIndex.containsKey(char) && charIndex[char]!! >= start) { start = charIndex[char]!! + 1 } else { maxLength = maxLength.coerceAtLeast(i - start + 1) } charIndex[char] = i } return maxLength } }
结果
执行用时 : 208 ms, 击败 60.94% 使用 Kotlin 的用户
内存消耗 : 36.87 MB, 击败 39.48% 使用 Kotlin 的用户
Dart
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution{ int lengthOfLongestSubstring(String s) { Map<String, int> charIndex = {}; int maxLength = 0, start = 0; for (int i = 0; i < s.length; i++) { String char = s[i]; if (charIndex.containsKey(char) && charIndex[char]! >= start) { start = charIndex[char]! + 1; } else { maxLength = maxLength > i - start + 1 ? maxLength : i - start + 1; } charIndex[char] = i; } return maxLength; } }
结果
执行用时 : 352 ms, 击败 41.18% 使用 Dart 的用户
内存消耗 : 147.63 MB, 击败 97.06% 使用 Dart 的用户
Go
1 2 3 4 5 6 7 8 9 10 11 12 13
funclengthOfLongestSubstring(s string)int { charIndex := make(map[byte]int) maxLength, start := 0, 0 for i := 0; i < len(s); i++ { if index, exists := charIndex[s[i]]; exists && index >= start { start = index + 1 } else { maxLength = max(maxLength, i-start+1) } charIndex[s[i]] = i } return maxLength }
结果
执行用时 : 4 ms, 击败 88.47% 使用 Go 的用户
内存消耗 : 3.07 MB, 击败 23.56% 使用 Go 的用户
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# @param {String} s # @return {Integer} deflength_of_longest_substring(s) char_index = {} max_length = start = 0 s.each_char.with_index do |char, i| if char_index.key?(char) && char_index[char] >= start start = char_index[char] + 1 else max_length = [max_length, i - start + 1].max end char_index[char] = i end max_length end
结果
执行用时 : 80 ms, 击败 100.00% 使用 Ruby 的用户
内存消耗 : 206.75 MB, 击败 15.79% 使用 Ruby 的用户
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
objectSolution{ deflengthOfLongestSubstring(s: String): Int = { var charIndex = Map[Char, Int]() var maxLength = 0 var start = 0 for (i <- s.indices) { val char = s(i) if (charIndex.contains(char) && charIndex(char) >= start) { start = charIndex(char) + 1 } else { maxLength = maxLength.max(i - start + 1) } charIndex += (char -> i) } maxLength } }
结果
执行用时 : 596 ms, 击败 38.89% 使用 Scala 的用户
内存消耗 : 58.82 MB, 击败 5.55% 使用 Scala 的用户
Rust
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
use std::collections::HashMap;
implSolution { pubfnlength_of_longest_substring(s: String) ->i32 { letmut char_index = HashMap::new(); // 用于存储字符的索引 letmut max_length = 0; letmut start = 0; for (i, c) in s.chars().enumerate() { ifletSome(&index) = char_index.get(&c) { if index >= start { start = index + 1; } } max_length = max_length.max(i - start + 1); char_index.insert(c, i); } max_length asi32 } }