500. Keyboard Row(for...)

感觉还蛮尴尬的,学 cpp 也有一段时间了,居然不知道 for 还有这样的使用。
不说了,贴题目和代码。

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:
You may use one character in the keyboard more than once.
You may assume the input string will only contain letters of alphabet.
class Solution {
public:
    vector<string> findWords(vector<string>& words) {
        vector<string> outStr;
        string qwe = "qwertyuiopQWERTYUIOP";
        string asd = "asdfghjklASDFGHJKL";
        string zxc = "zxcvbnmZXCVBNM";

        for (auto &word:words)
        {
          bool f1 = true;
          bool f2 = true;
          bool f3 = true;
          for (auto &ch:word)
          {
              for (int i = 0; i < qwe.length(); i++)
              {
                  if (ch == qwe[i])
                  {
                     f1 = false;
                     break;
                  }
              }
              for (int i = 0; i < asd.length(); i++)
              {
                  if (ch == asd[i])
                  {
                     f2 = false;
                     break;
                  }
              }
              for (int i = 0; i < zxc.length(); i++)
              {
                  if (ch == zxc[i])
                  {
                     f3 = false;
                     break;
                  }
              }
          }
            if(f1 == true && f2 == true && f3 == false) outStr.push_back(word);
            if(f1 == false && f2 == true && f3 == true) outStr.push_back(word);
            if(f1 == true && f2 == false && f3 == true) outStr.push_back(word);
        }
        return outStr;
    }
};

写 cpp 到现在,还比较抵触容器,还是比较习惯 c 里面的数组的概念,但数组也很不好用。
今天才知道 for (auto &word:words) 还可以这样使用 for 的,那不是美滋滋吗。

记住!

<template> <view class="keyboard-container"> <!-- 省份键盘 --> <view v-if="showProvince" class="province-board"> <view v-for="(row, i) in provinceRows" :key="i" class="keyboard-row" > <view v-for="(char, j) in row" :key="j" class="keyboard-item" @click="handleInput(char)" > {{ char }} </view> </view> <view class="delete-btn" @click="handleDelete"> <uni-icons type="backspace" size="36" color="#666" /> </view> </view> <!-- 字母数字键盘 --> <view v-else class="main-board"> <view class="number-row"> <view v-for="(num, i) in numberKeys" :key="i" class="keyboard-item" @click="handleInput(num)" > {{ num }} </view> </view> <view class="letter-row"> <view v-for="(char, j) in letterKeys" :key="j" class="keyboard-item" @click="handleInput(char)" > {{ char }} </view> </view> <view class="special-row"> <view v-for="(special, k) in specialKeys" :key="k" class="keyboard-item" @click="handleInput(special)" > {{ special }} </view> <view class="delete-btn" @click="handleDelete"> <uni-icons type="backspace" size="36" color="#666" /> </view> </view> </view> </view> </template> <script> const PROVINCE_ROWS = [ '京津冀晋蒙辽吉黑', '沪苏浙皖闽赣鲁豫', '鄂湘粤桂琼渝川贵', '云藏陕甘青宁新' ] export default { props: { showProvince: Boolean, currentIndex: Number }, data() { return { provinceRows: PROVINCE_ROWS, numberKeys: '1234567890', letterKeys: 'ABCDEFGHJKLMNPQRSTUVWXYZ', specialKeys: ['港', '澳', '学', '警'] } }, methods: { handleInput(value) { this.$emit('input', value) }, handleDelete() { this.$emit('delete') } } } </script> <style lang="scss"> .keyboard-container { background: #f0f1f3; padding: 20rpx; border-radius: 20rpx 20rpx 0 0; box-shadow: 0 -4rpx 20rpx rgba(0,0,0,0.08); } .keyboard-row { display: flex; justify-content: center; gap: 12rpx; margin-bottom: 12rpx; } .keyboard-item { width: 72rpx; height: 96rpx; background: #fff; border-radius: 12rpx; display: flex; align-items: center; justify-content: center; font-size: 34rpx; font-weight: 500; box-shadow: 0 2rpx 4rpx rgba(0,0,0,0.1); &:active { background: #e0e0e0; } } .delete-btn { width: 120rpx; background: #d0d2d6; &:active { background: #c0c2c6; } } .special-row { .keyboard-item { width: 88rpx; } } </style> 运行这段代码
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值