- Nem tiltották be a Teslát Kaliforniában, Robotaxival ünnepelt a márka
- Apró változásokkal, elődjénél jobb áron kezd a Google Pixel 10a
- Visszatért a Snapdragonhoz az Infinix, itt a Note 60 és Note 60 Pro
- Esélyes, hogy drágul a Nothing Phone (4a) széria
- Lelkiismeret-furdalás nélkül zabálhatod a süteményt a Galaxy S26-tal
- Privát Samsung funkciót tehet az Apple publikussá
- Visszatért a Snapdragonhoz az Infinix, itt a Note 60 és Note 60 Pro
- Újabb részletek a Galxy S26 Ultra privát képernyőmódjára vonatkozóan
- iPhone topik
- Apró változásokkal, elődjénél jobb áron kezd a Google Pixel 10a
- Telekom mobilszolgáltatások
- Garmin Instinct – küldetés teljesítve
- Nemzetközi vizekre evezett a Realme GT 7 és GT 7T
- Mobil flották
- Samsung Galaxy S23 Ultra - non plus ultra
Ú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);}elsethrow 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 stateif (isFinished)returnfalse;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 itint 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 elementint 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!
- Apple iPhone 16 Pro 128Gb, kártya független, sivatag színben
- Kingston HyperX Fury 2x16GB 3200MHz DDR4 kit / Beszámítás OK!
- Check-Point-V-82 - Security Gateway Switch
- Check-Point-V80 - Security Gateway Switch- több db
- IBM-Tape-Library-(3583-L36),-4x-IBM-LTO2-Ultrium-FC-Tape-Drive-(18P8159)-without-Tapes
- Bomba ár! HP ProBook 450 G8 - i5-1135G7 I 8GB I 256SSD I HDMI I 15,6" FHD I Cam I W11 I Gar
- Apple iPhone 12 Mini / 256GB / Kártyafüggetlen / 12Hó Garancia / Akku: 89%
- 3DKRAFT.HU - 3D NYOMTATÁS - AZONNALI ÁRAJÁNLAT - GYORS KIVITELEZÉS - 505+ POZITÍV ÉRTÉKELÉS
- Honor Magic7 Lite 512GB, Kártyafüggetlen, 1 Év Garanciával
- HIBÁTLAN iPhone 15 Plus 128GB Blue-1 ÉV GARANCIA - Kártyafüggetlen, MS4531,90% Akksi
Állásajánlatok
Cég: Laptopműhely Bt.
Város: Budapest
Cég: PCMENTOR SZERVIZ KFT.
Város: Budapest



