Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Swift Warmup : power of three
Search
Johnlin
December 13, 2016
Programming
88
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Swift Warmup : power of three
Johnlin
December 13, 2016
More Decks by Johnlin
See All by Johnlin
用 MLIR 實作 一個 Ruby IR (intermediate representation)
johnlinvc
0
290
Unearth Ruby builtin Gems 發掘 Ruby 的內建 Gems
johnlinvc
0
370
Ruby 型別檢查工具簡介
johnlinvc
0
610
Swift Actor 實作探索
johnlinvc
0
200
用 mruby 來寫跨平台工具
johnlinvc
0
120
Actor model 簡介
johnlinvc
0
240
一起玩 Helm 3
johnlinvc
1
160
為什麼 App 卡卡的
johnlinvc
2
1.3k
如何使用 byebug 來除錯 Ruby 程式
johnlinvc
0
260
Other Decks in Programming
See All in Programming
AI 時代のソフトウェア設計の学び方
masuda220
PRO
29
12k
Lessons from Spec-Driven Development
simas
PRO
0
180
Inside Stream API
skrb
1
690
The Arts and Crafts of Work in the AI Era — Toward Mastery in Software Development
kuranuki
1
750
気づいたらRubyで100作品 ー クリエイティブコーディングが生活の一部になるまで / 100 Ruby Sketches Later: How Creative Coding Became Part of My Life
chobishiba
3
570
Claspは野良GASの夢をみるか
takter00
0
190
Lemonade + Foundry Toolkit でお手軽アプリ開発
seosoft
1
320
Snowflake Summitでの新機能 CoCo / CoWork / snowflake-summit-2026-overall-what-new-coco
tatsuhiro
1
110
AI時代のUIはどこへ行く?その2!
yusukebe
21
7.1k
AutonomyとControlのあいだ:Graflowで記述するAIエージェント協調
myui
0
120
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
120
Hunting Vulnerabilities in Symfony with LLMs
vinceamstoutz
0
540
Featured
See All Featured
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Tell your own story through comics
letsgokoyo
1
950
How GitHub (no longer) Works
holman
316
150k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
200
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
ReactJS: Keep Simple. Everything can be a component!
pedronauck
666
130k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.1k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
250
1.3M
Building a A Zero-Code AI SEO Workflow
portentint
PRO
0
570
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.7k
Mind Mapping
helmedeiros
PRO
1
250
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
250
Transcript
Swift ఔࣜখ Power of Three John Lin
326. Power of Three • https://leetcode.com/problems/power-of-three/ • Given an integer,
write a function to determine if it is a power of three. • څ㟬ҰݸᏐɼሜҰݸവᏐᏗଞੋෆੋ 3 త࣍ํɻ
• Follow up: Could you do it without using any
loop / recursion? • Ճ: ՄҎෆ༻౸ ᅲ / ᬇ बղग़Ҋ䆩ʁ
• ༌ೖ: 81 • ༌ग़: true • 3*3*3*3 == 81
• ༌ೖ: 5566 • ༌ग़: false • 2 * 11
* 11 * 23 == 5566
ࠎՍ class Solution { func isPowerOfThree(_ n: Int) -> Bool
{ } }
ԋࢉ๏ 0 • ೳෆೳඃ 3 আɼՄҎతबআҎ 3ɻ • Ұ၏౸ෆೳඃ 3
আɼ҃ੋԙ 0 ҝࢭɼಹ ݸᏐࣈੋෆੋ 1
class Solution1 { func isPowerOfThree(_ n: Int) -> Bool {
var nn = n while(nn%3==0 && nn>0){ nn = nn / 3 } return nn==1 } }
class Solution1 { func isPowerOfThree(_ n: Int) -> Bool {
var nn = n while(nn%3==0 && nn>0){ nn = nn / 3 } return nn==1 } } ࣌ؒෳᯑ: O(log(n)) ۭؒෳᯑ: O(1) Runtime: 289 ms
ԋࢉ๏ 1 • ᏐࣈࡾਐҐɻ 5566 => “21122011” • 1 =>
“1”, 3 => “10”, 9 => “100”, 27 => “1000” • 3 త࣍ํҰఆ။Ṝᒬ 100000….. ɼबੋআྃ ։಄ੋ 1ɼଖଞੋ 0 • ᒾҰ༗ᔒ༗Ṝࠣಛੑबɻ
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
let threeBased = String(n, radix:3) guard let head = threeBased.characters.first, head == "1" else { return false } guard threeBased.characters.count > 1 else { return true } let tail = String(threeBased.characters.dropFirst()) return Int(tail) == 0 } } ᔒ༗༻౸ᅲ
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
let threeBased = String(n, radix:3) guard let head = threeBased.characters.first, head == "1" else { return false } guard threeBased.characters.count > 1 else { return true } let tail = String(threeBased.characters.dropFirst()) return Int(tail) == 0 } } ෆա䋯ಘຫ ࣌ؒෳᯑ: O(log(n)) ۭؒෳᯑ: O(log(n)) Runtime: 415 ms
ԋࢉ๏ 2 • ኺവᏐతఆٛ func isPowerOfThree(_ n: Int) -> Bool
ՄҎग़༌ೖᆴੋ༗্ݶతɼबੋ Int.max = 9223372036854775807 • ॴҎཁፙ౸্ݶ㚎࠷େత3త࣍ํɼ࠶ n ೳෆ ೳআଞɼबಓ n ੋෆੋ 3 త࣍ํྃɻ
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
let ms = String(Int.max, radix:3) let max3 = "1" + repeatElement("0", count: ms.characters.count-1).joined() let max3i = Int(max3, radix:3)! //4052555153018976267 return n > 0 && max3i % n == 0 } }
class Solution { func isPowerOfThree(_ n: Int) -> Bool {
return n > 0 && 4052555153018976267 % n == 0 } } ࣌ؒෳᯑ: O(1) ۭؒෳᯑ: O(1) Runtime: 255 ms
Q&A