日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

【leetcode】500. Keyboard Row

發(fā)布時(shí)間:2025/6/17 编程问答 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【leetcode】500. Keyboard Row 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

問(wèn)題描述:

Given a List of words, return the words that can be typed using letters of?alphabet?on only one row's of American keyboard like the image below.

?

?

Example 1:

Input: ["Hello", "Alaska", "Dad", "Peace"] Output: ["Alaska", "Dad"]

?

Note:

  • You may use one character in the keyboard more than once.
  • You may assume the input string will only contain letters of alphabet.
  • 解法一:

    常規(guī)解法,用unordered_set存儲(chǔ)每一行的字母,依次尋找判斷。

    1 class Solution { 2 public: 3 vector<string> findWords(vector<string>& words) { 4 unordered_set<char> row1 {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'}; 5 unordered_set<char> row2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}; 6 unordered_set<char> row3 { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'}; 7 vector<unordered_set<char>> rows {row1, row2, row3}; 8 9 10 vector<string> validWords; 11 for(int i=0; i<words.size(); ++i){ 12 int row=0; 13 14 for(int k=0; k<3; ++k){ 15 if(rows[k].count((char)tolower(words[i][0])) > 0) row = k; 16 } 17 18 validWords.push_back(words[i]); 19 for(int j=1; j<words[i].size(); ++j){ 20 if(rows[row].count((char)tolower(words[i][j])) == 0){ 21 validWords.pop_back(); 22 break; 23 } 24 } 25 26 } 27 return validWords; 28 } 29 };

    解法二:

    這種解法比較有啟發(fā)性,看起來(lái)很有意思。

    1 class Solution { 2 public: 4 vector<string> findWords(vector<string>& words) 5 { 6 vector<string> res; 7 8 for(auto str : words) 9 { 10 bool r1 = str.find_first_of("QWERTYUIOPqwertyuiop") == string::npos ? false : true; 11 bool r2 = str.find_first_of("ASDFGHJKLasdfghjkl") == string::npos ? false : true; 12 bool r3 = str.find_first_of("ZXCVBNMzxcvbnm") == string::npos ? false : true; 13 14 if(r1 + r2 + r3 == 1) 15 res.push_back(str); 16 } 17 18 return res; 19 } 20 21 };

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/SarahLiu/p/6702364.html

    總結(jié)

    以上是生活随笔為你收集整理的【leetcode】500. Keyboard Row的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。