|
| 1 | +package com.algorithm.study.demo.algorithm; |
| 2 | + |
| 3 | +import com.sun.deploy.util.StringUtils; |
| 4 | + |
| 5 | +import java.util.*; |
| 6 | +import java.util.regex.Matcher; |
| 7 | +import java.util.regex.Pattern; |
| 8 | + |
| 9 | +/** |
| 10 | + * 祖玛消除算法 |
| 11 | + * @Author: liuxun |
| 12 | + * @CreateDate: 2018/11/6 下午3:58 |
| 13 | + * @Version: 1.0 |
| 14 | + */ |
| 15 | +public class ZumaProject { |
| 16 | + public static void main(String[] args) { |
| 17 | + String[] is = new String[50]; |
| 18 | + Random ra =new Random(); |
| 19 | + for(int i=0;i<50;i++){ |
| 20 | + is[i]=ra.nextInt(3)+""; |
| 21 | + } |
| 22 | + //把输入的数据转换成字符串。这样可以利用正则表达式去查找重复数字 |
| 23 | + String input=StringUtils.join(Arrays.asList(is),""); |
| 24 | + System.out.println("初始化数据:"+input); |
| 25 | + String newInput=matcher(input); |
| 26 | + System.out.println("去掉初始化数据连续出现三次级以上数字后的结果:"+newInput); |
| 27 | + |
| 28 | + Scanner sc = new Scanner(System.in); |
| 29 | + System.out.println("输入需要消除的数字下标:"); |
| 30 | + int index=sc.nextInt(); |
| 31 | + String findInput=find(index,newInput); |
| 32 | + System.out.println("消除后的结果:"+findInput); |
| 33 | + |
| 34 | + } |
| 35 | + public static String matcher(String input) |
| 36 | + { |
| 37 | + //创建一个List 用于存放重复3次或者以上的的数字 |
| 38 | + List<String> list = new ArrayList<String>(); |
| 39 | + //创建匹配的模式 |
| 40 | + Pattern pattern = Pattern.compile("(\\w)\\1{2,}"); |
| 41 | + //匹配器 |
| 42 | + Matcher matcher = pattern.matcher(input); |
| 43 | + //查找与该模式匹配的子序列。从input 里面 查找出 与 此模式 "(\w)\1{2,}" 相匹配的 子序列。如果存在,返回true,如果不存在,返回false. |
| 44 | + while (matcher.find()) |
| 45 | + { |
| 46 | + //返回匹配的子序列,并加入到list里面。 |
| 47 | + list.add(matcher.group()); |
| 48 | + } |
| 49 | + if (list.isEmpty()){ |
| 50 | + System.out.println("当前数据没有出现连续三次或以上的数字"); |
| 51 | + return input; |
| 52 | + } |
| 53 | + System.out.println("当前连续三次或以上出现的数字为:"+list); |
| 54 | + //找到连续重复的字符,加入到数组中。 |
| 55 | + String[] strings = list.toArray(new String[0]); |
| 56 | + //找出连续并且重复的子序列。并且把这些连续重复的子序列用空字符串替换。 |
| 57 | + for(int i=0 ;i<=strings.length-1;i++){ |
| 58 | + if(strings[i].length()>1){ |
| 59 | +// System.out.println(strings[i]); |
| 60 | + input=input.replace(strings[i],""); |
| 61 | +// System.out.println(input); |
| 62 | + } |
| 63 | + } |
| 64 | + //返回把连续重复的字符赐除掉的字符序列。 |
| 65 | + return input; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * 删除字符串指定index的元素 |
| 70 | + * @param index |
| 71 | + * @param Str |
| 72 | + * @return |
| 73 | + */ |
| 74 | + public static String removeChar(int index,String Str) { |
| 75 | + Str = Str.substring(0, index) + Str.substring(index + 1, Str.length());//substring的取值范围是:[,) |
| 76 | + return Str; |
| 77 | + } |
| 78 | + /** |
| 79 | + * 用户需要消除的数字下标以及数字 |
| 80 | + * 思路就是删掉用户查找的那个数字然后返回新的字符串 |
| 81 | + * 比如{11212},用户传入下标为2的数字2返回"1112" |
| 82 | + * 然后把字符串继续重复调用matcher即可 |
| 83 | + * @param index |
| 84 | + * @return |
| 85 | + */ |
| 86 | + public static String find(int index,String array){ |
| 87 | + String newArr=removeChar(index,array); |
| 88 | + String matcherArr = matcher(newArr); |
| 89 | + if (newArr.equals(matcherArr)){ |
| 90 | + //如果用户输入数据后没有发现连续三次级以上的数据就返回原始数据 |
| 91 | + return array; |
| 92 | + } |
| 93 | + return matcherArr; |
| 94 | + } |
| 95 | +} |
0 commit comments