程序员面试金典——确定字符互异

确定字符互异

http://www.nowcoder.com/questionTerminal/9618c2a9e8a14c3e82954ee14168f592

题中要求不允许使用额外的存储结构,因此在这里用时间换空间,使用两个 for 循环,时间复杂度为 O(n*n)

import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class Solution {

    public boolean checkDifferent(String iniString) {
        if (iniString.length() == 1 || iniString.length() == 0) return true;
        if (iniString.length() > 256) return false;
        for (int i = 0; i < iniString.length(); i++) {
            for (int j = i + 1; j < iniString.length(); j++) {
                if (iniString.charAt(i) == iniString.charAt(j))
                    return false;
            }
        }
        return true;
    }

    @Test
    public void testSolve() {
        assertEquals(checkDifferent(""), true);
        assertEquals(checkDifferent("aeiou"), true);
        assertEquals(checkDifferent("BarackObama"), false);
        assertEquals(checkDifferent("A"), true);
    }
}
全部评论

相关推荐

牛客60022193...:大厂都招前端,他们觉得AI能替代前端,可能他们公司吊打btaj吧
点赞 评论 收藏
分享
头像 会员标识
12-16 14:43
浙江大学 Java
投递牛客等公司8个岗位
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务