中级农民
- 积分
- 106
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2016-8-18
- 最后登录
- 1970-1-1
|
第一题用了一个不太一样的方法,希望看了的小伙伴能说一下是否正确,如果有错请指出!
- public static boolean checkIfFileOrdered(String ordering, String File) {
- Set<Character> set = new HashSet<>();
- for(char c: ordering.toCharArray()) {
- set.add(c);
- }
- int idx = 0; //idx in ordering
- for(char c: File.toCharArray()) {
- if(!set.contains(c)) continue;//indeicating 'x'
- else {
- if(c == ordering.charAt(idx)) continue;//dulicate current char
- else {
- //update idx, equal to the next char
- idx++;
- if(c== ordering.charAt(idx)) continue;
- else return false;
- }
- }
- }
- return true;
- }
复制代码 |
|