字符串排序(2015华为校招)

字符串排序(2015华为校招)

1、字符串由大小写字母。数字以及空格组成,一个或多个空格将多个字符串隔开

2、解析出各个字符串
3、输出结果时,将得出的各子串按照长度从大到小排序,如果长度一样,按照小写字母>大写字母>数字,进行排序
4、输出结果时,一个空格隔离各个子串

比如w23r at 1rt Af w13r,排序后为w13r w23r 1rt at Af

Methods-01

//默认导入的基本包是java.lang,不是包含在基本包的类一定要import导入相应的包
public class StringCompareSort
{
    public static void main(String[] args)
    {
		           
		String str = "w23r at 1rt Af w13r";
		
		int[] index = new int[str.length()];
		int i;
		for(i=0; i<str.length(); i++) 
			index[i] = 0;
			
		//index[]存储字符串str空格位置索引
		i = 0;
		index[i] = str.indexOf(" ");
		i++;
		while((index[i-1]>0) && (i<str.length()))
		{
			index[i] = str.indexOf(" ", index[i-1]+1);
			i++;
		}
		
		//统计字符串str空格数
		int space_num = 0;
		for(int element : index)
			if(index[space_num]>0)
				space_num++;
				
		//获取空格之间的子串并存入child_str[]中		
		String[] child_str = new String[space_num+1];
		i = 0;
		child_str[i] = str.substring(0, index[i]);
		for(i=1; i<space_num; i++)
		{
			child_str[i] = str.substring(index[i-1]+1, index[i]);
		}
		child_str[i] = str.substring(index[i-1]+1);
			

		//按要求排序	
		String temp_str = "";
		for(i=0; i<space_num; i++)
			for(int j=0; j<space_num-i; j++)
			{
				if(child_str[j].length() < child_str[j+1].length())
				{
					temp_str = child_str[j+1];
					child_str[j+1] = child_str[j];
					child_str[j] = temp_str;	
				}
				else if(child_str[j].length() == child_str[j+1].length())	
				{
					for(int k=0; k<child_str[j].length(); k++)
					{
					    if((((child_str[j].charAt(k)>='A')&&(child_str[j].charAt(k)<='Z')) && ((child_str[j+1].charAt(k)>='a')&&(child_str[j+1].charAt(k)<='z'))) ||
					       (((child_str[j].charAt(k)>='0')&&(child_str[j].charAt(k)<='9')) && ((child_str[j+1].charAt(k)>='a')&&(child_str[j+1].charAt(k)<='z'))) ||
					       (((child_str[j].charAt(k)>='0')&&(child_str[j].charAt(k)<='9')) && ((child_str[j+1].charAt(k)>='A')&&(child_str[j+1].charAt(k)<='Z')))
					      )
						{
							temp_str = child_str[j+1];
							child_str[j+1] = child_str[j];
							child_str[j] = temp_str;
							break;	
						}
						else if((((child_str[j].charAt(k)>='a')&&(child_str[j].charAt(k)<='z')) && ((child_str[j+1].charAt(k)>='A')&&(child_str[j+1].charAt(k)<='Z'))) ||
						        (((child_str[j].charAt(k)>='a')&&(child_str[j].charAt(k)<='z')) && ((child_str[j+1].charAt(k)>='0')&&(child_str[j+1].charAt(k)<='9'))) ||
						        (((child_str[j].charAt(k)>='A')&&(child_str[j].charAt(k)<='Z')) && ((child_str[j+1].charAt(k)>='0')&&(child_str[j+1].charAt(k)<='9')))
						       ) break;	
						else if(((child_str[j].charAt(k)>='0')&&(child_str[j].charAt(k)<='9')) && ((child_str[j+1].charAt(k)>='0')&&(child_str[j+1].charAt(k)<='9')))
								if(child_str[j].charAt(k) > child_str[j+1].charAt(k))	
								{
									temp_str = child_str[j+1];
									child_str[j+1] = child_str[j];
									child_str[j] = temp_str;
									break;	
								}
					}
				}
			}
	//DEBUG
		for(String element : child_str)
			System.out.println(element);
		System.out.println("-------------------");
		
		str = "";
		for(i=0; i<space_num; i++)
		{
			str += child_str[i]+" ";
		}
		str += child_str[i];
		System.out.println(str);
	}
}

Methods-02

import java.util.*;

public class StringCompareSort
{
	public static void main(String[] args)
	{
		Scanner in = new Scanner(System.in);
		String str = in.nextLine();
		String[] words = str.split("\\W+");
		List<Item> L = new ArrayList();
		for(String word : words) L.add(new Item(word));
		System.out.println(L);
		Collections.sort(L);	
		System.out.println(L);
	}	

}

class Item implements Comparable<Item>
{
	private String word;
	
	public Item(String word) { this.word = word;}
	public String getWord() { return word;}
	public String toString() { return word;}
		
	public int compareTo(Item other)
	{
		int result = Integer.compare(other.getWord().length(), word.length());
		if(result == 0)
			return other.getWord().compareTo(word);
		else 
			return result;
	}	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值