Leetcode的3道新題分析

Leetcode 最近出了幾道新題,感覺思路還是挺直接的蜻懦,刷完了之后,把注釋標(biāo)在后面。


No.1 Happy Number

Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example: 19 is a happy number
1^2 + 9^2 = 82
8^2 + 2^2 = 68
6^2 + 8^2 = 100
12 + 0^2 + 0^2 = 1

My Solution

class Solution {
public:
    //Trick 1: map for small scale exponential operations
    int expoMap[10]={0,1,4,9,16,25,36,49,64,81};
    
    bool isHappy(int n) {
        if(n < 10){
            if(n == 1 || n == 7){ //Base case 
                return true;
            }else{
                return false;
            }
        }
        int m = breakSum(n); 
        return isHappy(m);
    }

    int breakSum(int n){
        int sum = 0;
        while(n){   //classic part in coding interview
            int a = n%10;
            sum += expoMap[a];
            n = n/10;
        }
        return sum;
    }
};

No.2 Isomorphic Strings

Given two strings s and t (same length), determine if they are isomorphic. Two strings are isomorphic if the characters in s can be replaced to get t. All occurrences of a character must be replaced with another character while preserving the order of characters. No two characters may map to the same character but a character may map to itself.

For example,
Given "egg", "add", return true.
Given "foo", "bar", return false.
Given "paper", "title", return true.

My Solution

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        int a[256] = {0}, b[256] = {0};
        int len = s.size();
        for(int i = 0; i < len; i++){
            if(a[s[i]] > 0 || b[t[i]] > 0){
                if(a[s[i]] == b[t[i]]){ //means they appear at the same position at the 1st time 
                    continue;
                }else{
                    return false;
                }
            }else{
                a[s[i]]=i+1; //record the first time they show up
                b[t[i]]=i+1; //+1 is the way to distinguish them from 0
            }
        }
        return true;
    }
};

No.3 Count Prime Numbers

Question:

Count the number of prime numbers less than a non-negative number, n.

This solution is based on Sieve of Eratosthenes, which might sound too academic, but actually pretty straightforward.

My Solution [Time: O(n) Space:O(n) ]

class Solution {
public:
    int countPrimes(int n) {
        if(n <= 2) {      //Base case;
            return 0;
        }
        bool checkPrime[n];              
        for(int i = 0; i < n; i++) {
            checkPrime[i]=true;
        }
        int prime = 2;
        while(prime <= sqrt(n)){    //If a number is not a prime, this number at least has a factor <= n/2
            for(int x = 2; prime*x < n; x++){
                checkPrime[x*prime] = false;
            }
            prime++; //move to the next number
            while(prime <= n/2 && checkPrime[prime] == false) {  //if the number has been marked as not a prime, jump over it.
                prime++;
            }
        }
        int count = 0;
        for(int i = 2; i < n; i++){  //Trap: remember start from 2.
            if(checkPrime[i]) count++;
        }
        return count;
    }
};
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末弄跌,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子尝苇,更是在濱河造成了極大的恐慌铛只,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茎匠,死亡現(xiàn)場離奇詭異格仲,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)诵冒,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進(jìn)店門凯肋,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人汽馋,你說我怎么就攤上這事侮东∪” “怎么了?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵悄雅,是天一觀的道長驱敲。 經(jīng)常有香客問我,道長宽闲,這世上最難降的妖魔是什么众眨? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮容诬,結(jié)果婚禮上娩梨,老公的妹妹穿的比我還像新娘。我一直安慰自己览徒,他們只是感情好狈定,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著习蓬,像睡著了一般纽什。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上躲叼,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天芦缰,我揣著相機(jī)與錄音,去河邊找鬼押赊。 笑死饺藤,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的流礁。 我是一名探鬼主播涕俗,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼神帅!你這毒婦竟也來了再姑?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤找御,失蹤者是張志新(化名)和其女友劉穎元镀,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體霎桅,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡栖疑,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了滔驶。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片遇革。...
    茶點(diǎn)故事閱讀 39,731評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出萝快,到底是詐尸還是另有隱情锻霎,我是刑警寧澤,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布揪漩,位于F島的核電站旋恼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏奄容。R本人自食惡果不足惜冰更,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望昂勒。 院中可真熱鬧冬殃,春花似錦、人聲如沸叁怪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽奕谭。三九已至,卻和暖如春痴荐,著一層夾襖步出監(jiān)牢的瞬間血柳,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工生兆, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留难捌,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓鸦难,卻偏偏與公主長得像根吁,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子合蔽,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,629評論 2 354

推薦閱讀更多精彩內(nèi)容