[LeetCode#266] Palindrome Permutation
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, ...
随机推荐
- awk实用技巧
awk '{sum+=$1} END {print "Sum = ", sum}' awk '{sum+=$1} END {print "Average = " ...
- Java代码操作HDFS(在/user/root/下面創建目錄)
1.创建HDFS目录并打成jar包 package Hdfs; import java.io.IOException; import java.net.URI; import org.apache.h ...
- CSS权威指南-第三版--读书笔记
第一章:CSS和文档 html是结构化语言,css是样式语言,html主要用来被强大的搜索引擎更好的索引,更好的让一个盲人通过语音浏览器来了解我们的网页,这也就是为什么说html是结构话语言,因为这是 ...
- 样式 style="clear:both"
<div style="clear:both"></div>clear:both该属性的值指出了不允许有浮动对象的边.通俗的讲:这段代码的做用是:清除同行元 ...
- [验证码实现] Captcha 验证码类,一个很个性的验证码类 (转载)
点击下载 Captcha.zip /// <summary> /// 类说明:条码生成类 /// 编 码 人:苏飞 /// 联系方式:361983679 /// 更新网站:[url=htt ...
- SOCKET,TCP/UDP,HTTP,FTP
(一)TCP/UDP,SOCKET,HTTP,FTP简析 TCP/IP是个协议组,可分为三个层次:网络层.传输层和应用层: 网络层:IP协议.ICMP协议.ARP协议.RARP协议和BOOTP协议 传 ...
- Android 设计随便说说
我曾经搞过应用程序的设计,当时只是读了半本宝典<重构...>,现在看来就这半本九阴真经,收益甚多啊 .再加上这现年工作上的印证,基本上可以拿出喷一下了.当然现在看来当年的项目设计真是很烂了 ...
- Android - IOExceptionConnection to xxx refused.
还是stackoverflow上老外牛,往google上type一下,就找到原因了. 今天在使用Apache提供的HttpClient连接Tomcat服务器,使用log捕获异常的时候,提示说:IOEx ...
- javascript创建对象(一)
对象定义:无序属性的集合,属性包含基本值.对象.函数,相当于一组没有特定顺序的值. 创建自定义对象最简单的方式就是: var movie=new Object(); movie.name=&qu ...
- 3D Game Programming with directx 11 习题答案 8.3
第八章 第三题 1.将flare.dds和flarealpha.dds拷贝到工程目录 2.创建shader resource view HR(D3DX11CreateShaderResourceVie ...