当前位置:网站首页>2022/07/25 ------ arrangement of strings
2022/07/25 ------ arrangement of strings
2022-07-26 10:25:00 【City Pig】
List of articles
Title Description
Their thinking
I looked at the solution of other big guys , There are three main methods : The idea of backtracking recursion ; The idea of complete arrangement of dictionary order ; Stack based thinking . At present, I only use the idea of full arrangement based on dictionary order to solve problems , Because I haven't understood the other two yet , So coding is a little difficult .
About : The arrangement process of dictionary order is from the minimum dictionary order , The process of arranging to the maximum lexicographic order . Use the pre suffix method to locate .
Borrow the example of a big man to understand .
Code implementation
The code implementation is as follows .
package cz;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
public class PermutationP_0725 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String s = "bac";
String[] res=Permutation(s);
}
public static String [] Permutation(String str) {
// New return list
ArrayList<String> res=new ArrayList<>();
if(str.length()==0||str==null) return (String[]) res.toArray();
// Convert string to character array
char[] chars=str.toCharArray();
// Get the minimum dictionary order
Arrays.sort(chars);
res.add(String.valueOf(chars));
int len=chars.length;
while(true) {
int sindex1=len-1;
int sindex2;
while(sindex1>=1 && chars[sindex1-1]>=chars[sindex1]) {
// The preceding number is greater than the following number ,
sindex1--;
}
if(sindex1==0) {
// Maximum dictionary order , And then jump out of the loop
break;
}
sindex2=sindex1;
while(sindex2<len && chars[sindex2]>chars[sindex1-1]) {
// Locate the lowest digit greater than the position value to be exchanged ,
sindex2++;
}
swap(chars,sindex1-1,sindex2-1);// Exchange the positions of these two places
reverse(chars,sindex1);//sindex1 The remaining mantissa are in reverse order ()
res.add(String.valueOf(chars));
}
return (String[]) res.toArray(new String[res.size()]);
}
public static void reverse(char[] chars,int k){
// Determine the input of outliers
if(chars==null||chars.length<=k) return;
for(int i=0;i<(chars.length-k)/2;i++) {
int left=k+i;
int right=chars.length-1-i;
if(left<=right) {
swap(chars,left,right);
}
}
}
public static void swap(char[] chars, int i, int j){
char temp=chars[i];
chars[i]=chars[j];
chars[j]=temp;
}
}
边栏推荐
猜你喜欢

数通基础-TCPIP参考模型

The difference between equals and = =
![[Halcon vision] image filtering](/img/7a/b95f8977f02fab644ef9fb205424e7.png)
[Halcon vision] image filtering

Introduction to latex, EPS picture bounding box

canvas上传图片base64-有裁剪功能-Jcrop.js

【Halcon视觉】编程逻辑

Data communication foundation - layer 2 switching principle

The charm of SQL optimization! From 30248s to 0.001s

数据库的复习--1.概述

Learning about opencv (2)
随机推荐
【Halcon视觉】数组
[Halcon vision] morphological corrosion
简单化构造函数的继承方法(一)- 组合继承
[qualcomm][network] QTI service analysis
Data communication foundation telnet remote management equipment
Li Kou daily question 917
Review of database -- 3. SQL language
Study on the basis of opencv
What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?
Introduction to latex, EPS picture bounding box
【Halcon视觉】图像的傅里叶变换
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
[Halcon vision] polar coordinate transformation
INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
Under win10 64 bit, matlab fails to configure notebook
Dynamically determine file types through links
Agenda express | list of sub forum agenda on July 27
2022/07/25------字符串的排列
Cause: could't make a guess for solution
String null to empty string (what does empty string mean)