一、字首和給定乙個放有字元和數字的陣列,找到最長的子陣列,且包含的字元和數字的個數相同。
返回該子陣列,若存在多個最長子陣列,返回左端點最小的。若不存在這樣的陣列,返回乙個空陣列。
示例 1
:輸入:
["a"
,"1"
,"b"
,"c"
,"d"
,"2"
,"3"
,"4"
,"e"
,"5"
,"f"
,"g"
,"6"
,"7"
,"h"
,"i"
,"j"
,"k"
,"l"
,"m"
]輸出:
["a"
,"1"
,"b"
,"c"
,"d"
,"2"
,"3"
,"4"
,"e"
,"5"
,"f"
,"g"
,"6"
,"7"
]示例 2
:輸入:
["a"
,"a"
]輸出:
array.length <=
100000
字母為1,數字為-1,記錄當前字元前面的和;字首相同說明兩個字元區間的合為0;記錄每個字首和第一次出現的位置;選取最長的區間。
class
solution
:def
findlongestsubarray
(self, array: list[
str])-
> list[
str]
: larray =
len(array)
memo =[-
2for i in
range
((larray <<1)
+1)]
memo[larray]=-
1 begin, end =0,
0 res, count =0,
0for i in
range
(larray)
: is_num =
true
for ch in array[i]:if
(ord
(ch)
<
ord(
'0'))or
(ord
(ch)
>
ord(
'9')):
is_num =
false
break
count +=-1
if is_num else
1if memo[count + larray]
<=-2
: memo[count + larray]
= i elif i - memo[count + larray]
> res:
begin, end = memo[count + larray]+1
, i+
1 res = i - memo[count + larray]
return array[begin:end]
python 判斷是字母 數字
str 1 123 str 2 abc str 3 123abc 用isdigit函式判斷是否數字 print str 1.isdigit ture print str 2.isdigit false print str 3.isdigit false 用isalpha判斷是否字母 print st...
VBA列數字與字母互換
內容來至excelhome vba列數字與字母互換 方案一 速度 快 function numtostr byval num as long as string 數字轉字母 dim m as long if num 1 then exit function dom num mod 26 if m 0...
彙編中鍵入數字與字母處理
彙編中接收資料以及卡上下界 key in equ 01h mov ah,key in int 21h 呼叫dos系統功能,使鍵入的字元回顯在螢幕上,同時把鍵入的字元以ascii 碼的形式儲存在al中 sub al,30h 將ascii碼處理成輸入的十六進製制數 jl exit 卡數字的下界 cmp ...