翻译电话号码
来源:互联网 发布:cad编程 编辑:程序博客网 时间:2024/04/28 01:38
将 电话号码 one two 。。。nine zero
翻译成1 2 。。9 0
中间会有double
例如输入:OneTwoThree
输出:123
输入:OneTwoDoubleTwo
输出:1222
输入:1Two2输出:ERROR
输入:DoubleDoubleTwo输出:ERROR
第三题:有空格,非法字符,两个Double相连,Double位于最后一个单词都错误
package com.jhl.test;
import java.util.HashMap;
import java.util.Stack;
public class GetResult11 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String result=getResult("OneDoubleThreeDoubleDoubleFive");
System.out.println(result);
}
public static String getResult(String input){
HashMap<String,Integer> map=new HashMap<String,Integer>();
map.put("One", 1);
map.put("Two", 2);
map.put("Three", 3);
map.put("Four", 4);
map.put("Five", 5);
map.put("Six", 6);
map.put("Seven", 7);
map.put("Eight", 8);
map.put("Nine", 9);
map.put("Zero", 0);
Stack<String> stack=new Stack<String>();
for(int i=0;i<input.length();i++){
char c=input.charAt(i);
if(Character.isLetter(c)){
if(stack.isEmpty()){
stack.push(String.valueOf(c));
}else{
String temp=stack.peek();
if(temp.length()==1){
String temp1=stack.pop();
String temp2=String.valueOf(c);
stack.push(temp1+temp2);
}else if(temp.length()==2){
String temp1=stack.pop();
String temp2=String.valueOf(c);
stack.push(temp1+temp2);
}else if(temp.length()==3){
if(temp.equals("One")||temp.equals("Two")||temp.equals("Six")){
stack.push(String.valueOf(c));
}else{
String temp1=stack.pop();
String temp2=String.valueOf(c);
stack.push(temp1+temp2);
}
}else if(temp.length()==4){
if(temp.equals("Four")||temp.equals("Five")||temp.equals("Nine")||temp.equals("Zero")){
stack.push(String.valueOf(c));
}else{
String temp1=stack.pop();
String temp2=String.valueOf(c);
stack.push(temp1+temp2);
}
}else if(temp.length()==5){
if(temp.equals("Three")||temp.equals("Seven")||temp.equals("Eight")){
stack.push(String.valueOf(c));
}else{
String temp1=stack.pop();
String temp2=String.valueOf(c);
stack.push(temp1+temp2);
}
}else{
if(temp.equals("Double")){
stack.push(String.valueOf(c));
}
}
}
}else{
return "ERROR";
}
}
System.out.println(stack);
String result="";
Stack<String> checkstack=new Stack<String>();
String temp=stack.peek();
if(temp.equals("Double")){
return "ERROR";
}else{
while(!stack.isEmpty()){
String temp1=stack.pop();
if(checkstack.isEmpty()){
checkstack.push(temp1);
}else{
String temp3=checkstack.peek();
if(temp3.equals("Double")){
if(temp1.equals("Double")){
return "Error";
}else{
checkstack.push(temp1);
}
}else{
checkstack.push(temp1);
}
}
}
System.out.println(checkstack);
Stack<String> word=new Stack<String>();
while(!checkstack.isEmpty()){
String temp6=checkstack.pop();
if(word.isEmpty()){
word.push(temp6);
}else{
String temp7=word.peek();
if(temp7.equals("Double")){
word.pop();
word.push(temp6);
word.push(temp6);
}else{
word.push(temp6);
}
}
}
System.out.println(word);
while(!word.isEmpty()){
String temp4=word.pop();
int value=map.get(temp4);
result=value+result;
}
return result;
}
}
}
- 翻译电话号码
- 翻译电话号码
- *翻译电话号码
- 华为-翻译电话号码
- 电话号码的翻译
- 华为机试 翻译电话号码
- 华为上机—电话号码翻译
- 华为机试之 翻译电话号码
- 华为机试—翻译电话号码
- 华为机试电话号码翻译C++实现
- 华为机试——翻译电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 电话号码
- 行列转换 listagg & wm_concat的安装
- 百练 2682 循环移动
- 如何将Matlab代码带颜色无乱码粘贴到word或网页中
- 百练 2926 算术运算
- 百练 2000 Gold Coins
- 翻译电话号码
- 网站计数器——Java实现
- 百练 2722 学分绩点
- 百练 2885 计算反序数
- 百练 3756 多边形内角和
- RCP的Application和产品
- LeetCode | Remove Duplicates from Sorted List II
- 百练 2758 菲波那契数列(2)
- java基础入门-----思想的开端