LeetCode Day1
Palindrome Linked List

/**
* LeetCode: Palindrome Linked List
* Given a singly linked list, determine if it is a palindrome.
* Could you do it in O(n) time and O(1) space?
*
* @author LuoPeng
* @time 2015.8.3
*
*/
public class Solution {
/**
* Reverse the second part, then compare the nodes one by one.
* Time: O(n) + O((n-1)/2) + O(n/2-1) + O(n/2) + O(1) = O(n)
* Space: Only 7 parameters are needed, so it is O(1)
*
* @param head the first node of a linked list
* @return true if it is a palindrome, false otherwise.
*/
public boolean isPalindrome(ListNode head) {
if ( head == null || head.next == null) {return true;}
ListNode middle = null; // the middle index of linked list
ListNode current = null; // the nodes need to be reversed
ListNode lastHaveReversed = null; // the last node that has been reversed
ListNode temp = head;
/*
* the size of linked list
* Time: O(n)
*/
int length = 0;
while (temp != null) {
temp = temp.next;
length++;
}
/*
* get the index of middle
* Time: O((n-1)/2)
*/
int i = 0, size;
middle = head;
size = (length-1)/2;
while ( i < size) {
middle = middle.next;
i++;
}
/*
* reverse the last half part, from middleNext to the end
* just like the insert-sort
* Time: O(n/2-1)
*/
size = length/2-1; // the number of nodes need to be reversed
lastHaveReversed = middle.next;
current = lastHaveReversed.next;
for ( i = 0; i < size; i++) {
lastHaveReversed.next = current.next;
current.next = middle.next;
middle.next = current;
current = lastHaveReversed.next;
}
/*
* judge
* Time: O(n/2)
*/
temp = head; // the start of first part
middle = middle.next; // the start of second part
for ( i = 0; i < length/2; i++) {
if ( temp.val == middle.val) {
temp = temp.next;
middle = middle.next;
} else {
break;
}
}
return i == length/2;
}
/**
* For every node in the first part, find the other one in the second part and compare the value
* Time: O(n)
* Space: O(1)
*
* @param head
* @return
*/
public boolean isPalindrome2(ListNode head) {
if ( head == null || head.next == null) {
return true;
} else {
ListNode temp = head;
int length = 0;
while (temp != null) {
temp = temp.next;
length++;
}
int i, j;
for ( i = 0; i < length/2; i++) {
j = i;
temp = head;
while (j < length-1-i) {
temp = temp.next;
j++;
}
if ( head.val != temp.val) {
break;
}
head = head.next;
}
return i == length/2;
}
}
}
Implement Queue using Stacks

/**
* LeetCode: Implement Queue using Stacks
*
* @author LuoPeng
* @time 2015.8.3
*
*/
class MyQueue {
// Push element x to the back of queue.
public void push(int x) {
main.push(x);
}
// Removes the element from in front of queue.
public void pop() {
int size = main.size();
for ( int i = 0; i < size-1; i++ ) {
help.push(main.peek());
main.pop();
}
main.pop();
for ( int i = 0; i < size-1; i++) {
main.push(help.peek());
help.pop();
}
}
// Get the front element.
public int peek() {
int size = main.size();
for ( int i = 0; i < size-1; i++ ) {
help.push(main.peek());
main.pop();
}
int value = main.peek();
for ( int i = 0; i < size-1; i++) {
main.push(help.peek());
help.pop();
}
return value;
}
// Return whether the queue is empty.
public boolean empty() {
return main.empty();
}
private MyStack main = new MyStack();
private MyStack help = new MyStack();
}
class MyStack {
public void push(int x) {
values.add(x);
}
public void pop() {
values.remove(values.size()-1);
}
public int peek() {
return values.get(values.size()-1);
}
public boolean empty() {
return values.size() == 0;
}
public int size() {
return values.size();
}
private List<Integer> values = new ArrayList<Integer>();
}
LeetCode Day1的更多相关文章
- 【LeetCode算法题库】Day1:TwoSums & Add Two Numbers & Longest Substring Without Repeating Characters
[Q1] Given an array of integers, return indices of the two numbers such that they add up to a specif ...
- leetcode每日刷题计划-简单篇day1
orzorz开始刷题 争取坚持每周平均下来简单题一天能做两道题吧 非常简单的题奇奇怪怪的错误orz越不做越菜 Num 7 整数反转 Reverse Integer 刚开始多给了一个变量来回折腾占地方, ...
- 刷leetcode是什么样的体验?【转】
转自:https://www.zhihu.com/question/32322023 刷leetcode是什么样的体验? https://leetcode.com/ 1 条评论 默认排序 按时间排 ...
- 知乎上的一些文章---leetcode【笔记1】
张土汪 http://github.com/shawnfan Java{script}代码仔 42 人赞同 [1.19.2017] 更新: 2017年1月17日, 陪我征战多年的 2014 MackB ...
- 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)
Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
随机推荐
- Python re模块 正则表达式
1 简介 就其本质而言,正则表达式(或 RE)是一种小型的.高度专业化的编程语言,(在Python中)它内嵌在Python中,并通过 re 模块实现.正则表达式模式被编译成一系列的字节码,然后由用 C ...
- convention-plugin
1.官方介绍的地址: http://struts.apache.org/2.1.6/docs/convention-plugin.html 2.struts.xml文件配置 只挑选几个重要的常量说明: ...
- C#.NET学习笔记11,12---布尔表达式2组合,if语句
C#.NET学习笔记11---布尔表达式2组合 2013/9/6 技术qq交流群:JavaDream:251572072 教程下载,在线交流:创梦IT社区:www.credream.com int ...
- Android中实现静态的默认安装和卸载应用
近期好长时间都没有写blog了,主要是由于近期工作上的事以及下载Android源代码的事耽误的(下载源代码这件事会在兴许的blog中写道.这个真的非常有意义呀~~),那么今天来写点什么呢?基本的灵感来 ...
- 让浏览器非阻塞加载javascript的几种方式
通常大多数浏览器是并行下载资源的,但由于外部脚本的特殊性例如通过脚本改变文档的DOM结构.脚本之间的存在依赖关系.使用document.write 向页面输出HTML等.浏览器为了确保正确执行脚本和呈 ...
- android 获取屏幕尺寸
文章转载自:http://blog.csdn.net/congqingbin/article/details/7474276// 通过WindowManager获取 DisplayMetrics dm ...
- Unhandled event loop exception 解决办法
网上搜索了一下.对其他人有效的办法有两种: 1. 安装了adsafe .卸载掉就可以了. 2.安装了流氓软件:百度杀毒. 卸载掉也解决问题了 我就是第三种情况.到现在还没解决的热.!. 谁还有更好的 ...
- application,session,cookie三者之间的区别和联系
application: 程序全局变量对象,对每个用户每个页面都有效 session: 用户全局变量,对于该用户的所有操作过程都有效 session主要是在服务器端用,一般对客户端不 ...
- SVG 和字符图标
制作网站往往需要使用一些图标来提高用户体验,如果我们的是一些扁平化设计的图标,我们可以选择 SVG 或 图标字体来提高用户体验. 下面对这两种技术进行比较. 开发难度: 现在的在线工具非常强大,比如 ...
- Android通过PHP连接MySQL(用到Json)
1下载phpnow 如果已经有mysql 则需要换一个端口 在服务器机器上的phpnow安装目录E:\PHPnow-1.5.5\htdocs下新建一个test.php文件: 其中我用的数据库是test ...