天池 在线编程 LR String
生活随笔
收集整理的這篇文章主要介紹了
天池 在线编程 LR String
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 1. 題目
- 2. 解題
1. 題目
來源:https://tianchi.aliyun.com/oj/164423301311799378/184808348725744276
2. 解題
class Solution { public:/*** @param s: a string* @param t: a string* @param n: max times to swap a 'l' and a 'r'.* @return: return if s can transform to t.*/bool LRString(string &s, string &t, int n) {// write your code here.if(s.size() != t.size())return false;//長度不等不行int ls = 0, rs = 0, lt = 0, rt = 0;vector<int> sLidx, tLidx;for(int i = 0; i < s.size(); i++){if(s[i] == 'l'){ls++;sLidx.push_back(i);}elsers++;}for(int i = 0; i < t.size(); i++){if(t[i] == 'l'){lt++;tLidx.push_back(i);}elsert++;}if(ls != lt || rs != rt)return false;// lr字符數量不等不行int sw = 0;for(int i = 0; i < sLidx.size(); i++){if(sLidx[i] == tLidx[i])continue;else if(sLidx[i] < tLidx[i])return false;// l 的位置只能往左,不可能做到elsesw += sLidx[i] - tLidx[i];// 要移動的距離}return sw <= n;} };50ms C++
我的CSDN博客地址 https://michael.blog.csdn.net/
長按或掃碼關注我的公眾號(Michael阿明),一起加油、一起學習進步!
總結
以上是生活随笔為你收集整理的天池 在线编程 LR String的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 天池 在线编程 所有子数组之和(排列组合
- 下一篇: 天池 在线编程 聪明的销售(计数+贪心)