LC821.字符的最短距离

原题链接
image.png

思路1:两次遍历

  • 这是非常自然的思路
  • 从左往右扫描,记录每一个数字到左侧最近目标数字的距离。
  • 从右往左扫描,记录每一个数字到右侧最近目标数字的距离,同时和左边最近距离比较,取近的。

    代码1:

    1. class Solution {
    2. public:
    3. vector<int> shortestToChar(string s, char c) {
    4. int ns = s.size();
    5. vector<int> ans(ns, 0);
    6. for (int i = 0; i < ns; ) {
    7. int cnt = 0;
    8. if (s[i] == c) {
    9. i += 1;
    10. while (i < ns && s[i] != c) {
    11. cnt += 1;
    12. ans[i] = cnt;
    13. i += 1;
    14. }
    15. } else {
    16. i += 1;
    17. }
    18. }
    19. for (int i = ns - 1; i >= 0; ) {
    20. int cnt = 0;
    21. if (s[i] == c) {
    22. i -= 1;
    23. while (i >= 0 && s[i] != c) {
    24. cnt += 1;
    25. if (ans[i] == 0) {
    26. ans[i] = cnt;
    27. } else {
    28. ans[i] = min(ans[i], cnt);
    29. }
    30. i -= 1;
    31. }
    32. } else {
    33. i -= 1;
    34. }
    35. }
    36. return ans;
    37. }
    38. };

    思路2:BFS

  • vector<int> ans记录各个位置到目标位置的最近距离

  • 所有目标字母的位置,全部进入队列(多个root)
  • BFS遍历,将当前节点所记录位置cur_pos的左右两个位置next_pos分别进行检验,符合条件就入队。
  • 在入队的时候修改ans[next_pos]ans[next_pos] = ans[cur_pos] + 1
  • 如何避免重复?可以提前把ans中所有位置全部置-1,然后在入队的时候,需满足两个条件

    • next_pos不能越界
    • ans[next_pos] == -1

      代码2:

      class Solution {
      public:
      vector<int> shortestToChar(string s, char c) {
         queue<int> q;
         int ns = s.size();
         vector<int> ans(ns, -1);
      
         // push root
         for (int i = 0; i < ns; i++) {
             if (s[i] == c) {
                 q.push(i);
                 ans[i] = 0;
             }
         }
      
         // bfs
         while (!q.empty()) {
             int cur_pos = q.front();
             q.pop();
      
             for (int i = 0; i < 2; i++) {
                 int next_pos = 0;
                 if (i == 0) {
                     next_pos = cur_pos + 1;
                 } else {
                     next_pos = cur_pos - 1;
                 }
      
                 if (0 <= next_pos && next_pos < ns && ans[next_pos] == -1) {
                     ans[next_pos] = ans[cur_pos] + 1;
                     q.push(next_pos);
                 }
             }
         }
      
         return ans;
      }
      };class Solution {
      public:
      vector<int> shortestToChar(string s, char c) {
         queue<int> q;
         int ns = s.size();
         vector<int> ans(ns, -1);
      
         // push root
         for (int i = 0; i < ns; i++) {
             if (s[i] == c) {
                 q.push(i);
                 ans[i] = 0;
             }
         }
      
         // bfs
         while (!q.empty()) {
             int cur_pos = q.front();
             q.pop();
      
             for (int i = 0; i < 2; i++) {
                 int next_pos = 0;
                 if (i == 0) {
                     next_pos = cur_pos + 1;
                 } else {
                     next_pos = cur_pos - 1;
                 }
      
                 if (0 <= next_pos && next_pos < ns && ans[next_pos] == -1) {
                     ans[next_pos] = ans[cur_pos] + 1;
                     q.push(next_pos);
                 }
             }
         }
      
         return ans;
      }
      };