Leetcode3 无重复字符的最长子串
Golang 实现
实现思路:
首先需要一个map
记录每个出现的字母出现的位置 id_prev
当这个字母再次出现,新的位置为 id, 那么这个不重复字符串长度即为 id-id_prev
然后重新构造map
func lengthOfLongestSubstring(s string) int {
longest := 0
length := 0
hash := make(map[rune]int, 0)
for i, c := range s {
if prev, ok := hash[c]; ok {
length = i - prev
hash = make(map[rune]int, 0)
for j := i; j > prev; j-- {
hash[rune(s[j])] = j
}
} else {
length++
hash[c] = i
}
if length > longest {
longest = length
}
}
return longest
}
队列的实现方式
Leetcode6 Z 字形变换
class Solution:
def convert(self, s, numRows):
if not numRows > 1:
return s
if numRows == 2:
s1 = s[::2]
s2 = s[1::2]
s3 = s1 + s2
return s3
s_Initialize = [''] * numRows
# print(s_row)
i = 0
n = len(s)
while i < n:
for count_columns in range(numRows):
if i < n:
s_Initialize[count_columns] += s[i] # 这里进行了将numRows个字符从上往下安置入每一行
# print(s_row)
i += 1
# print(s_row)
for count_Rows in range(numRows - 2, 0, -1): # 这里进行了将numRows-2个字符从下往上安置入每一行
if i < n:
s_Initialize[count_Rows] += s[i]
i += 1
return ''.join(s_Initialize)
Leetcode12 整数转罗马数字
class Solution:
def intToRoman(self, num: int) -> str:
nums = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1]
romans = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"]
index = 0
res = ''
while index < 13:
# 注意:这里是等于号,表示尽量使用大的"面值"
while num >= nums[index]:
res += romans[index]
num -= nums[index]
index += 1
return res
Leetcode17 电话号码的字母组合
Golang实现
func letterCombinations(digits string) []string {
hash := map[rune][]string{
'2': {
"a", "b", "c"},
'3': {
"d", "e", "f"},
'4': {
"g"