LeetCode 821. Shortest Distance to a Character

Given a string s and a character c that occurs in s, return an array of integers answer where answer.length == s.length and answer[i] is the distance from index i to the closest occurrence of character c in s.

The distance between two indices i and j is abs(i - j), where abs is the absolute value function.

Example 1:

Input: s = "loveleetcode", c = "e"
Output: [3,2,1,0,1,0,0,1,2,2,1,0]
Explanation: The character 'e' appears at indices 3, 5, 6, and 11 (0-indexed).
The closest occurrence of 'e' for index 0 is at index 3, so the distance is abs(0 - 3) = 3.
The closest occurrence of 'e' for index 1 is at index 3, so the distance is abs(1 - 3) = 2.
For index 4, there is a tie between the 'e' at index 3 and the 'e' at index 5, but the distance is still the same: abs(4 - 3) == abs(4 - 5) = 1.
The closest occurrence of 'e' for index 8 is at index 6, so the distance is abs(8 - 6) = 2.

Example 2:

Input: s = "aaab", c = "b"
Output: [3,2,1,0]

Constraints:

  • 1 <= s.length <= 104
  • s[i] and c are lowercase English letters.
  • It is guaranteed that c occurs at least once in s.

这题给了一个string和一个char,要求这个string里每个字符到离它最近的那个指定char的距离。

这题自己认真想了一下写了一下,思路大概就是扫一遍string,每次找到一个char就把它前面的那些字符的距离update一下。那么前面那些字符的距离应该设为多少呢,对于出现第一个char之前,那就是无穷大了,对于剩下的,就是它到之前记录下的char的距离。于是代码写起来也很简单。但是刚开始提交的时候踩了个坑,就是在记录第一个char出现之前的距离用了index == 0,结果没有考虑到可能第一个字符就是char呢,于是就错了一次,改对以后就过了。

我这个做法乍一看像是one pass,但是每次找到一个char的时候都会往前再遍历一遍,所以也相当于是two pass了。

class Solution {
    public int[] shortestToChar(String s, char c) {
        int[] array = new int[s.length()];
        int index = 0;
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == c) {
                for (int j = index; j <= i; j++) {
                    array[j] = Math.min(array[j], Math.abs(i - j));
                }
                index = i;
            } else {
                if (index == 0 && s.charAt(0) != c) {
                    array[i] = Integer.MAX_VALUE;
                } else {
                    array[i] = Math.abs(i - index);
                }
            }
        }
        return array;
    }
}

然后看了大家的解法,嗯,很妙,但是想法都大差不差吧,有点dp那味道了。

主要的思想就是从左边开始算一次,从右边开始算一次,取两次的最小值。其实跟我自己的想法很像,但是感觉这种思路更巧妙吧。

1. 先把dist初始化成array的长度作为一个最大的可能的距离(这里不能用Integer.MAX_VALUE因为需要对这个距离进行+1,溢出了就无了)。for循环里每一次判断当前字符是否等于char,如果等于那就dist = 0,如果不等于那就是dist + 1,然后赋值给array,相当于直接给char右边的所有字符都初始化了它到左边的值。然后第二个for里也是一样的计算dist,最后放入array的时候就判断是现在的dist小还是之前从左边计算的dist小。

class Solution {
    public int[] shortestToChar(String s, char c) {
        int[] array = new int[s.length()];
        int dist = s.length();
        for (int i = 0; i < s.length(); i++) {
            dist = s.charAt(i) == c ? 0 : dist + 1;
            array[i] = dist;
        }
        dist = s.length();
        for (int i = s.length() - 1; i >= 0; i--) {
            dist = s.charAt(i) == c ? 0 : dist + 1;
            array[i] = Math.min(array[i], dist);
        }
        return array;
    }
}

这种写法就是不那么直观,微微有一些些绕。

另一个解法更直观。第一次先initialize所有的,不是char就记max,是char就记0。第二次从左到右扫,如果它的值不是max的话就说明右边可以开始记录了,就开始update右边的值们,update的规则是dist[i + 1] = Math.min(dist[i + 1], dist[i] + 1)。第三次从右往左扫,因为最右边的值一定有记录而不是max,所以可以直接update,update的规则是dist[i - 1] = Math.min(dist[i  - 1], dist[i] + 1)。诶,写完还是觉得有点绕……还是自己的写法自己最舒服……

class Solution {
    public int[] shortestToChar(String s, char c) {
        int[] array = new int[s.length()];
        int dist = s.length();
        for (int i = 0; i < s.length(); i++) {
            array[i] = s.charAt(i) == c ? 0 : Integer.MAX_VALUE;
        }
        // update right part of the first occurance
        for (int i = 0; i < s.length() - 1; i++) {
            if (array[i] != Integer.MAX_VALUE) {
                array[i + 1] = Math.min(array[i + 1], array[i] + 1);
            }
        }
        for (int i = s.length() - 1; i > 0; i--) {
            array[i - 1] = Math.min(array[i - 1], array[i] + 1);
        }
        return array;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值