Hirdetés
- Honor 400 Pro - Gép a képben
- Kézbe fogható paradoxon lett az iPhone Air
- iPhone topik
- Telekom mobilszolgáltatások
- Merész dizájn és új teleobjektív az iPhone 17 Pro mobilokban
- Milyen okostelefont vegyek?
- Apple Watch
- Apple Watch Sport - ez is csak egy okosóra
- Xiaomi 14T Pro - teljes a család?
- Youtube Android alkalmazás alternatívák reklámszűréssel / videók letöltése
Új hozzászólás Aktív témák
-
joysefke
veterán
válasz
pmonitor #9466 üzenetére
Átírva C#-ba és használható formában
A kezdeti 1x tömb-klónozáson kívül (hogy ne legyen elrontva a bemeneti tömb) teljesen allokációmentes.
A char[] State tárolja az aktuális állapotot, a bool Next() pedig lépteti az és visszajelez a sikerről. Ha a char[] State állapotot nem csak olvasni akarod akkor értelemszerűen ki kell menteni róla egy másolatot.A char[] State köré lehetne még valami readonly wrappert rakni, de azt nem tudom hogy viselkedne.
char[] megy bele ctor bemeneti paraméterként
És ezen még lehetne gyorsítani.
using System;
namespace Permutator
{
class Program
{
static void Main(string[] args)
{
int i = 0;
var p = new Permutator("abcdananana".ToCharArray());
do
{
i++;
Console.WriteLine(new string(p.State));
}
while (p.Next());
Console.WriteLine($"Nr of results: {i}");
}
}
public class Permutator
{
public char[] State { get; }
int _size;
bool isFinished = false;
public Permutator(char[] symbols)
{
if (symbols?.Length > 0)
{
_size = symbols.Length;
State = (char[])symbols.Clone();
Array.Sort(State);
}
else
throw new ArgumentException("input must be non-empty");
}
public bool Next()
{
// if we have already finished we indicate failure on getting next element
// else we try to advance the state and propagate success of advancing state
if (isFinished)
return
false;
isFinished = !AdvanceState();
return !isFinished;
}
bool AdvanceState()
{
// Find the rightmost character
// which is smaller than its next
// character. Let us call it 'first
// char'
int i;
for (i = _size - 2; i >= 0; --i)
if (State[i] < State[i + 1])
break;
// If there is no such character, all
// are sorted in decreasing order,
// means we just printed the last
// permutation and we are done.
if (i == -1)
return false;
// Find the ceil of 'first char'
// in right of first character.
// Ceil of a character is the
// smallest character greater
// than it
int ceilIndex = findCeil(State, State[i], i + 1, _size - 1);
char tmp = State[i];
State[i] = State[ceilIndex];
State[ceilIndex] = tmp;
Array.Sort(State, i + 1, _size - i - 1);
return true;
}
// This function finds the index of the
// smallest character which is greater
// than 'first' and is present in str[l..h]
int findCeil(char[] str, char first, int l, int h)
{
// initialize index of ceiling element
int ceilIndex = l;
// Now iterate through rest of the
// elements and find the smallest
// character greater than 'first'
for (int i = l + 1; i <= h; i++)
if (str[i] > first && str[i] < str[ceilIndex])
ceilIndex = i;
return ceilIndex;
}
}
}
Új hozzászólás Aktív témák
● ha kódot szúrsz be, használd a PROGRAMKÓD formázási funkciót!
- Kerékpárosok, bringások ide!
- sellerbuyer: Milyen laptopot vegyek? Segítek: semmilyet!
- Milyen asztali (teljes vagy fél-) gépet vegyek?
- BestBuy ruhás topik
- Luck Dragon: Asszociációs játék. :)
- Bambu Lab 3D nyomtatók
- Xbox Series X|S
- Honor 400 Pro - Gép a képben
- Amazon Kindle
- Azonnali alaplapos kérdések órája
- További aktív témák...
- Hp USB-C/Thunderbolt 3 dokkolók: USB-C Universal, G2, G4, G5, Hp Elite/Zbook- Thunderbolt 4 G4
- AKCIÓ! Apple Macbook Pro 16" 2019 i9 9980HK 64GB 500GB Radeon Pro 5500M notebook garanciával
- GYÖNYÖRŰ iPhone 11 Pro Max 64GB Midnight Green -1 ÉV GARANCIA -Kártyafüggetlen, MS3253,100% Akkumulá
- Apple iPhone 15 Pro Max / 256GB / Kártyafüggetlen / 12 Hó Garancia / 98% akku
- Bomba ár! Lenovo ThinkPad L13 G1 - i5-10GEN I 16GB I 512SSD I 13,3" FHD I HDMI I Cam I W11 I Gari!
Állásajánlatok
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest
Cég: CAMERA-PRO Hungary Kft.
Város: Budapest