You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
classSolution{/// 0 ms 19.70 MBfunctwoSum(_nums:[Int],_target:Int)->[Int]{varmaps:[Int:Int]=[:]for(i,num)innums.enumerated(){letsearch=target-numifletindex=maps[search]{return[index,i]}else{maps[num]=i}}return[]}/// O(n) time, O(n) space}
Alternative implementations
classSolution{/// 30 ms 19.34 MBfunctwoSum(_nums:[Int],_target:Int)->[Int]{var_nums=numsfor(i,numA)innums.enumerated(){_nums.remove(at:0)for(j,numB)in_nums.enumerated(){ifnumA+numB==target{return[i,i+j+1]}}}return[]}/// O(n²) time, O(1) space}
classSolution{/// 0 ms 19.79 MBfuncisValid(_s:String)->Bool{letmap:[Character:Character]=["(":")","{":"}","[":"]"]varcurrent=[Character]()forcharins{ifletsearchChar=map[char]{current.append(searchChar)}elseifchar!=current.popLast(){returnfalse}}returncurrent.isEmpty}/// O(n) time, O(n) space}
Alternative implementations
classSolution{/// 2 ms 20.15 MBfuncisValid(_s:String)->Bool{letmap:[Character:Character]=["(":")","{":"}","[":"]"]varcurrent=""forcharins{ifletsearchChar=map[char]{current.append(searchChar)}elseifchar!=current.popLast(){returnfalse}}returncurrent.isEmpty}}
classSolution{/// 23 ms 20.09 MBfuncisValid(_s:String)->Bool{letmap:[Character:Character]=["(":")","{":"}","[":"]"]varcurrent=""forcharins{ifletsearchChar=map[char]{current.append(searchChar)}else{ifcurrent.last==char{current=String(current.dropLast())}else{returnfalse}}}returncurrent.isEmpty}}
classSolution{/// 25 ms 19.99 MBfuncisValid(_s:String)->Bool{varcurrent=""forcharins{switchchar{case"(":current.append(")")case"{":current.append("}")case"[":current.append("]")default:ifcurrent.last==char{current=String(current.dropLast())}else{returnfalse}}}returncurrent.isEmpty}}
classSolution{/// 141 ms 20.37 MBfuncisValid(_s:String)->Bool{letmap:[Character:Character]=["(":")","{":"}","[":"]"]varcurrent=[Character]()forcharins{ifletsearchChar=map[char]{current.append(searchChar)}else{ifcurrent.last==char{current=current.dropLast()}else{returnfalse}}}returncurrent.isEmpty}}
classSolution{/// 0 ms 19.54 MBfuncmergeTwoLists(_list1:ListNode?,_list2:ListNode?)->ListNode?{switch(list1?.val,list2?.val){caselet(val1,val2)whereval1==nil&&val2!=nil:returnlist2caselet(val1,val2)whereval1!=nil&&val2==nil:returnlist1caselet(val1,val2)whereval1!=nil&&val2!=nil:returnval1??0<val2??0?ListNode(val1??0,(mergeTwoLists(list1?.next,list2))):ListNode(val2??0,(mergeTwoLists(list1,list2?.next)))default:returnnil}}}
classSolution{/// 303 ms 21.14 MBfunclargestRectangleArea(_heights:[Int])->Int{varmaxArea=0varskeep=falsevarskeepCount=0for(i,height)inheights.enumerated(){varcurentHeigth=(skeepCount+1)*heightvarflag=trueforheightRigthinheights[(i+1)...]{ifheightRigth>=height{curentHeigth+=heightifheightRigth==height,flag{skeep=trueskeepCount+=1break}flag=false}else{break}}ifskeep{skeep=falsecontinue}forheightLeftinheights[..<(i-skeepCount)].reversed(){ifheightLeft>=height{curentHeigth+=height}else{break}}skeepCount=0maxArea=max(maxArea,curentHeigth)}returnmaxArea}}
Alternative implementations
classSolution{/// over timefunclargestRectangleArea(_heights:[Int])->Int{varmaxArea=0varmap=[Int:Int]()for(i,height)inheights.enumerated(){map[i]=height}foriin0..<heights.count{letheight=map[i]??0varcurentHeigth=heightforleftin(0..<i).reversed(){ifmap[left]??0>=height{curentHeigth+=height}else{break}}forrigthin(i+1)..<heights.count{ifmap[rigth]??0>=height{curentHeigth+=height}else{break}}maxArea=max(maxArea,curentHeigth)}returnmaxArea}}
classSolution{/// over timefunclargestRectangleArea_over2(_heights:[Int])->Int{varmaxArea=0for(i,height)inheights.enumerated(){varcurentHeigth=heightforheightLeftinheights[..<i].reversed(){ifheightLeft>=height{curentHeigth+=height}else{break}}forheightRigthinheights[(i+1)...]{ifheightRigth>=height{curentHeigth+=height}else{break}}maxArea=max(maxArea,curentHeigth)}returnmaxArea}}
classSolution{/// 0 ms 20.84 MBfuncmaxProfit(_prices:[Int])->Int{varmaxProfit=0varbuy=Int.maxforpriceinprices{buy=min(buy,price)maxProfit=max(price-buy,maxProfit)}returnmaxProfit}}
Alternative implementations
classSolution{/// 4561 ms 20.68 MBfuncmaxProfit(_prices:[Int])->Int{varmaxProfit=0varcurrent=pricesforpinprices{letbuy=current.removeFirst()forpriceincurrent{ifprice<=p{break}maxProfit=max(price-buy,maxProfit)}}returnmaxProfit}}
classSolution{/// over timefuncmaxProfit(_prices:[Int])->Int{varmaxProfit=0varcurrent=pricesfor _ inprices{letbuy=current.removeFirst()forpriceincurrent{maxProfit=max(price-buy,maxProfit)}}returnmaxProfit}}
classSolution{/// 7 ms 20.76 MBfuncisPalindrome(_s:String)->Bool{letmap:[Character:String]=["A":"a","B":"b","C":"c","D":"d","E":"e","F":"f","G":"g","H":"h","I":"i","J":"j","K":"k","L":"l","M":"m","N":"n","O":"o","P":"p","Q":"q","R":"r","S":"s","T":"t","U":"u","V":"v","W":"w","X":"x","Y":"y","Z":"z","a":"a","b":"b","c":"c","d":"d","e":"e","f":"f","g":"g","h":"h","i":"i","j":"j","k":"k","l":"l","m":"m","n":"n","o":"o","p":"p","q":"q","r":"r","s":"s","t":"t","u":"u","v":"v","w":"w","x":"x","y":"y","z":"z","0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9"]letcurrent=s.compactMap{map[$0]}letcount=current.count/2letleft=current.prefix(count)letrigth=current.suffix(count).reversed()returnleft.elementsEqual(rigth)}}
Alternative implementations
classSolution{/// 9 ms 21.5 MBfuncisPalindrome_9(_s:String)->Bool{letcurrent=s.compactMap{$0.isLetter||$0.isNumber?$0.lowercased():nil}letcount=current.count/2letleft=current.prefix(count)letrigth=current.suffix(count).reversed()returnleft.elementsEqual(rigth)}}
classSolution{/// 382 ms 20.54 MBfuncisPalindrome_382(_s:String)->Bool{letmap:[Character:String]=["A":"a","B":"b","C":"c","D":"d","E":"e","F":"f","G":"g","H":"h","I":"i","J":"j","K":"k","L":"l","M":"m","N":"n","O":"o","P":"p","Q":"q","R":"r","S":"s","T":"t","U":"u","V":"v","W":"w","X":"x","Y":"y","Z":"z","a":"a","b":"b","c":"c","d":"d","e":"e","f":"f","g":"g","h":"h","i":"i","j":"j","k":"k","l":"l","m":"m","n":"n","o":"o","p":"p","q":"q","r":"r","s":"s","t":"t","u":"u","v":"v","w":"w","x":"x","y":"y","z":"z","0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9"]varcurrent=s.compactMap{map[$0]}whilecurrent.count>1{ifcurrent.removeFirst()!=current.popLast(){returnfalse}}returntrue}}
classSolution{/// over timefuncisPalindrome_over(_s:String)->Bool{letmap=["A":"a","B":"b","C":"c","D":"d","E":"e","F":"f","G":"g","H":"h","I":"i","J":"j","K":"k","L":"l","M":"m","N":"n","O":"o","P":"p","Q":"q","R":"r","S":"s","T":"t","U":"u","V":"v","W":"w","X":"x","Y":"y","Z":"z","a":"a","b":"b","c":"c","d":"d","e":"e","f":"f","g":"g","h":"h","i":"i","j":"j","k":"k","l":"l","m":"m","n":"n","o":"o","p":"p","q":"q","r":"r","s":"s","t":"t","u":"u","v":"v","w":"w","x":"x","y":"y","z":"z","0":"0","1":"1","2":"2","3":"3","4":"4","5":"5","6":"6","7":"7","8":"8","9":"9"]varcurrent=swhile!current.isEmpty{varleft=""repeat{ifcurrent.isEmpty{returntrue}left=map[String(current.prefix(1))]??""current=String(current.dropFirst())}whileleft.isEmptyvarrigth=""repeat{ifcurrent.isEmpty{returntrue}rigth=map[String(current.suffix(1))]??""current=String(current.dropLast())}whilerigth.isEmptyifleft!=rigth{returnfalse}}returntrue}}
classSolution{/// 0 ms 19.81 MBfuncinvertTree(_root:TreeNode?)->TreeNode?{ifletval=root?.val{returnTreeNode(val,invertTree(root?.right),invertTree(root?.left))}else{returnnil}}}