744. Find Smallest Letter Greater Than Target

Description:

Given a characters array letters that is sorted in non-decreasing order and a character target, return the smallest character in the array that is larger than target.

Note that the letters wrap around.

For example, if target == ‘z’ and letters == [‘a’, ‘b’], the answer is ‘a’.

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/find-smallest-letter-greater-than-target

Solution_1

traverse all elements in array and find the smallest character in non-decreasing order sorted arrray

The results of my codes:

The smallest character in the array that is larger than d is f

The corresponding codes:

#include <iostream>
#include <vector>
#include <iomanip>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::max;
using std::vector;
using std::setw;
char nextGreatestLetter(vector<char>& letters, char target);
int main()
{
	vector<char> letters{'c','f','j'};
	char target = 'd';
	char result = nextGreatestLetter(letters, target);
	cout << "The smallest character in the array that is larger than " << target << " is " << result << endl;
	return 0;
}

char nextGreatestLetter(vector<char>& letters, char target)
{
	for (char letter : letters){
		if (letter > target) return letter;
	}
	return letters[0];
}

Solution_2

==Just use binary search method to find the ==

The codes:

#include <iostream>
#include <vector>
#include <iomanip>
#include <bits/stdc++.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::max;
using std::vector;
using std::setw;
//see whether num is self dividing or not
char nextGreatestLetter(vector<char>& letters, char target);
int main()
{
	vector<char> letters{'c','f','j'};
	char target = 'd';
	char result = nextGreatestLetter(letters, target);
	cout << "The smallest character in the array that is larger than " << target << " is " << result << endl;
	return 0;
}

char nextGreatestLetter(vector<char>& letters, char target)
{
	return target < letters.back() ? *upper_bound(letters.begin(), letters.end() - 1, target) : letters[0];
}

If you want to find know the meaning of upper_bound() or basic utility ways, you could click it.

Solution_3

The realization of this method is through binary search to find the smallest bigger one!

The corresponding codes just like following:

#include <iostream>
#include <vector>
#include <iomanip>
#include <bits/stdc++.h>

using std::cout;
using std::cin;
using std::endl;
using std::string;
using std::max;
using std::vector;
using std::setw;
//see whether num is self dividing or not
char nextGreatestLetter(vector<char>& letters, char target);
int main()
{
	vector<char> letters{'c','f','j'};
	char target = 'l';
	char result = nextGreatestLetter(letters, target);
	cout << "The smallest character in the array that is larger than " << target << " is " << result << endl;
	return 0;
}

char nextGreatestLetter(vector<char>& letters, char target)
{
	int len = letters.size();
	int left{0}, right = len - 1, middle{0};
	while (left <= right){
		middle = (left + right)/2;
		if (letters[middle] > target){
			right = middle - 1;
		} else if( letters[middle] < target){
			left = middle + 1;
		}
	}
	return letters[left % len];
}

The results is descripbed as follows:

The smallest character in the array that is larger than l is c

Anyway, hope you love my blog and appreciate its useful for your coding career.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值