查找范围内所有数字递增递减的数字
给定整数L和R ,找到L到R范围内的所有数字,其数字交替递增 - 递减,即当前数字中的数字是否为 d1, d2, d3, d4, d5 。 . .然后d1 < d2 > d3 < d4。 . .必须成立。
例子:
Input: L = 60, R = 100
Output: 67 68 69 78 79 89
Explanation: These numbers follow the increasing decreasing manner of digits
Input: L = 4, R = 12
Output: 4 5 6 7 8 9 12
方法:遍历 L 到 R 范围内的所有数字,并找到具有给定数字模式的数字。请按照以下步骤操作:
- 遍历数字中的每个数字
- 检查前面两个索引上的字符是否从当前字符增加,
- 否则检查前面两个索引上的字符是否从当前字符减少
- 如果这两种情况都是错误的,则中断并检查下一个数字
- 如果所有情况都为真,则打印数字
下面是上述方法的实现。
C++
// C++ code to implement the above approach
#include
using namespace std;
// Utility function to check if
// the digits of the current
// integer forms a wave pattern
bool check(int N)
{
// Convert the number to a string
string S = to_string(N);
// Loop to iterate over digits
for (int i = 0; i < S.size(); i++) {
if (i == 0) {
// Next character of
// the number
int next = i + 1;
// Current character is
// not a local minimum
if (next < S.size()) {
if (S[i] >= S[next]) {
return false;
}
}
}
else if (i == S.size() - 1) {
// Previous character of
// the number
int prev = i - 1;
if (prev >= 0) {
// Character is a
// local maximum
if (i & 1) {
// Character is not
// a local maximum
if (S[i] <= S[prev]) {
return false;
}
}
else {
// Character is a
// local minimum
if (S[i] >= S[prev]) {
return false;
}
}
}
}
else {
int prev = i - 1;
int next = i + 1;
if (i & 1) {
// Character is a
// local maximum
if ((S[i] > S[prev]) &&
(S[i] > S[next])) {
}
else {
return false;
}
}
else {
// Character is a
// local minimum
if ((S[i] < S[prev]) &&
(S[i] < S[next])) {
}
else {
return false;
}
}
}
}
return true;
}
// Function to find the numbers
void findNumbers(int L, int R)
{
for (int i = L; i <= R; i++)
if (check(i))
cout << i << " ";
}
// Driver Code
int main()
{
int L = 60, R = 100;
findNumbers(L, R);
return 0;
}
Java
// Java code to implement the above approach
class GFG
{
// Utility function to check if
// the digits of the current
// integer forms a wave pattern
static boolean check(int N)
{
// Convert the number to a string
String S = Integer.toString(N);
// Loop to iterate over digits
for (int i = 0; i < S.length(); i++) {
if (i == 0) {
// Next character of
// the number
int next = i + 1;
// Current character is
// not a local minimum
if (next < S.length()) {
if (S.charAt(i) >= S.charAt(next)) {
return false;
}
}
}
else if (i == S.length() - 1) {
// Previous character of
// the number
int prev = i - 1;
if (prev >= 0) {
// Character is a
// local maximum
if ((i & 1) > 0) {
// Character is not
// a local maximum
if (S.charAt(i) <= S.charAt(prev)) {
return false;
}
} else {
// Character is a
// local minimum
if (S.charAt(i) >= S.charAt(prev)) {
return false;
}
}
}
} else {
int prev = i - 1;
int next = i + 1;
if ((i & 1) > 0) {
// Character is a
// local maximum
if ((S.charAt(i) > S.charAt(prev)) &&
(S.charAt(i) > S.charAt(next))) {
} else {
return false;
}
} else {
// Character is a
// local minimum
if ((S.charAt(i) < S.charAt(prev)) &&
(S.charAt(i) < S.charAt(next))) {
} else {
return false;
}
}
}
}
return true;
}
// Function to find the numbers
static void findNumbers(int L, int R) {
for (int i = L; i <= R; i++)
if (check(i))
System.out.print(i + " ");
}
// Driver Code
public static void main(String args[]) {
int L = 60, R = 100;
findNumbers(L, R);
}
}
// This code is contributed by gfgking.
Python3
# Python code for the above approach
# Utility function to check if
# the digits of the current
# integer forms a wave pattern
def check(N):
# Convert the number to a string
S = str(N);
# Loop to iterate over digits
for i in range(len(S)):
if (i == 0):
# Next character of
# the number
next = i + 1;
# Current character is
# not a local minimum
if (next < len(S)):
if (ord(S[i]) >= ord(S[next])):
return False;
elif (i == len(S) - 1):
# Previous character of
# the number
prev = i - 1;
if (prev >= 0):
# Character is a
# local maximum
if (i & 1):
# Character is not
# a local maximum
if (ord(S[i]) <= ord(S[prev])):
return False;
else:
# Character is a
# local minimum
if (ord(S[i]) >= ord(S[prev])):
return False;
else:
prev = i - 1;
next = i + 1;
if (i & 1):
# Character is a
# local maximum
if (ord(S[i]) > ord(S[prev])) and (ord(S[i]) > ord(S[next])):
print("", end="")
else:
return False;
else:
# Character is a
# local minimum
if (ord(S[i]) < ord(S[prev])) and (ord(S[i]) < ord(S[next])):
print("", end="")
else:
return False;
return True;
# Function to find the numbers
def findNumbers(L, R):
for i in range(L, R + 1):
if (check(i)):
print(i, end= " ")
# Driver Code
L = 60
R = 100;
findNumbers(L, R);
# This code is contributed by Saurabh Jaiswal
C#
// C# code to implement the above approach
using System;
class GFG
{
// Utility function to check if
// the digits of the current
// integer forms a wave pattern
static bool check(int N)
{
// Convert the number to a string
string S = N.ToString();
// Loop to iterate over digits
for (int i = 0; i < S.Length; i++) {
if (i == 0) {
// Next character of
// the number
int next = i + 1;
// Current character is
// not a local minimum
if (next < S.Length) {
if (S[i] >= S[next]) {
return false;
}
}
}
else if (i == S.Length - 1) {
// Previous character of
// the number
int prev = i - 1;
if (prev >= 0) {
// Character is a
// local maximum
if ((i & 1) > 0) {
// Character is not
// a local maximum
if (S[i] <= S[prev]) {
return false;
}
}
else {
// Character is a
// local minimum
if (S[i] >= S[prev]) {
return false;
}
}
}
}
else {
int prev = i - 1;
int next = i + 1;
if ((i & 1) > 0) {
// Character is a
// local maximum
if ((S[i] > S[prev]) &&
(S[i] > S[next])) {
}
else {
return false;
}
}
else {
// Character is a
// local minimum
if ((S[i] < S[prev]) &&
(S[i] < S[next])) {
}
else {
return false;
}
}
}
}
return true;
}
// Function to find the numbers
static void findNumbers(int L, int R)
{
for (int i = L; i <= R; i++)
if (check(i))
Console.Write(i + " ");
}
// Driver Code
public static void Main()
{
int L = 60, R = 100;
findNumbers(L, R);
}
}
// This code is contributed by Samim Hossain Monda
Javascript
输出
67 68 69 78 79 89
时间复杂度: O((RL) * D) 其中 D 是 R 中的位数
辅助空间: O(D)