[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, ...
随机推荐
- 第三篇:web之前端之JavaScript基础
前端之JavaScript基础 前端之JavaScript基础 本节内容 JS概述 JS基础语法 JS循环控制 ECMA对象 BOM对象 DOM对象 1. JS概述 1.1. javascript ...
- 基于Memcache的分布式缓存系统详解
文章不是简单的的Ctrl C与V,而是一个字一个标点符号慢慢写出来的.我认为这才是是对读者的负责,本教程由技术爱好者成笑笑(博客:http://www.chengxiaoxiao.com/)写作完成. ...
- HTML+CSS基础学习笔记(4)
一.认识CSS样式 1.定义 CSS全称:层叠样式表(Cascading Style Sheets) 主要作用:定义HTML内容在浏览器内的显示样式,比如文字大小.颜色.字体加粗等 优点:通过定义某个 ...
- javascript闭包分析
闭包是什么?闭包是Closure,简而言之,闭包就是: 闭包就是函数的局部变量集合,只是这些局部变量在函数返回后会继续存在. 闭包就是就是函数的“堆栈”在函数返回后并不释放,我们也可以理解为这些函数堆 ...
- HTML中的API
在程序语言里面就使用API这个行为来讲,可拆解为两个操作:取得API接口和运行API功能 例如:书本具有传授知识的功能,这里就好比一个API,学生拿出某个课本学习,就相当于取得API,学习通过课本学习 ...
- JavaScript 删除数组重复元素
unique :function (array){ var n = {}, r = [], len = array.length, val, type; for (var i = 0; i < ...
- javascript 用函数实现“继承”
一.知识储备: 1.枚举属性名称的函数: (1)for...in:可以在循环体中遍历对象中所有可枚举的属性(包括自有属性和继承属性) (2)Object.keys():返回数组(可枚举的自有属性) ( ...
- 网页icon和文本对齐神技 2016.03.23
一直以来icon和文本需要对齐都使用vertical-align: middle;的方法,但兼容性不理想.参考了鑫旭大大的博客,终于收获不用vertical-align可以对齐的神技,原博点这里. 代 ...
- GridView获取单个单元格的值
0.GridView中的所有数据都存储在Rows集合中,可以通过Rows的Cell属性获取单个单元格的值:如果某个单元格包含其他控件,则通过使用单元格的 Controls 集合,从单元格检索控件:如果 ...
- php开源项目学习二次开发的计划
开源项目: cms 国内 dedecms cmstop 国外 joomla, drupal 电商 国内 ecshop 国外 Magento 论坛 discuz 博客 wordpress 学习时 ...