Two Pointers

1. 28. Implement strStr()

  用 i 记录haystack偏移量,j 记录 needle 的偏移量。

  

 class Solution {
public int strStr(String haystack, String needle) {
int lenH = haystack.length();
int lenN = needle.length();
if( lenN > lenH)
return -1;
if(lenN == 0)
return 0;
for(int i = 0; i <= lenH - lenN; i++){
boolean flag = true;
for(int j = 0; j < lenN; j++){
if( haystack.charAt(i + j) != needle.charAt(j)){
flag = false;
break;
}
}
if (flag)
return i;
}
return -1;
}
}

2. 125. Valid Palindrome

  只需要建立两个指针,head 和 tail, 分别从字符的开头和结尾处开始遍历整个字符串,如果遇到非字母数字的字符就跳过,继续往下找,直到找到下一个字母数字或者结束遍历,如果遇到大写字母,就将其转为小写。等左右指针都找到字母数字时,比较这两个字符,若相等,则继续比较下面两个分别找到的字母数字,若不相等,直接返回false.

    Character.isLetterOrDigit ( char ).  判断是否为字符或数字

     Character.toLowercase ( char ) . 两个函数

  

 class Solution {
public boolean isPalindrome(String s) {
if( s.isEmpty())
return true; int head = 0, tail = s.length() -1;
char cHead, cTail;
while( head <= tail){
cHead = s.charAt(head);
cTail = s.charAt(tail);
if(!Character.isLetterOrDigit(cHead))
head++;
else if(!Character.isLetterOrDigit(cTail))
tail--;
else{
if(Character.toLowerCase(cHead) != Character.toLowerCase(cTail))
return false;
head++;
tail--;
}
}
return true;
}
}

3. 142. Linked List Cycle II  Medium

  用快慢指针,假设有环时,head到环起点距离为A,环起点到相遇地点为B,慢指针走A+B,快指针移动距离总是慢指针两倍,且比慢指针多走一圈设为N。A+B+N = 2A + 2B。

  A = N - B。head到环起点(新设一个指针指向head) = 一圈 减去 环起点到相遇地点 = 相遇点到环起点距离。

  

 public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while(fast!=null && fast.next!=null){
slow = slow.next;
fast = fast.next.next;
if( fast == slow){
slow = head;
while( slow != fast){
slow = slow.next;
fast = fast.next;
}
return fast;
}
} return null;
}
}

Leetcode 8 Two Pointers的更多相关文章

  1. LeetCode 4Sum (Two pointers)

    题意 Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = ...

  2. LeetCode 3Sum (Two pointers)

    题意 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all ...

  3. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  4. Leetcode 笔记 117 - Populating Next Right Pointers in Each Node II

    题目链接:Populating Next Right Pointers in Each Node II | LeetCode OJ Follow up for problem "Popula ...

  5. Leetcode 笔记 116 - Populating Next Right Pointers in Each Node

    题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...

  6. [LeetCode] Populating Next Right Pointers in Each Node II 每个节点的右向指针之二

    Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...

  7. [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针

    Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...

  8. [LeetCode]题解(python):116 Populating Next Right Pointers in Each Node

    题目来源 https://leetcode.com/problems/populating-next-right-pointers-in-each-node/ Given a binary tree ...

  9. LeetCode:Populating Next Right Pointers in Each Node I II

    LeetCode:Populating Next Right Pointers in Each Node Given a binary tree struct TreeLinkNode { TreeL ...

随机推荐

  1. spring beans源码解读之--总结篇

    spring beans下面有如下源文件包: org.springframework.beans, 包含了操作java bean的接口和类.org.springframework.beans.anno ...

  2. 服务器配置java

    先去链接下载jdk or jre(服务器上这个就好) 然后解压 tar 下载的文件,放到/usr/local/java/jdk_xxx下面 -v: 可视化显示进度. Enables verbose m ...

  3. java中求质数(素数)的问题

    这篇笔记讲讲关于java中质数的问题. 一.什么是质数(素数)? 定义:质数又称素数.一个大于1的自然数,除了1和它自身外,不能被其他自然数整除的数叫做质数:否则称为合数.它可以有无限个数. 二.ja ...

  4. transform旋转,平移,缩放,扭曲 斜切

    transform  改变rotate 旋转translate  位移scale 缩放 skew  斜切变形 记得兼容性:-webkit-   -moz-    -ms-     -o- transf ...

  5. Spring注解IOC/DI(4)

    2019-03-08/11:10:17 演示:使用注解的方式完成注入对象中的效果 注解参考链接:https://www.cnblogs.com/szlbm/p/5512931.html Spring中 ...

  6. footer固定在页面底部的实现方法总结

    方法一:footer高度固定+绝对定位 HTML代码: <body> <header>头部</header> <main>中间内容</main&g ...

  7. Apache2配置多域名站点及支持https

    0x00 预备条件 申请SSL证书 建立对应站点目录 开放443端口 0x01 配置sites-available文件 执行 vi /etc/apache2/sites-available/zecoc ...

  8. 五一出门必备的手机APP神器 让你瞬间大开眼界

    如今我们手机上有各种各样的软件,但是比较实用的又有哪些呢?所以每次大家都会花上很久的时间去查找满意的软件吧!今天就给大家送上一波福利,因为五一小长假就要到来了,说不定大家会使用到呢! 轻颜相机 轻颜相 ...

  9. intellij idea 2017和Jprofiler 10的集成 报错问题

    本来想用Jprofiler来分析一下自己写的Java项目,以提高代码执行效率和自己的编码能力.结果,官网和网上很多帖子都写了点出session->IDE integrations->选择i ...

  10. 【English】八、食物相关

    一.beer.wine.coffee.soup.oil.juice beer 啤酒 They drink beer. wine 葡萄酒 Wine and coffee. coffee 咖啡 Wine ...