File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /**
2+ * 1021. Remove Outermost Parentheses
3+ *
4+ * @param {string } s
5+ * @return {string }
6+ */
7+ var removeOuterParentheses = function ( s ) {
8+ // () 須相等數量,才能變成一對
9+ let splitS = s . split ( "" ) ;
10+ let res = "" ;
11+ let count = 1 ;
12+ for ( let i = 1 ; i < splitS . length ; ++ i ) {
13+ if ( splitS [ i ] === "(" ) {
14+ count ++ ;
15+ if ( count > 1 ) {
16+ res += "(" ;
17+ }
18+ } else {
19+ if ( count > 1 ) {
20+ res += ")" ;
21+ }
22+ count -- ;
23+ }
24+ }
25+ return res ;
26+ } ;
27+ // let s = "(()())(())"
28+ // "()()()"
29+ let s = "(()())(())(()(()))" ;
30+ // "()()()()(())"
31+ // "(()()) (())(()(()))" => (()())
32+ console . log ( removeOuterParentheses ( s ) ) ;
Original file line number Diff line number Diff line change 1+ /**
2+ * 1844. Replace All Digits with Characters
3+ *
4+ * @param {string } s
5+ * @return {string }
6+ */
7+ var replaceDigits = function ( s ) {
8+ const reg = new RegExp ( '^[0-9]+$' ) ;
9+ // 數字前面的一個字母表示該字母要往後增加幾個才能變成新字母。a2 => a後面的第2個字母,回傳ac
10+ // 數字所在index都是奇數
11+ let res = "" ;
12+ for ( let i = 0 ; i < s . length ; ++ i ) {
13+ // 前一個(字母)
14+ let prev = s [ i - 1 ] ;
15+ // 目前所在位置(數字)
16+ let next = s [ i ] ;
17+ if ( i % 2 !== 0 ) {
18+ // char.charCodeAt() 取得ascii
19+ // ascii 轉字母 String.fromCharCode(ascii)
20+ res += String . fromCharCode ( prev . charCodeAt ( ) + parseInt ( next ) )
21+ } else {
22+ res += s [ i ] ;
23+ }
24+ }
25+ return res ;
26+ } ;
27+ let s = "a1c1e1"
28+ // Output: "abcdef"
29+ // Explanation: The digits are replaced as follows:
30+ // - s[1] -> shift('a',1) = 'b'
31+ // - s[3] -> shift('c',1) = 'd'
32+ // - s[5] -> shift('e',1) = 'f'
33+ console . log ( replaceDigits ( s ) ) ;
Original file line number Diff line number Diff line change @@ -1048,34 +1048,33 @@ let numerator = 1, denominator = 2;
10481048// console.log(fractionToDecimal(numerator,denominator));
10491049
10501050/**
1051- * 1021. Remove Outermost Parentheses
1051+ * 2315. Count Asterisks
10521052 *
1053+ * 給一個字串,字串中可能會有"|"和"*",找出字串中被一組 | 包住的連續 ** 有幾個
10531054 * @param {string } s
1054- * @return {string }
1055+ * @return {number }
10551056 */
1056- var removeOuterParentheses = function ( s ) {
1057- // () 須相等數量,才能變成一對
1057+ var countAsterisks = function ( s ) {
1058+ // 須判斷字串中是否有"*"符號
1059+ // 拆成陣列,計算一組"|"中有幾個連續"*"
10581060 let splitS = s . split ( "" ) ;
1059- let res = "" ;
1060- let count = 1 ;
1061- for ( let i = 1 ; i < splitS . length ; ++ i ) {
1062- if ( splitS [ i ] === "(" ) {
1063- count ++ ;
1064- if ( count > 1 ) {
1065- res += "(" ;
1066- }
1067- } else {
1068- if ( count > 1 ) {
1069- res += ")" ;
1061+ let ans = 0 ;
1062+ let pairs = false ;
1063+ for ( let i = 0 ; i < splitS . length ; ++ i ) {
1064+ if ( splitS [ i ] === "|" ) {
1065+ if ( pairs ) {
1066+ pairs = true ;
1067+ continue ;
1068+ } else {
1069+ pairs = false ;
10701070 }
1071- count -- ;
10721071 }
1072+
10731073 }
1074- return res ;
1074+ return ans ;
10751075} ;
1076- // let s = "(()())(())"
1077- // "()()()"
1078- let s = "(()())(())(()(()))" ;
1079- // "()()()()(())"
1080- // "(()()) (())(()(()))" => (()())
1081- console . log ( removeOuterParentheses ( s ) ) ;
1076+ // let s = "l|*e*et|c**o|*de|"
1077+ // Output: 2
1078+ let s = "yo|uar|e**|b|e***au|tifu|l" ;
1079+ // 5
1080+ console . log ( countAsterisks ( s ) )
Original file line number Diff line number Diff line change 2020
2121# oop
2222# from file name(module) import the class.
23- from oop .Animal import Animal
24- from oop .Tortoise import Tortoise
25- from oop .Lion import Lion
26- from oop .Behavior import move
27-
28- greek = Tortoise ("福氣" ,7 ,"veggie" ,"地中海型陸龜" )
29- greek .eat ()
30- greek .hibernation ()
31- greek .environment ()
32- greek .attack ()
33- greek .affend ()
34- print (greek .specice )
35- print (greek .avgWeight (7 ,5 ))
23+ # from oop.Animal import Animal
24+ # from oop.Tortoise import Tortoise
25+ # from oop.Lion import Lion
26+ # from oop.Behavior import move
27+
28+ # greek = Tortoise("福氣",7,"veggie","地中海型陸龜")
29+ # greek.eat()
30+ # greek.hibernation()
31+ # greek.environment()
32+ # greek.attack()
33+ # greek.affend()
34+ # print(greek.specice)
35+ # print(greek.avgWeight(7,5))
3636
3737# lion = Lion("獅子","未知","肉食")
3838# lion.eat()
@@ -250,3 +250,18 @@ def isPrime(element:int):
250250# print(closestPrimes(left,right))
251251
252252
253+ def replaceDigits (s : str ) -> str :
254+ res = ""
255+ for i in range (len (s )):
256+ prev = s [i - 1 ]
257+ current = s [i ]
258+
259+ if i % 2 != 0 :
260+ res += chr (ord (prev ) + int (current ))
261+ else :
262+ res += s [i ]
263+
264+ return res
265+ s = "a1c1e1"
266+ # Output: "abcdef"
267+ print (replaceDigits (s ))
You can’t perform that action at this time.
0 commit comments