混合字母数字字符串排序

小谢2017 2012-10-29 11:53:56
java:一个字符串比如:17fe38D24d6e;对数字进行排序而字母位置不变,结果为:12fe34D67d8e
...全文
515 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
qqid88147418 2012-10-30
  • 打赏
  • 举报
回复
我觉得1楼的思路非常的好,然后三楼的代码非常的完美。
就看楼主了,很多东西你自己敲一下代码就能够理解了。
raistlic 2012-10-30
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 的回复:]

if( Character.isDigit(c) ) {

while( count[j] == 0 )
j++;
c = (char)(j + '0');
count[j]--;
}
这段不太懂,还有这个 count[c - '0']++;
为什么自增啊

这段挺简单啊,意思就是,首先判断是不是数字,如果是数字,那么从小到大依次检查,如果0没有……
[/Quote]

+1 =)
raistlic 2012-10-30
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

引用 3 楼 的回复:

if( Character.isDigit(c) ) {

while( count[j] == 0 )
j++;
c = (char)(j + '0');
count[j]--;
}
这段不太懂,还有这个 count[c - '0']++;
为什么自增啊

count[10]记录0-9 10个数在字符串中出现的次数
[/Quote]

+1 =)
raistlic 2012-10-29
  • 打赏
  • 举报
回复

public class DigitSort {

public static void main(String[] args) {

System.out.println(sortDigits("17fe38D24d6e"));
}

private static String sortDigits(String str) {

// O(n)

int[] count = new int[10];
for(int i=str.length()-1; i>=0; i--) {

char c = str.charAt(i);
if( Character.isDigit(c) )
count[c - '0']++;
}

StringBuilder b = new StringBuilder(str.length());
for(int i=0, j=0; i<str.length(); i++) {

char c = str.charAt(i);
if( Character.isDigit(c) ) {

while( count[j] == 0 )
j++;
c = (char)(j + '0');
count[j]--;
}
b.append(c);
}

return b.toString();
}
}

run:
12fe34D67d8e
BUILD SUCCESSFUL (total time: 0 seconds)
rorom 2012-10-29
  • 打赏
  • 举报
回复
提一个不费脑子的算法:
1.遍历String,将原串转成数组并把为数字的字符换成0,将数字专门存到另一个数组
2.排序数字数组
3.遍历原串数组,将为0的字符替换为数字数组里的数字
DreamMakers 2012-10-29
  • 打赏
  • 举报
回复
if( Character.isDigit(c) ) {

while( count[j] == 0 )
j++;
c = (char)(j + '0');
count[j]--;
}
这段不太懂,还有这个 count[c - '0']++;
为什么自增啊

这段挺简单啊,意思就是,首先判断是不是数字,如果是数字,那么从小到大依次检查,如果0没有了,就查找是否有1,如果1没有了就查找是否有2,依次类推,因为count[]数组中存储了每个数字出现的次数了。j++实现的就是往后不断查找,知道找到某个数字的个数不为0。如果还不懂我就没办法了!
24K純帥 2012-10-29
  • 打赏
  • 举报
回复
冒泡排序就行啊
名字到底多长 2012-10-29
  • 打赏
  • 举报
回复
http://blog.csdn.net/mingzidaodiduochang/article/details/8010839

自己看吧,根据串的长度挑一个

第二个参数Comparator<String>写个匿名类,内部compare方法除了2个参数都是数字的话返回真实比较值,其他
返回相等就可以了。
ZZZ5512536 2012-10-29
  • 打赏
  • 举报
回复

public static void main(String[] args) {
String s = "17fe38D24d6e";
System.out.println(sort(s));
}

private static String sort(String s) {
String str = "";
char[] chars = s.toCharArray();
// 排序数字
for(int i=0;i<chars.length-1;i++){
for(int j=i+1;j<chars.length;j++){
if(Character.isLetter(chars[i]) || Character.isLetter(chars[j])){ // 如果是字母就不比较
continue;
}
if(chars[i]>chars[j]){
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
}
}
for(char c:chars){
str+=c;
}
return str;
}
zly361814478 2012-10-29
  • 打赏
  • 举报
回复
根据1楼的思路编了下,看能否参考。
package practice;

import java.util.ArrayList;
import java.util.Collections;

//一个字符串比如:17fe38D24d6e;对数字进行排序而字母位置不变,结果为:12fe34D67d8e
public class SortedTest {
public void sortString(String s) {
String s1 = s;
int n = 0; // 集合的索引
char[] c = s1.toCharArray(); //转为字符数组。
ArrayList<Character> list = new ArrayList<Character>(); // 用来存放数字,
for (int i = 0; i < c.length; i++) {
char ch = c[i];
if (Character.isDigit(ch)) { // 如果为数字,
list.add(ch); // 就把该数字添加到集合里
c[i] = 0; // 并且把此位置的值改为0
}
// System.out.print(ch + " ");
}
Collections.sort(list); // 把集合里面的数字 从小到大排列。
// System.out.println("\n" + list.size());
for (int i = 0; i < c.length; i++) { // 重新遍历字符数组
int ch = c[i];

if (0 == ch) { // 如果此位置为0
c[i] = list.get(n); // 就把此位置的0换成集合第一个数
n++; // 移动集合索引。
}
}
System.out.println(c);
}

public static void main(String[] args) {
String s = "17fe38D24d6e";
new SortedTest().sortString(s);
}
}
wuchen1986 2012-10-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]

if( Character.isDigit(c) ) {

while( count[j] == 0 )
j++;
c = (char)(j + '0');
count[j]--;
}
这段不太懂,还有这个 count[c - '0']++;
为什么自增啊
[/Quote]
count[10]记录0-9 10个数在字符串中出现的次数
小谢2017 2012-10-29
  • 打赏
  • 举报
回复
if( Character.isDigit(c) ) {

while( count[j] == 0 )
j++;
c = (char)(j + '0');
count[j]--;
}
这段不太懂,还有这个 count[c - '0']++;
为什么自增啊

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧