Problem:
Given a string, determine if a permutation of the string could form a palindrome.
For example,"code"
-> False, "aab"
-> True, "carerac"
-> True.
General Analysis:
This problem is easy.
Basic idea is:
iff s with odd characters, only one character is allowed to appear odd times.
iff s with even characters, each character should appear even times.
Wrong Solution 1:
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null)
throw new IllegalArgumentException("s is null");
int len = s.length();
if (len <= 1)
return true;
boolean[] odd_flag = new boolean[26];
int odd_count = 0;
for (int i = 0; i < len; i++) {
int index = s.charAt(i) - 'a';
if (odd_flag[index]) {
odd_flag[index] = false;
odd_count--;
} else{
odd_flag[index] = true;
odd_count++;
}
}
if (odd_count >= 2)
return false;
else
return true;
}
}
Mistake Analysis 1:
Runtime Error Message:
Line 12: java.lang.ArrayIndexOutOfBoundsException: -32
Last executed input:
"AaBb//a" Mistake analysis:
Lack throughly understanding of the problem, the problem does not say the character only appears in the range from 'a' to 'z'.
Wrong Solution 2:
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null)
throw new IllegalArgumentException("s is null");
int len = s.length();
if (len <= 1)
return true;
boolean[] odd_flag = new boolean[128];
int odd_count = 0;
for (int i = 0; i < len; i++) {
int index = s.charAt(i) - '0';
if (odd_flag[index]) {
odd_flag[index] = false;
odd_count--;
} else{
odd_flag[index] = true;
odd_count++;
}
}
if (odd_count >= 2)
return false;
else
return true;
}
}
Mistake Analysis 2:
Runtime Error Message:
Line 46: java.lang.ArrayIndexOutOfBoundsException: -1
Last executed input:
"AaBb//a" Mistakes:
https://simple.wikipedia.org/wiki/ASCII
Even though the length of the ASCII table is 128, the firsrt character in the table is not '0', but null. You should not do it in such ulgy way!
Analysis 2:
Since each chracter is in the range of [0, 255], why not directly use it for indexing element???
boolean[] odd_flag = new boolean[256];
int odd_count = 0;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (odd_flag[c]) {
..
}
}
Solution:
public class Solution {
public boolean canPermutePalindrome(String s) {
if (s == null)
throw new IllegalArgumentException("s is null");
int len = s.length();
if (len <= 1)
return true;
boolean[] odd_flag = new boolean[256];
int odd_count = 0;
for (int i = 0; i < len; i++) {
char c = s.charAt(i);
if (odd_flag[c]) {
odd_flag[c] = false;
odd_count--;
} else{
odd_flag[c] = true;
odd_count++;
}
}
if (odd_count >= 2)
return false;
else
return true;
}
}
[LeetCode#266] Palindrome Permutation的更多相关文章
-
leetcode 266.Palindrome Permutation 、267.Palindrome Permutation II
266.Palindrome Permutation https://www.cnblogs.com/grandyang/p/5223238.html 判断一个字符串的全排列能否形成一个回文串. 能组 ...
-
[LeetCode] 266. Palindrome Permutation 回文全排列
Given a string, determine if a permutation of the string could form a palindrome. Example 1: Input: ...
-
LeetCode 266. Palindrome Permutation (回文排列)$
Given a string, determine if a permutation of the string could form a palindrome. For example," ...
-
[LeetCode] 267. Palindrome Permutation II 回文全排列 II
Given a string s, return all the palindromic permutations (without duplicates) of it. Return an empt ...
-
[LeetCode#267] Palindrome Permutation II
Problem: Given a string s, return all the palindromic permutations (without duplicates) of it. Retur ...
-
【leetcode】266. Palindrome Permutation
原题 Given a string, determine if a permutation of the string could form a palindrome. For example, &q ...
-
【LeetCode】266. Palindrome Permutation 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode ...
-
266. Palindrome Permutation
题目: Given a string, determine if a permutation of the string could form a palindrome. For example,&q ...
-
266. Palindrome Permutation 重新排列后是否对称
[抄题]: Given a string, determine if a permutation of the string could form a palindrome. For example, ...
随机推荐
-
在Win10系统中关闭Hyper-V
1.将鼠标移至开始图标,单击右键(注意:是右键,不是左键!!!): 2.点击“控制面板”: 3.点击“程序”: 4.点击“启用或关闭windows功能”: 5.去掉“Hyper-V”的勾选,确定:
-
.NET Framework 4 中的并行编程9---线程安全集合类
原文转载自:http://www.cnblogs.com/xray2005/archive/2011/10/11/2206745.html 在.Net 4中,新增System.Collections. ...
-
python安装numpy科学计算模块
解决两个问题: (1)Import Error: No module named numpy (2)Python version 2.7 required, which was not found i ...
-
rac 11g_生产库日志组损坏处理
原创作品,出自 "深蓝的blog" 博客,转载时请务必注明出处,否则有权追究版权法律责任. 深蓝的blog:http://blog.csdn.net/huangyanlong/ar ...
-
PAT-乙级-1029. 旧键盘(20)
1029. 旧键盘(20) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 旧键盘上坏了几个键,于是在敲一段文字的 ...
-
putty保持连接不自动段开
经常在网上看到有人说自己利用putty工具登录服务器总是连接不上,这样的情况自己在刚接触putty时也遇到过.在 Connection 里面有个 Seconds between keepaliaves ...
-
定制炫彩界面:duilib与MFC 的对比
duilib是以DirectUI为技术原理开发的一款轻量级Windows桌面UI库,使用XML来描述界面风格,界面布局,可以很方便的构建高效,绚丽的,非常易于扩展的界面.从而很好的将界面和逻辑分离,同 ...
-
leetcode-只出现一次的数字
题目:只出现一次的数字 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. 你可以不使用额外空间来实现吗? ...
-
cadence布线约束规则设置
DRC检查规则在布线过程中是必不可少的,包括时序规则,走线规则,间距规则,信号完整性规则和物理规则等,在绘制电路板时,设计相关规则满足设计需求,是非常关键的! https://wenku.baidu. ...
-
html button 点击 显示倒计时秒数
如下: <html> <body> <input type="button" value="click" id="cli ...