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
94
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
320
Unearth Ruby builtin Gems 發掘 Ruby 的內建 Gems
johnlinvc
0
400
Ruby 型別檢查工具簡介
johnlinvc
0
630
Swift Actor 實作探索
johnlinvc
0
200
用 mruby 來寫跨平台工具
johnlinvc
0
130
Actor model 簡介
johnlinvc
0
240
一起玩 Helm 3
johnlinvc
1
160
為什麼 App 卡卡的
johnlinvc
2
1.3k
如何使用 byebug 來除錯 Ruby 程式
johnlinvc
0
270
Other Decks in Programming
See All in Programming
まずはプロンプトガイドを読もう、話はそれからだ
kiakiraki
0
140
AI時代のPHPer生存戦略 ~「言語、もうなんでもよくない?」に本気で向き合う~
vivion
0
820
<title><a id="</title>君はこのHTMLをパースできるか"></a></title> #雑LT_study
pizzacat83
0
150
そこに3びきプロダクトがいるじゃろう——生成AI時代における“価値が届かない理由”の構造
kosuket
0
520
【QA Test Talk Vol.8】AI-DLC による Whole Team Approach の加速
pkshadeck
PRO
0
210
AI Engineeringは、AIプロダクトだけのものか? 〜AIがソフトウェアを作る時代の新しい当たり前〜 / No AI in your product. AI Engineering in your development.
rkaga
4
480
AIが無かった頃の素敵な出会いの話
codmoninc
1
470
Android CLI
fornewid
0
230
属人化した知識を、 AIが辿れる地図にする
pkshadeck
PRO
1
190
Laravel Boostに学ぶ、AIにPHPを書かせる技術 〜OSSの実装から蒸留するエージェント制御の王道〜
kentaroutakeda
3
730
jsmini JavaScript Engine を作ってみた話
yosuke_furukawa
PRO
0
330
実装をデザインガイドラインに追従させるための取り組み / 260731-dip-mosh-design-system
dachi023
0
5.7k
Featured
See All Featured
WENDY [Excerpt]
tessaabrams
11
39k
Reflections from 52 weeks, 52 projects
jeffersonlam
356
21k
Money Talks: Using Revenue to Get Sh*t Done
nikkihalliwell
0
470
Testing 201, or: Great Expectations
jmmastey
46
8.2k
Designing for Timeless Needs
cassininazir
1
450
DevOps and Value Stream Thinking: Enabling flow, efficiency and business value
helenjbeal
1
350
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
2.4k
Save Time (by Creating Custom Rails Generators)
garrettdimon
PRO
32
4.4k
Deep Space Network (abreviated)
tonyrice
0
260
DBのスキルで生き残る技術 - AI時代におけるテーブル設計の勘所
soudai
PRO
68
57k
Producing Creativity
orderedlist
PRO
348
40k
Marketing Yourself as an Engineer | Alaka | Gurzu
gurzu
0
280
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