LeetCode #771.Jewels and Stones python

Question

You're given strings J representing the types of stones that are jewels, and S representing the stones you have. Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

The letters in J are guaranteed distinct, and all characters in J and S are letters. Letters are case sensitive, so "a" is considered a different type of stone from "A".

Example1:

Input: J = "aA", S = "aAAbbbb"
Output: 3

Example2:

Input: J = "z", S = "ZZ"
Output: 0

Thought(思路)

It is easy to do. I will talk about thought and data structure.(思路很簡單,大致說一下蓝翰,主要說一下可能涉及到的數(shù)據(jù)結(jié)構(gòu)害碾。)
J include unrepeated elements. Let us loop J and check whether everyone of J is included S and sum the counts.(J是不會重復(fù)的,循環(huán)J中所有元素春锋,看是否在S中累贤,在舱权,就統(tǒng)計(jì)個數(shù)矗晃。)

Data structure

Array, Hashtable/Hashmap, Tree(Linklist).
In python3, Accessing set is faster than accessing list.(在python3中,訪問集合比訪問列表速度快)
How to use map().
map function takes an arbitrary function and applies it over the elements of an iterable. An iterable, loosely speaking, is anything which allows you to loop through its elements.
Consider this example,

>>> round(2.3)
2
>>> round(4.9)
5
>>> rounded = map(round, [2.3, 4.9])
>>> list(rounded)
[2, 5]

There are two main components to this example:

  • the round function
  • the iterable i.e., list [2.3, 4.9]

Let’s say you have this list of hundred numbers that you want to round off. Of course, you are not going to write hundred round statements. One of the many ways to achieve this [in Python] is to use map.

You can provide map with the function as argument, and the list of elements on which it has to be applied. The result is a generator expression. The last step retrieves the results from the generator.


map

Now, let’s look at the code below:

a, b = map(int, input().split())

Here’s a breakdown:

In [0]: input().split()
12 13
Out [0]: ['12', '13']

That is, take an input, split it and create a list, then convert them into an integer by mapping the function int, finally save the results in variables a, b.

That’s one way you can take multiple inputs on the same line.
source from: https://www.quora.com/What-does-a-map-function-do-in-Python-3

Code

#the First solution
class Solution:
    def numJewelsInStones(self, J, S):
        """
        :type J: str
        :type S: str
        :rtype: int
        """
        j = set(list(J))
        s = list(S)
        count = 0
        for stone in s:
            if stone in j:
                count += 1
        return count
#the Second
class solution:
    def numJewelsInStones(self,J,S):
        return sum(s in J for s in S)
#the Third
class solution:
     def numJewelsInStones(self, J, S):
         return sum(map(J.count, S))
#the Fourth
class solution:
      def numJewelsInStones(self, J, S):
          return sum(map(S.count, J))  

Complexity

Time complexity: O(s*j)(s is length of S,j is length of J)(為字符串S的長度宴倍,j為字符串J的長度)
Space complexity: O(1)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末张症,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鸵贬,更是在濱河造成了極大的恐慌俗他,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,744評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件阔逼,死亡現(xiàn)場離奇詭異兆衅,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)嗜浮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,505評論 3 392
  • 文/潘曉璐 我一進(jìn)店門羡亩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人危融,你說我怎么就攤上這事畏铆。” “怎么了吉殃?”我有些...
    開封第一講書人閱讀 163,105評論 0 353
  • 文/不壞的土叔 我叫張陵辞居,是天一觀的道長楷怒。 經(jīng)常有香客問我,道長瓦灶,這世上最難降的妖魔是什么鸠删? 我笑而不...
    開封第一講書人閱讀 58,242評論 1 292
  • 正文 為了忘掉前任,我火速辦了婚禮贼陶,結(jié)果婚禮上刃泡,老公的妹妹穿的比我還像新娘。我一直安慰自己碉怔,他們只是感情好捅僵,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,269評論 6 389
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著眨层,像睡著了一般。 火紅的嫁衣襯著肌膚如雪上荡。 梳的紋絲不亂的頭發(fā)上趴樱,一...
    開封第一講書人閱讀 51,215評論 1 299
  • 那天,我揣著相機(jī)與錄音酪捡,去河邊找鬼叁征。 笑死,一個胖子當(dāng)著我的面吹牛逛薇,可吹牛的內(nèi)容都是我干的捺疼。 我是一名探鬼主播,決...
    沈念sama閱讀 40,096評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼永罚,長吁一口氣:“原來是場噩夢啊……” “哼啤呼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起呢袱,我...
    開封第一講書人閱讀 38,939評論 0 274
  • 序言:老撾萬榮一對情侶失蹤官扣,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后羞福,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體惕蹄,經(jīng)...
    沈念sama閱讀 45,354評論 1 311
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,573評論 2 333
  • 正文 我和宋清朗相戀三年治专,在試婚紗的時候發(fā)現(xiàn)自己被綠了卖陵。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,745評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡张峰,死狀恐怖泪蔫,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情挟炬,我是刑警寧澤鸥滨,帶...
    沈念sama閱讀 35,448評論 5 344
  • 正文 年R本政府宣布嗦哆,位于F島的核電站,受9級特大地震影響婿滓,放射性物質(zhì)發(fā)生泄漏老速。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,048評論 3 327
  • 文/蒙蒙 一凸主、第九天 我趴在偏房一處隱蔽的房頂上張望橘券。 院中可真熱鬧,春花似錦卿吐、人聲如沸旁舰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,683評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽箭窜。三九已至,卻和暖如春衍腥,著一層夾襖步出監(jiān)牢的瞬間磺樱,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,838評論 1 269
  • 我被黑心中介騙來泰國打工婆咸, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留竹捉,地道東北人。 一個月前我還...
    沈念sama閱讀 47,776評論 2 369
  • 正文 我出身青樓尚骄,卻偏偏與公主長得像块差,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子倔丈,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,652評論 2 354

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

  • 五分鐘商學(xué)院一年課程結(jié)束了憨闰,事實(shí)上我深深地感覺到自己很多概念沒吃透,錯過太多知識點(diǎn)乃沙。 離下一季開課還有一段時間起趾,立...
    身兼數(shù)職老板娘閱讀 364評論 0 1
  • 默 我曾與時光賽跑, 我的世界都是光警儒。 有一天你闖入我的世界训裆,看著我, 你眼中的星空蜀铲,讓我不知...
    半部小說閱讀 168評論 0 2
  • 雖然現(xiàn)在整天敲代碼敲到凌晨累得不想動边琉,但是當(dāng)你敲的有靈感時,你會感覺很充實(shí)的记劝,那種成果最終顯現(xiàn)在自己面前变姨,心里是...
    lp79閱讀 188評論 0 1
  • 本文摘要:今天是《鄭淵潔的家庭教育課》的第十八課,我們?yōu)榇蠹依^續(xù)解讀厌丑。本文深度剖析了大眾教育中從眾的特性定欧,過渡到家...
    石頭聊家庭教育閱讀 1,437評論 11 12