当前位置:网站首页>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;
}
}
边栏推荐
- equals与==的区别
- Kaptcha image verification code integration
- RecyclerView最后一条显示不全或显示部分的问题解决
- 数通基础-二层交换原理
- [Halcon vision] programming logic
- Solution of inputting whole line string after inputting integer
- 【Halcon视觉】极坐标变换
- Introduction to latex, EPS picture bounding box
- Wechat official account release reminder (wechat official account template message interface)
- 关于函数模板描述错误的是(链接格式错误怎么解决)
猜你喜欢

30 minutes to thoroughly understand the synchronized lock upgrade process

Dynamically determine file types through links

【Halcon视觉】形态学腐蚀

Learning about opencv (1)

数通基础-网络基础知识

Session based recommendations with recurrent neural networks
![[Halcon vision] image filtering](/img/4b/e73a8d589b49276d96621f0ef02449.png)
[Halcon vision] image filtering

Data communication foundation STP principle

The practice of OpenCV -- bank card number recognition

About automatic operation on Web pages
随机推荐
【Halcon视觉】图像灰度变化
Google与Pixar开发Draco支持USD格式 加速3D对象传输&lt;转发&gt;
数通基础-网络基础知识
Replay the snake game with C language (II) end
Cause: couldn‘t make a guess for 解决方法
【Halcon视觉】形态学膨胀
Self encapsulated database dbutils universal template
Solution of inputting whole line string after inputting integer
The problem of four columns of Hanoi Tower
The CLOB field cannot be converted when querying Damon database
The reason why go language is particularly slow to develop run and build commands
函数模板与同名的非模板函数不可以重载(重载的定义)
数通基础-二层交换原理
INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决方式
Use of Android grendao database
[Halcon vision] affine transformation
Like, "new programmer" e-book is free for a limited time!
[Halcon vision] software programming ideas
SQL Server 2008 R2 installation problems
Yarn 'TSC' is not an internal or external command, nor is it a runnable program or batch file. The problem that the command cannot be found after installing the global package