第一种思路:
swift:
let array1 = NSMutableArray.init(array: [1,2,3,4])
let array22 = NSMutableArray.init(array: [])
for index1 in 0..<4 {
let a1 = array1[index1]
for index2 in 0 ..<4 {
let a2=array1[index2]
if (a1 as! Int)==(a2 as! Int) {
continue
}
for index3 in 0..<4 {
let a3=array1[index3]
if (a1 as! Int)==(a3 as! Int)||(a2 as! Int)==(a3 as! Int) {
continue
}else{
let mid = (a1 as! Int)*100+(a2 as! Int)*10+(a3 as! Int)
array22.addObject(mid)
}
}
}
}
print("不重复",array22.count)
print(array22)
第二种思路:
(1).swift
let array1=NSMutableArray.init(array: [1,2,3,4]);
let array22 = NSMutableArray.init(array: []);
for index1 in 0 ..<4 {
if(index1 != 0){
let tmp1=array1[index1];
array1[index1]=array1[0];
array1[0]=tmp1;
}
let a1=array1[0];
for index2 in 1 ..<4 {
let a2=array1[index2]
for index3 in 1 ..<4 {
let a3=array1[index3]
if ((a2 as! Int) != (a3 as! Int)) {
let mid = (a1 as! Int)*100+(a2 as! Int)*10+(a3 as! Int)
array22.addObject(mid);
}
}
}
}
print("2不重复",array22.count)
print( array22)
(2).java
public staticint getTotal(int index) {
return index>1 ? index*getTotal(index-1) :1;
}
public staticvoid main(String[] args) {
String number[]={"1","2","3","4"};
int total=getTotal(number.length);
System.out.println("total="+total);
for(int i=0;i<number.length;i++){
if(i !=0){
String tmp1=number[i];
number[i]=number[0];
number[0]=tmp1;
}
for(int j=1;j<number.length;j++){
for(int k=1;k<number.length;k++){
if(j!=k)
System.out.println(number[0]+number[j]+number[k]);
}
}
}
}
第三种思路:
代码省略:
原理:
123是最小值,432是最大值;
遍历排除掉有相同数字的即可。
总结:
第二种循环次数最少,第三种循环次数最多