leetcode 回文二叉树
C++最简单的方法,遍历存在vector<int> ivec容器中,然后头尾对应比较
O(n)时间,O(n)空间
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head==NULL) return true;
ListNode* cur=head;
vector<int> ivec;
while(cur){
ivec.push_back(cur->val);
cur=cur->next;
}
for(int i=;i<ivec.size()--i;i++){
int j=ivec.size()--i;
if(ivec[i]!=ivec[j]) return false;
}
return true;
}
};
C++进阶方法:使用O(n)时间,O(1)空间,对前一半元素链表指向进行翻转,然后从中间到两边遍历判断,缺点是更改了原来的链表,好处是没有占用额外存储空间;
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head==NULL||head->next==NULL) return true;
ListNode* cur,*pre;
cur=head;
int length=;
while(cur!=NULL){
length+=;
cur=cur->next;
}
cur=head->next;pre=head;head->next=NULL;
for(int i=;i<(length+)/;i++){
ListNode* temp;
temp=cur->next;
cur->next=pre;
pre=cur;
cur=temp;
}
if(length%==) pre=pre->next;
while(cur!=NULL){
if(cur->val==pre->val){
cur=cur->next;pre=pre->next;
}
else return false;
}
return true;
}
};
leetcode 回文二叉树的更多相关文章
- leetcode回文子串拆分-最小拆分次数
转载请注明来自souldak,微博:@evagle 上一篇是要输出所有的可能拆分,这回是要输出拆分次数最少的切割次数. 如果直接按照上一篇那么做的话,就会超时,因为我们在判断s[i][j]是否是回文的 ...
- Leetcode 回文数字判断
一.问题描述 判断一个integer 型的数字是否是回文,空间复杂度应该是常数级别的 . 二.问题分析 首先,负数不是回文,10的整数倍不会是回文,个位数一定是回文. 三.代码实现 思路:将一个数字翻 ...
- leetcode 回文数
判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 示例 1: 输入: 121 输出: true 示例 2: 输入: -121 输出: false 解释: 从左向 ...
- Leetcode. 回文字符串的分割和最少分割数
Q1: 回文字符串的分割 Given a string s, partition s such that every substring of the partition is a palindrom ...
- LeetCode——回文链表
题目 给定一个链表的头节点head,请判断该链表是否为回 文结构. 例如: 1->2->1,返回true. 1->2->2->1,返回true. 15->6-> ...
- LeetCode 回文串问题
5. Longest Palindromic Substring 647. Palindromic Substrings 解法一:从中心一点向两边扩展,需要考虑中心为一点,中心为两点. 解法二:马拉车 ...
- leetcode-分割回文子串
给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串. 返回 s 所有可能的分割方案. 示例: 输入: "aab" 输出: [ ["aa",&quo ...
- [LeetCode] Longest Palindrome 最长回文串
Given a string which consists of lowercase or uppercase letters, find the length of the longest pali ...
- [LeetCode] Palindrome Pairs 回文对
Given a list of unique words. Find all pairs of distinct indices (i, j) in the given list, so that t ...
随机推荐
- EditPlus配置Java编译器
一.环境说明 系统: windows 7 64位 editplus version: 4.3 二.设置步骤 打开工具中的配置用户工具: 找到用户工具User tools,点击组名Group Name ...
- Git生成公钥.pub 及秘钥 命令
Git生成公钥.pub 及秘钥 命令 ssh-keygen -t rsa -C "******@qq.com" 将.pub公钥里面内容复制到github或者将这文件交给git管理员 ...
- 为了实现动态加载而编写的自己的ClassLoader
Copy备用 之前客户要求在不重启应用的前提下实现动态增加服务及交易,在网上查了很长时间也没发现类似的技术,最后研究了一下ClassLoader.因为项目是与Spring,一开始我和同事尝试替换源码的 ...
- nginx的高级配置和优化
Nginx的高级配置(优化) 针对内核的配置优化 1)net.core.netdev_max_backlog 表示当网络接口接收数据包的速度大于内核处理这些包块的时候,允许发送到队列的数据包的最大数目 ...
- SpringBootMybatis02 mybatis-generator-gui|pageHelper|前后端分离|Filter权限实现
一.Mybatis-generator-gui 下载地址:https://github.com/LittlePageProgram/mybatis-generator-gui.git 使用方法:填写相 ...
- Vasya And The Matrix CodeForces - 1016D (思维+构造)
Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the ma ...
- JAVA笔记13-异常处理Exception
掌握:一个图(分类).五个关键字(try catch finally throws throw) 一.概念 定义: 异常指的是运行期出现的错误(如除0溢出,空指针,数组/字符串下标越界,所要读取的文件 ...
- python之sys._getframe() 用于查看函数被什么函数调用以及被第几行调用及被调用函数所在文件
import sys def get_cur_info(): print(sys._getframe().f_code.co_filename) # 当前文件名,可以通过__file__获得 prin ...
- DOM自定义属性操作
DOM标准 (一)核心DOM 可以操作一切结构化文档的API,包括HTML和XML,核心DOM是万能的,但又是繁琐的. (二)HTML DOM 专门操作HTML文档的简化版DOM AP ...
- Git的使用及安装
1安装. 步骤一 如果是32位就安装32位,64位就安装64,任选一款. 步骤二 步骤三 步骤四 步骤五 步骤六 步骤七 步骤八 步骤九 步骤十 步骤十一 上面的安装完成以后,下面的程序包按要求安装就 ...