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的更多相关文章

  1. 【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 ...

  2. leetcode每日刷题计划-简单篇day1

    orzorz开始刷题 争取坚持每周平均下来简单题一天能做两道题吧 非常简单的题奇奇怪怪的错误orz越不做越菜 Num 7 整数反转 Reverse Integer 刚开始多给了一个变量来回折腾占地方, ...

  3. 刷leetcode是什么样的体验?【转】

    转自:https://www.zhihu.com/question/32322023 刷leetcode是什么样的体验? https://leetcode.com/ 1 条评论   默认排序 按时间排 ...

  4. 知乎上的一些文章---leetcode【笔记1】

    张土汪 http://github.com/shawnfan Java{script}代码仔 42 人赞同 [1.19.2017] 更新: 2017年1月17日, 陪我征战多年的 2014 MackB ...

  5. 【Leetcode周赛】从contest-51开始。(一般是10个contest写一篇文章)

    Contest 51 (2018年11月22日,周四早上)(题号681-684) 链接:https://leetcode.com/contest/leetcode-weekly-contest-51 ...

  6. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. [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 ...

  9. 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 ...

随机推荐

  1. OutputCache祥解

    当用户訪问页面时,整个页面将会被server保存在内存中,这样就对页面进行了缓存.当用户再次訪问该页,页面不会再次运行数据操作,页面首先会检查server中是否存在缓存,假设缓存存在,则直接从缓存中获 ...

  2. MFC多线程内存泄漏问题&amp;解决方法

    在用visual studio进行界面编程时(如MFC),前台UI我们能够通过MFC的消息循环机制实现.而对于后台的数据处理.我们可能会用到多线程来处理. 那么对于大多数人(尤其是我这样的菜鸟),一个 ...

  3. LR实战之Discuz开源论坛——登录脚本

    脚本业务流:访问Discuz论坛首页——登录论坛——退出论坛.本次使用LoadRunner11版本. 一.录制脚本注意 1.确保Discuz论坛能在服务器运行正常. 2.录制前先试访问Discuz论坛 ...

  4. MVC学习 (一)

    在学习MVC之前对asp.net MVC已经有了一些了解,但是还是有很多的疑问,接下来我慢慢来看书学习并带着问题写博客以作记录. 1.MVC是什么? 2.Asp.net MVC和传统的Asp.net ...

  5. JQ 替换节点

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  6. ANCS协议翻译

    综述 苹果通知中心(Apple Notification Center Service, ANCS)的目的是提供给蓝牙外设一种简单.方便的获取ios设备通知信息的方式. 依赖 ANCS的使用没有依赖, ...

  7. iOS开发那些事儿(一)轮播器

    前言 市面上绝大部分的APP被打开之后映入眼帘的都是一个美轮美奂的轮播器,所以能做出一个符合需求.高效的轮播器成为了一个程序员的必备技能.所以今天的这篇博客就来谈谈轮播器这个看似简单的控件其中蕴含的道 ...

  8. 在T-SQL语句中访问远程数据库(openrowset/opendatasource/openquery)

    1.启用Ad Hoc Distributed Queries 在使用openrowset/opendatasource前搜先要启用Ad Hoc Distributed Queries服务,因为这个服务 ...

  9. synchronized 方式实现监控器中数据成员的同步

    要对监控器中的数据成员进行访问,在考虑到多线程的情况下必须使用同步代码块来改变监控器中数据成员的值: synchronized (mAdapterLocking) { if (pEvery == 0) ...

  10. javascript事件委托和jQuery事件绑定on、off 和one

    一. 事件委托什么是事件委托?用现实中的理解就是:有100 个学生同时在某天中午收到快递,但这100 个学生不可能同时站在学校门口等,那么都会委托门卫去收取,然后再逐个交给学生.而在jQuery 中, ...