재미있고 간단한 두 단락 코드
1966 단어 performanceIDEA
만약 당신이 작은 프로그램을 쓰고 싶다면 문자열의 앞뒤 두 문자가 똑같다는 것을 보증하면 다음과 같이 실현할 수 있습니다.
public static boolean isPalindrome(String string) {
for (int forward = 0, backward = string.length() - 1; forward < string
.length(); forward++, backward--)
if (string.charAt(forward) != string.charAt(backward))
return false;
return true;
}
다른 방법으로는 성능이 좋지만 가독성이 떨어지는 것이 다음과 같습니다.
public static boolean isPalindrome(String string) {
if (string.length() == 0)
return true;
int limit = string.length() / 2;
for (int forward = 0, backward = string.length() - 1; forward < limit; forward++, backward--)
if (string.charAt(forward) != string.charAt(backward))
return false;
return true;
}
agile java라는 책 중의 한 단락을 보면 다음과 같은 생각을 할 필요가 있다.
인용하다
"While normally you shouldn't worry about performance until it is a problem, an algorithm should be clean. Doing something unnecessarily in an algorithm demonstrates poor understanding of the problem. It can also confuse future developers"
다음은 테스트 코드입니다.
public void testPalindrome() {
assertFalse(isPalindrome("abcdef"));
assertFalse(isPalindrome("abccda"));
assertTrue(isPalindrome("abccba"));
assertFalse(isPalindrome("abcxba"));
assertTrue(isPalindrome("a"));
assertTrue(isPalindrome("aa"));
assertFalse(isPalindrome("ab"));
assertTrue(isPalindrome(""));
assertTrue(isPalindrome("aaa"));
assertTrue(isPalindrome("aba"));
assertTrue(isPalindrome("abbba"));
assertTrue(isPalindrome("abba"));
assertFalse(isPalindrome("abbaa"));
assertFalse(isPalindrome("abcda"));
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
.NET 6 - AutoMapper 및 데이터 전송 개체(DTO) 🗺YouTube에서 전체 동영상을 볼 수 있습니다. 오늘 AutoMapper가 무엇이고 왜 필요한지 설명하는 것으로 시작할 수 있습니다. 이제 nuget 패키지가 설치되었으므로 automapper 구현 및 활용을 시작...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.