华为的一道机试题,单词倒序

单词倒序,“-”作为单词的一部分,"--"作为和其他特殊字符或数字作为分隔符

如: i am a--stu-dent

输出:stu-dent a am i


我使用的是栈的概念来解决问题

变量命名随便弄的。。。

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;




public class Main {


	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
			String input="@@I--am n-ot*&(under--stand3you";
			Stack<Character> charstack=new Stack<>();
			for(int i=0;i<input.length();i++)
			{
				charstack.push(input.charAt(i));
				System.out.println(input.charAt(i));
			}
			System.out.println(charstack.size());
			Stack<String> result=new Stack<String>();
			String tempstr="";
			while(!charstack.isEmpty())
			{
				char temp=charstack.pop();
				System.out.println(temp+"--");
				if((temp>='a' && temp<='z') || (temp>='A' && temp<='Z')){
					tempstr=temp+tempstr;
				}
				else if(temp=='-'){
					if(charstack.peek()=='-'){
						if(!tempstr.equals("")){
							result.push(tempstr.trim());
						}
						if(!result.peek().equals(" ")){
							result.push(" ");
						}
						charstack.pop();
						tempstr="";
					}else{
						tempstr=temp+tempstr;
					}
				}
				else{
					if(!tempstr.equals("")){
						result.push(tempstr.trim());
					}
					if(!result.peek().equals(" ")){
						result.push(" ");
					}
					tempstr="";
				}
			}
			result.add(tempstr);
			for(int k=0,h=result.size();k<h;k++)
			{
				System.out.print(result.get(k));
			}
	}


}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值