Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite45e928

Browse files
refactor 409
1 parenta4f05f8 commite45e928

File tree

3 files changed

+86
-0
lines changed

3 files changed

+86
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,7 @@ Your ideas/fixes/algorithms are more than welcome!
373373
|412|[Fizz Buzz](https://leetcode.com/problems/fizz-buzz/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_412.java)| O(n)|O(1)||Easy|
374374
|411|[Minimum Unique Word Abbreviation](https://leetcode.com/problems/minimum-unique-word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_411.java)| O(?)|O(?) | |Hard| NP-Hard, Backtracking, Trie, Recursion
375375
|410|[Split Array Largest Sum](https://leetcode.com/problems/split-array-largest-sum/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_410.java)| O(nlogn)|O(1) | |Hard| Binary Search, DP
376+
|409|[Longest Palindrome](https://leetcode.com/problems/longest-palindrome/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_409.java)| O(n)|O(1)||Easy|
376377
|408|[Valid Word Abbreviation](https://leetcode.com/problems/valid-word-abbreviation/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_408.java)| O(n)|O(1)||Easy|
377378
|407|[Trapping Rain Water II](https://leetcode.com/problems/trapping-rain-water-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_407.java)| | | |Hard| Heap
378379
|406|[Queue Reconstruction by Height](https://leetcode.com/problems/queue-reconstruction-by-height/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_406.java)| O(nlogn)|O(1) | |Medium| LinkedList, PriorityQueue
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
packagecom.fishercoder.solutions;
2+
3+
/**
4+
* 409. Longest Palindrome
5+
*
6+
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
7+
* This is case sensitive, for example "Aa" is not considered a palindrome here.
8+
*
9+
* Note:
10+
* Assume the length of given string will not exceed 1,010.
11+
*
12+
* Example:
13+
* Input:
14+
* "abccccdd"
15+
*
16+
* Output:
17+
* 7
18+
*
19+
* Explanation:
20+
* One longest palindrome that can be built is "dccaccd", whose length is 7.
21+
*/
22+
publicclass_409 {
23+
publicstaticclassSolution1 {
24+
publicintlongestPalindrome(Strings) {
25+
int[]counts =newint[56];
26+
for (charc :s.toCharArray()) {
27+
if (Character.isUpperCase(c)) {
28+
counts[c -'A' +33]++;
29+
}else {
30+
counts[c -'a']++;
31+
}
32+
}
33+
booleanhasOdd =false;
34+
intlen =0;
35+
for (inti =0;i <56;i++) {
36+
if (counts[i] %2 !=0) {
37+
hasOdd =true;
38+
if (counts[i] >1) {
39+
len +=counts[i] -1;
40+
}
41+
}else {
42+
len +=counts[i];
43+
}
44+
}
45+
returnhasOdd ?len +1 :len;
46+
}
47+
}
48+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
packagecom.fishercoder;
2+
3+
importcom.fishercoder.solutions._409;
4+
importorg.junit.BeforeClass;
5+
importorg.junit.Test;
6+
7+
importstaticorg.junit.Assert.assertEquals;
8+
9+
publicclass_409Test {
10+
privatestatic_409.Solution1solution1;
11+
12+
@BeforeClass
13+
publicstaticvoidsetup() {
14+
solution1 =new_409.Solution1();
15+
}
16+
17+
@Test
18+
publicvoidtest1() {
19+
assertEquals(7,solution1.longestPalindrome("abccccdd"));
20+
}
21+
22+
@Test
23+
publicvoidtest2() {
24+
assertEquals(7,solution1.longestPalindrome("abccAccdd"));
25+
}
26+
27+
@Test
28+
publicvoidtest3() {
29+
assertEquals(983,solution1.longestPalindrome(
30+
"civilwartestingwhetherthatnaptionoranynartionsoconceivedandsodedicatedcanlongendureWeareqmetonagreatbattlefiemldoftzhatwarWehavecometodedicpateaportionofthatfieldasafinalrestingplaceforthosewhoheregavetheirlivesthatthatnationmightliveItisaltogetherfangandproperthatweshoulddothisButinalargersensewecannotdedicatewecannotconsecratewecannothallowthisgroundThebravelmenlivinganddeadwhostruggledherehaveconsecrateditfaraboveourpoorponwertoaddordetractTgheworldadswfilllittlenotlenorlongrememberwhatwesayherebutitcanneverforgetwhattheydidhereItisforusthelivingrathertobededicatedheretotheulnfinishedworkwhichtheywhofoughtherehavethusfarsonoblyadvancedItisratherforustobeherededicatedtothegreattdafskremainingbeforeusthatfromthesehonoreddeadwetakeincreaseddevotiontothatcauseforwhichtheygavethelastpfullmeasureofdevotionthatweherehighlyresolvethatthesedeadshallnothavediedinvainthatthisnationunsderGodshallhaveanewbirthoffreedomandthatgovernmentofthepeoplebythepeopleforthepeopleshallnotperishfromtheearth"));
31+
}
32+
33+
@Test
34+
publicvoidtest4() {
35+
assertEquals(3,solution1.longestPalindrome("ccc"));
36+
}
37+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp