383. Ransom Note(map容器)
題目
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct(“a”, “b”) -> false
canConstruct(“aa”, “ab”) -> false
canConstruct(“aa”, “aab”) -> true
題意
判斷字串ransomNote中的元素是否是由magazine裡的元素構成的。
題解
C++程式碼
class Solution {
public:
bool canConstruct(string ransomNote, string magazine) {
map<char, int>m;
for(int i=0; i<magazine.length(); i++)
{
m[magazine[i]]++;
}
for(int i=0; i<ransomNote.length(); i++)
{
if(m[ransomNote[i]]==0)
return false;
m[ransomNote[i]]--;
}
return true;
}
};
python程式碼
class Solution(object):
def canConstruct(self, ransomNote, magazine):
"""
:type ransomNote: str
:type magazine: str
:rtype: bool
"""
ransomNote = list(ransomNote)
magazine = list(magazine)
for i in range(0, len(ransomNote)):
if ransomNote[i] in magazine:
magazine.remove(ransomNote[i])
else:
return False
return True
相關推薦
383. Ransom Note(map容器)
題目 Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will
leetcode-383-Ransom Note(以空間換時間)
ini from each 小寫字母 自己的 hat bmi 第一個字符 BE 題目描述: Given an arbitrary ransom note string and another string containing letters from all the m
LeetCode演算法題-Ransom Note(Java實現)
這是悅樂書的第212次更新,第225篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第80題(順位題號是383)。給定一個任意贖金票據字串和另一個包含所有雜誌字母的字串,如果贖金票據可以從雜誌中構建,則寫一個函式將返回true;否則,它將返回false。雜誌字串中的每個字母
leetcode第四題(383. Ransom Note)
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the rans
C++ map容器和multimap容器(STL map容器)
目錄 1. 關聯容器和map容器概述 2. map容器 2.1 map的建立以及初始化列表 2.2 map容器的一般常用屬性(方法) 2.3 插入資料 2.4 資料的訪問和遍歷 2.5 資料的刪除 2.6 map中關鍵詞的排序 3. multimap容器
直方圖統計(map容器按值排序)
#include <iostream> #include<opencv.hpp> #include <vector> #include<map> #include<algorithm> using na
LeetCode oj 383. Ransom Note(雜湊)
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return
383. Ransom Note (Easy)
count ngs blog cti 利用 con self ted lec Given an arbitrary ransom note string and another string containing letters from all the magazines
383. Ransom Note【easy】
begin logs char rar assume ron return code color 383. Ransom Note【easy】 Given an arbitrary ransom note string and another string contain
383. Ransom Note
term struct ngs 題目 let 字符串 lean fault truct Given an arbitrary ransom note string and another string containing letters from all the maga
POJ 2503 Babelfish(map入門)
pro getc scan appear std 出現 elf map stream 題目鏈接:http://poj.org/problem?id=2503 代碼: #include<cstdio> #include<string> #in
383. Ransom Note 在字典數組中查找筆記數組
字典 int ted 結構 each exit let 一句話 英文 [抄題]: Given an arbitrary ransom note string and another string containing letters from all the magazin
383 Ransom Note 贖金信
返回 意思 否則 c++ can ans 第一個 problem pub 給定一個贖金信 (ransom) 字符串和一個雜誌(magazine)字符串,判斷第一個字符串ransom能不能由第二個字符串magazines裏面的字符構成。如果可以構成,返回 true ;否則返回
Summarize to the Power of Two(map+思維)
fin bsp you -i 出現次數 出現 exists rst init A sequence a1,a2,…,ana1,a2,…,an is called good if, for each element aiai,
[LeetCode] 383. Ransom Note
cti letters note not ngs result 多個 etc ons Given an arbitrary ransom note string and another string containing letters from all the magaz
UVA11991 Easy Problem from Rujia Liu?(map+vector)
給你n個數(n<=10w,但是數值<=100w),現在要你回答m個查詢,對於每個查詢都是k和v,要求你回答原始資料中第k個v值出現的原始下標,如果不存在輸出0. #include<iostream> #include<cstdio> #include<c
Python學習筆記系列——高階函數(map/reduce)
from 類型 fun 轉換 浮點 color normal 整數 cto from functools import reduce #變量可以指向函數,函數的參數能接受變量,那麽一個函數就可以接受另一個函數作為參數,這種函數被稱之為高階函數 def add(x,y
Python學習筆記系列——高階函式(map/reduce)
一、map #變數可以指向函式,函式的引數能接受變數,那麼一個函式就可以接受另一個函式作為引數,這種函式被稱之為高階函式 def add(x,y,f): return f(x)+f(y) print(add(-5,-9,abs)) ''' map()函式接受兩個引數,一個是函式,
[LeetCode&Python] Problem 383. Ransom Note
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom no
51Nod1095 Anigram單詞(map+sort)
這道題思路很簡單,用map容器維護,把原有的字串放入一個map,然後排完序後的字串放入另一個map容器。 第一個map容器作用是判斷某字串是否出現過,第二個字串作用是記錄排序後字串出現的次數。 #include<iostream> #include<algorithm&