500. Keyboard Row

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.


American keyboard


Example 1:

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

Note:

  1. You may use one character in the keyboard more than once.
  2. You may assume the input string will only contain letters of alphabet.

我的答案:

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        set<char> st1 {'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'};
        set<char> st2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'};
        set<char> st3 {'z', 'x', 'c', 'v', 'b', 'n', 'm'};
    /*    st1.insert('q');
        st1.insert('w');
        st1.insert('e');
        st1.insert('r');
        st1.insert('t');
        st1.insert('y');
        st1.insert('u');
        st1.insert('i');
        st1.insert('o');
        st1.insert('p');
        st2.insert('a');
        st2.insert('s');
        st2.insert('d');
        st2.insert('f');
        st2.insert('g');
        st2.insert('h');
        st2.insert('j');
        st2.insert('k');
        st2.insert('l');
        st3.insert('z');
        st3.insert('x');
        st3.insert('c');
        st3.insert('v');
        st3.insert('b');
        st3.insert('n');
        st3.insert('m');*/
        vector<string> rs;
        for(int i = 0; i < words.size(); ++i){
            int status = 0;
            bool flag = true;
            for(int j = 0; j < words[i].length(); ++j){
                if(st1.find(words[i][j]) != st1.end()){
                    if(status == 0 || status == 1){
                        status = 1;
                    }else{
                        flag = false;
                        break;
                    }
                }
                if(st2.find(words[i][j]) != st2.end()){
                    if(status == 0 || status == 2){
                        status = 2;
                    }else{
                        flag = false;
                        break;
                    }
                }
                if(st3.find(words[i][j]) != st3.end()){
                    if(status == 0 || status == 3){
                        status = 3;
                    }else{
                        flag = false;
                        break;
                    }
                }
            }
            if(flag){
               rs.push_back(words[i]); 
            }
        }
        return rs;
    }
};

leetcode上简洁一点:

class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        unordered_set<char> row1 {'q', 'w', 'e', 'r', 't', 'y','u', 'i', 'o', 'p'};
        unordered_set<char> row2 {'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'}; 
        unordered_set<char> row3 { 'z', 'x', 'c', 'v', 'b' ,'n', 'm'};
        vector<unordered_set<char>> rows {row1, row2, row3};
        
        
        vector<string> validWords;
        for(int i=0; i<words.size(); ++i){
            int row=0;
            
            for(int k=0; k<3; ++k){
                if(rows[k].count((char)tolower(words[i][0])) > 0) row = k;
            }
            
            validWords.push_back(words[i]);
            for(int j=1; j<words[i].size(); ++j){
                if(rows[row].count((char)tolower(words[i][j])) == 0){
                    validWords.pop_back();
                    break;
                }
            }
            
        }
        return validWords;
    }
};
运行时间上,初始化的时候给set赋值比挨个insert要快不少(待研究)

检测鼠标事件 def mouse_event(self, event, x, y, flags, param): if event == cv2.EVENT_LBUTTONUP and x > 550 and y < 50: def open_login_window(my_window, on_entry_click): loginwindow = LoginWindow(on_entry_click) loginwindow.transient(my_window) loginwindow.wait_visibility() loginwindow.grab_set() def quit_window(my_window): # self.camera_process.terminate() my_window.destroy() # 虚拟键盘 def on_entry_click(self, event, entry): if self.keyboard_window: self.keyboard_window.destroy() keyboard_window = tk.Toplevel(self) keyboard_window.title("虚拟键盘") keyboard_window.geometry("610x140") keyboard_window.resizable(False, False) button_list = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '<-', 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'z', 'x', 'c', 'v', 'b', 'n', 'm'] row = 0 col = 0 for button_text in button_list: button = tk.Button(keyboard_window, text=button_text, width=3) if button_text != '<-': button.config(command=lambda char=button_text: entry.insert(tk.END, char)) else: button.config( command=lambda char=button_text: entry.delete(len(entry.get()) - 1, tk.END)) button.grid(row=row, column=col) col += 1 if col > 10: row += 1 col = 0 keyboard_window.deiconify() self.keyboard_window = keyboard_window # 登录界面 my_window = tk.Tk() my_window.title("登录") my_window.geometry("300x200") # 计算窗口位置,让其出现在屏幕中间 screen_width = my_window.winfo_screenwidth() screen_height = my_window.winfo_screenheight() x = (screen_width - 300) // 2 y = (screen_height - 200) // 2 my_window.geometry("+{}+{}".format(x, y)) my_window.wm_attributes("-topmost", True) login_button = tk.Button(my_window, text="登录", font=('Arial', 12), width=10, height=1, command=lambda: open_login_window(my_window, on_entry_click)) login_button.pack(side='left', expand=True) exitbutton = tk.Button(my_window, text="退出", font=('Arial', 12), width=10, height=1, command=lambda: [quit_window(my_window)]) exitbutton.pack(side='left', expand=True) my_window.mainloop() if event == cv2.EVENT_LBUTTONUP and x < 50 and y > 1000: cv2.destroyAllWindows() 在此基础上请实现让tk界面不会出现重影 用中文回答
最新发布
06-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值