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

Appearance settings

Commit bc81371

Browse filesBrowse files
committed
commit
1 parent a0d0d44 commit bc81371
Copy full SHA for bc81371

File tree

Expand file treeCollapse file tree

7 files changed

+344
-103
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+344
-103
lines changed

‎.idea/modules.xml

Copy file name to clipboardExpand all lines: .idea/modules.xml
+1Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎.idea/workspace.xml

Copy file name to clipboardExpand all lines: .idea/workspace.xml
+121-103Lines changed: 121 additions & 103 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit4">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.12/junit-4.12.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
15+
</CLASSES>
16+
<JAVADOC />
17+
<SOURCES />
18+
</library>
19+
</orderEntry>
20+
</component>
21+
</module>
+20Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
4+
/**
5+
* @author: wangjunchao(王俊超)
6+
* @time: 2019-07-03 04:15
7+
**/
8+
public class Main {
9+
@Test
10+
public void test1() {
11+
Solution solution = new Solution();
12+
Assert.assertEquals(7, solution.longestPalindrome("abccccdd"));
13+
}
14+
15+
@Test
16+
public void test2() {
17+
Solution solution = new Solution();
18+
Assert.assertEquals(7, solution.longestPalindrome("aAaaaaaa"));
19+
}
20+
}
+55Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* https://leetcode.com/problems/longest-palindrome/
3+
*
4+
* @author: wangjunchao(王俊超)
5+
* @time: 2019-07-03 04:08
6+
**/
7+
public class Solution {
8+
/**
9+
* <pre>
10+
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
11+
*
12+
* This is case sensitive, for example "Aa" is not considered a palindrome here.
13+
*
14+
* Note:
15+
* Assume the length of given string will not exceed 1,010.
16+
*
17+
* Example:
18+
*
19+
* Input:
20+
* "abccccdd"
21+
*
22+
* Output:
23+
* 7
24+
*
25+
* Explanation:
26+
* One longest palindrome that can be built is "dccaccd", whose length is 7.
27+
* 思路
28+
* 1、先记录每个字符出现的次数
29+
* 2、取每个字符出现的最大的偶数
30+
* 3、如果还有剩下的单个字符,只取其中的一个
31+
* </pre>
32+
*/
33+
public int longestPalindrome(String s) {
34+
if (s == null || s.length() < 1) {
35+
return 0;
36+
}
37+
38+
int[] counter = new int[256];
39+
40+
for (int i = 0; i < s.length(); i++) {
41+
char ch = s.charAt(i);
42+
counter[ch - 'A']++;
43+
}
44+
45+
int result = 0;
46+
int left = 0;
47+
for (int value : counter) {
48+
49+
result += 0xFFFFFFFE & value;
50+
left |= 0b1 & value;
51+
}
52+
53+
return result + left;
54+
}
55+
}
+60Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* https://leetcode.com/problems/longest-palindrome/
3+
*
4+
* @author: wangjunchao(王俊超)
5+
* @time: 2019-07-03 04:08
6+
**/
7+
public class Solution1 {
8+
/**
9+
* <pre>
10+
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
11+
*
12+
* This is case sensitive, for example "Aa" is not considered a palindrome here.
13+
*
14+
* Note:
15+
* Assume the length of given string will not exceed 1,010.
16+
*
17+
* Example:
18+
*
19+
* Input:
20+
* "abccccdd"
21+
*
22+
* Output:
23+
* 7
24+
*
25+
* Explanation:
26+
* One longest palindrome that can be built is "dccaccd", whose length is 7.
27+
* 思路
28+
* 1、先记录每个字符出现的次数
29+
* 2、取每个字符出现的最大的偶数
30+
* 3、如果还有剩下的单个字符,只取其中的一个
31+
* </pre>
32+
*/
33+
public int longestPalindrome(String s) {
34+
if (s == null || s.length() < 1) {
35+
return 0;
36+
}
37+
38+
int[] counter = new int[256];
39+
40+
for (int i = 0; i < s.length(); i++) {
41+
char ch = s.charAt(i);
42+
counter[ch - 'A']++;
43+
}
44+
45+
int result = 0;
46+
int left = 0;
47+
for (int value : counter) {
48+
if (value > 0) {
49+
if (value % 2 == 0) {
50+
result += value;
51+
} else {
52+
result += value - 1;
53+
left = 1;
54+
}
55+
}
56+
}
57+
58+
return result + left;
59+
}
60+
}
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* https://leetcode.com/problems/longest-palindrome/
3+
* @author: wangjunchao(王俊超)
4+
* @time: 2019-07-03 04:08
5+
**/
6+
public class Solution2 {
7+
/**
8+
* <pre>
9+
* Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
10+
*
11+
* This is case sensitive, for example "Aa" is not considered a palindrome here.
12+
*
13+
* Note:
14+
* Assume the length of given string will not exceed 1,010.
15+
*
16+
* Example:
17+
*
18+
* Input:
19+
* "abccccdd"
20+
*
21+
* Output:
22+
* 7
23+
*
24+
* Explanation:
25+
* One longest palindrome that can be built is "dccaccd", whose length is 7.
26+
* 思路
27+
* 1、先记录每个字符出现的次数
28+
* 2、取每个字符出现的最大的偶数
29+
* 3、如果还有剩下的单个字符,只取其中的一个
30+
* </pre>
31+
*/
32+
public int longestPalindrome(String s) {
33+
if (s == null || s.length() < 1) {
34+
return 0;
35+
}
36+
37+
int[] counter = new int[26];
38+
// TIP: 这里是大小写不敏感的
39+
for (int i = 0; i < s.length(); i++) {
40+
char ch = s.charAt(i);
41+
if (ch >= 'a' && ch <= 'z') {
42+
counter[ch - 'a']++;
43+
} else {
44+
counter[ch - 'A']++;
45+
}
46+
}
47+
48+
int result = 0;
49+
int left = 0;
50+
for (int value : counter) {
51+
if (value > 0) {
52+
if (value % 2 == 0) {
53+
result += value;
54+
// counter[i] = 0;
55+
} else {
56+
result += value - 1;
57+
// counter[i] = 1;
58+
// 记录还有剩下的1
59+
left = 1;
60+
}
61+
}
62+
}
63+
64+
return result + left;
65+
}
66+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.