莱文斯坦距离,又称 Levenshtein 距离,是编辑距离的一种。指两个字串之间,由一个转成另一个所需的最少编辑操作次数。 允许的编辑操作包括: 插入一个字符 删除一个字符 将一个字符替换成另一个字符 需要你编写一个程序,实现以下功能: 给定一个字符串集合 S 以及一个模板串 P,从 S 中找出与 P 莱文斯坦距离最小的字符串 T,输出 T 以及其对应的编辑距离 D。如果 S 中出现多个满足条件的字符串,则取按字典序排列的第一个。
classCashier { unordered_map<int, int> m; int d; int nn; int index = 0; public: Cashier(int n, int discount, vector<int>& products, vector<int>& prices) { d = discount; nn = n; for (int i = 0; i < products.size(); ++i) { m[products[i]] = prices[i]; } index = 0; } doublegetBill(vector<int> product, vector<int> amount){ ++index; bool discount = false; if (index == nn) { index = 0; discount = true; } double ans = 0; for (int i = 0; i < product.size(); ++i) { ans += amount[i] * m[product[i]]; } if (discount) ans *= 1 - d / 100.0; return ans; } };
/** * Your Cashier object will be instantiated and called as such: * Cashier* obj = new Cashier(n, discount, products, prices); * double param_1 = obj->getBill(product,amount); */
classSolution { public: intcountNegatives(vector<vector<int>>& grid){ int ans = 0; constint m = grid.size(); constint n = grid[0].size(); int row = 0, col = n - 1; while (row < m && col >= 0) { if (grid[row][col] < 0) { ans += m - row; --col; } else { ++row; } } return ans; } };