前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LWC 62:744. Find Smallest Letter Greater Than Target

LWC 62:744. Find Smallest Letter Greater Than Target

作者头像
用户1147447
发布2018-01-02 10:45:56
5430
发布2018-01-02 10:45:56
举报
文章被收录于专栏:机器学习入门

LWC 62:744. Find Smallest Letter Greater Than Target

传送门:744. Find Smallest Letter Greater Than Target

Problem:

Given a list of sorted characters letters containing only lowercase letters, and given a target letter target, find the smallest element in the list that is larger than the given target. Letters also wrap around. For example, if the target is target = ‘z’ and letters = [‘a’, ‘b’], the answer is ‘a’.

Examples:

Input: letters = [“c”, “f”, “j”] target = “a” Output: “c” Input: letters = [“c”, “f”, “j”] target = “c” Output: “f” Input: letters = [“c”, “f”, “j”] target = “d” Output: “f” Input: letters = [“c”, “f”, “j”] target = “g” Output: “j” Input: letters = [“c”, “f”, “j”] target = “j” Output: “c” Input: letters = [“c”, “f”, “j”] target = “k” Output: “c”

Note:

letters has a length in range [2, 10000].

letters consists of lowercase letters, and contains at least 2 unique letters.

target is a lowercase letter.

思路: 因为wrap around,所以当给的target大于最大元素,就取集合中最小字符。否则,取大于target的第一个元素。(集合需要排序)

Java版:

代码语言:javascript
复制
    public char nextGreatestLetter(char[] letters, char target) {
        Set<Character> set = new HashSet<>();
        for (char c : letters) set.add(c);

        char[] let = new char[set.size()];
        int i = 0;
        for (char c : set) {
            let[i++] = c;
        }

        Arrays.sort(let);
        for (int j = 0; j < let.length; ++j) {
            if (let[j] > target) return let[j];
        }
        return let[0];
    }

实际上是不需要去重的,代码如下:

代码语言:javascript
复制
    public char nextGreatestLetter(char[] letters, char target) {
        Arrays.sort(letters);
        for (int j = 0; j < letters.length; ++j) {
            if (letters[j] > target) return letters[j];
        }
        return letters[0];
    }

因为集合有序,所以可以使用upper_bound的二分查找,代码如下:

代码语言:javascript
复制
    public char nextGreatestLetter(char[] letters, char target) {
        Arrays.sort(letters);
        return letters[upperBound(letters, target)];
    }

    public int upperBound(char[] letters, char target) {
        int l = 0;
        int r = letters.length - 1;
        while (l < r) {
            int m = l + (r - l) / 2;
            if (letters[m] <= target) {
                l = m + 1;
            }
            else{
                r = m;
            }
        }
        if (letters[r] > target) return r;
        else return 0;
    }

当然你也可以使用Java自家的二分查找接口,但可惜的是,官方接口不支持重复元素集合的准确查找(会出错),所以在使用之前需要对元素去重。

代码如下:

代码语言:javascript
复制
    public char nextGreatestLetter(char[] letters, char target) {
        Set<Character> set = new HashSet<>();
        for (char c : letters) set.add(c);

        char[] let = new char[set.size()];
        int i = 0;
        for (char c : set) {
            let[i++] = c;
        }

        Arrays.sort(let);
        int idx = Arrays.binarySearch(let, target);
        if (idx >= 0){
            if (idx + 1 >= let.length) return let[0];
            else return let[idx + 1];
        }
        else {
            idx = -idx - 1;
            if (idx == let.length) return let[0];
            else return let[idx];
        }
    }

Python版本:

代码语言:javascript
复制
    def nextGreatestLetter(self, letters, target):
        """
        :type letters: List[str]
        :type target: str
        :rtype: str
        """
        sorted(letters)
        for l in letters:
            if (target < l): return l
        return letters[0]

自带的库也是非常方便:

代码语言:javascript
复制
    def nextGreatestLetter(self, letters, target):
        """
        :type letters: List[str]
        :type target: str
        :rtype: str
        """
        pos = bisect.bisect_right(letters, target)
        return letters[0] if pos == len(letters) else letters[pos] 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-12-10 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • LWC 62:744. Find Smallest Letter Greater Than Target
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档