网易网考题“9和g”
2016-03-25 21:46
363 查看
一个字符串中9和g模糊,如果同等对待,输出所有可能性
public class Solution { public static String toFullBinaryString(int num,int size) { char[] chs = new char[size]; for(int i = 0; i < size; i++) { chs[size - 1 - i] = (char)(((num >> i) & 1) + '0'); } return new String(chs); } public ArrayList<String> getPossibleList(String input){ input = input.replace("9", "g"); ArrayList<String> result = new ArrayList<String>(); ArrayList<Integer> flag = new ArrayList<Integer>(); int j = 0; for(int i=0;i<input.length();i++){ if(input.charAt(i) == 'g'){ flag.add(i); } } int count = flag.size(); for(int i=0;i<Math.pow(2, count);i++){ String binary = toFullBinaryString(i,count); StringBuilder tmp = new StringBuilder(input); for(int k=0;k<binary.length();k++){ if(binary.charAt(k) == '1'){ tmp.deleteCharAt(flag.get(k).intValue()); tmp.insert(flag.get(k).intValue(),"9"); } else continue; } result.add(tmp.toString()); } return result; } public static void main(String[] args){ String input = "123g97g"; Solution sl = new Solution(); ArrayList<String> result = sl.getPossibleList(input); for(String output : result){ System.out.println(output); } System.out.println("Total : " + result.size()); } }
相关文章推荐
- 巧用ifstream判断文件是否存在
- 探讨Nodejs中的作用域问题。
- linux(centos)搭建SVN服务器
- WPF学习——依赖项属性(3)
- Java设计模式之“工厂方法模式”
- MapReduce处理多个不同的出入文件
- Windows 7 Ultimate(旗舰版)SP1 32/64位官方原版下载(2011年5月12日更新版)
- ExtJS获取父子、兄弟容器元素方法
- 20145214 《Java程序设计》第4周学习总结
- objective-C学习笔记(九)ARC
- 设置一个虚拟域名使内网其他用户也可以通过虚拟域名访问
- 用shell脚本监控进程是否存在 不存在则启动的实例
- poj3904 Sky Code【容斥原理】
- Ubuntu下编译linux内核,报"mkimage" command not found错的解决
- 无人机矩阵操作系统
- PHP include 和 require 语句
- 在Java中使用Json
- 群发微信图文消息,但是正文中的图片却不显示
- KMP训练五题
- linker command failed with exit code 1 (use -v to see invocation)