Code Hunt Sector 06 Strings - Java

原创
2016/06/04 15:18
阅读数 185

微软编码游戏 https://www.codehunt.com

Code Hunt 06.01

public class Program {
    public static Boolean Puzzle(String s) {
        return false;
    }
}

 

Code Hunt 06.02 - 没有找到三星解法

一星解法:

public class Program {
    public static String Puzzle(String s) {
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i += 2) {
            c[i] = Character.toUpperCase(c[i]);
        }
        return new String(c);
    }
}

二星解法:

public class Program {
    public static String Puzzle(String s) {
        s = s.length() > 1 ? s.substring(0, 1).toUpperCase() + s.substring(1) : s.toUpperCase();
        return s.length() > 2 ? s.substring(0, 2) + Puzzle(s.substring(2)) : s;
    }
}

 

Code Hunt 06.03 - 没有找到三星解法

二星解法:

public class Program {
    public static String Puzzle(String s) {
        char[] c = s.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (i == c.length - 1 || c[i + 1] == ' ') {
                c[i] = Character.toUpperCase(c[i]);
            }
        }
        return new String(c);
    }
}

 

Code Hunt 06.04

public class Program {
    public static char Puzzle(String s, int x) {
        return s.charAt(x);
    }
}

 

Code Hunt 06.05

public class Program {
    public static String Puzzle(String one, String two) {
        return two.concat(one);
    }
}

 

Code Hunt 06.06

public class Program {
    public static String Puzzle(String s) {
        return s.substring(s.length() / 2);
    }
}

 

Code Hunt 06.07

public class Program {
    public static String Puzzle(String s) {
        int mid = s.length() / 2;
        return s.substring(mid + 1).toUpperCase() + s.substring(mid);
    }
}

 

Code Hunt 06.08

public class Program {
    public static int Puzzle(String a, String b) {
        return a.length() > b.length() ? a.length() : b.length();
    }
}

 

Code Hunt 06.09

public class Program {
    public static String Puzzle(String a, String b) {
        return a.length() == b.length() ? a + b : (a.length() > b.length() ? a : b);
    }
}

 

Code Hunt 06.10

public class Program {
    public static int Puzzle(String s) {
        return s.length() / 3;
    }
}

 

Code Hunt 06.11

public class Program {
    public static String Puzzle(int i, int j, String s) {
        return s.substring(i, s.length() - 1) + s.substring(j, s.length() - 1);
    }
}

 

Code Hunt 06.12

一星解法:

public class Program {
    public static String Puzzle(String s) {
	    return s + new StringBuffer(s).reverse();
    }
}

三星解法(令人无语):

public class Program {
    public static String Puzzle(String s) {
	    return s + s.charAt(1) + s.charAt(0);
    }
}

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部