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. javascript 倒计数功能

    最近在项目中遇到一个倒计时功能,在网上没有找到合适的,就自己写了个方法.贴在这里,权且当个记录. export const timeRun = (timeStr, callBack) => { ...

  2. oracle学习笔记(一) oracle 体系结构简单介绍以及创建表空间和用户

    体系结构 oracle数据服务器由oracle数据库和实例组成 实例由后台进程和内存结构组成 内存结构由共享池,数据缓冲区,日志缓存区 Oracle数据库是通过表空间来存储物理表的,一个数据库实例可以 ...

  3. Spring(三)使用JdbcTemplate对象完成查询

    查询银行账户的数量 1.建立一个项目导入jar包(ioc aop dao 连接池 数据库驱动 ),拷贝容器对应的配置文件到src下 2.在配置文件中开启组件扫描 3.写一个DAO接口定义一个查询方法 ...

  4. 使用Huginn抓取Discourse论坛

    Hi! I don't know why the xpath does not work, but have an easier solution. Discourse also has a JSON ...

  5. 基于django的视频点播网站开发

    项目名称 基于django的视频点播网站开发 项目背景 学习完毕python和django之后,想找个项目练练手,本来想写个博客项目练手,无奈别人已经写过了,所以笔者就打算写一个视频点播网站,因为笔者 ...

  6. There is already an object named '#xxxx' in the database.

    这个案例是前几天同事遇到的一个案例,在存储过程中"删除"了一个临时表,然后重新创建这个临时表时遇到"There is already an object named 'x ...

  7. SpringBoot使用qq邮箱发送邮件

    最近公司要做一个邮箱注册和重置密码的功能,因为之前就做过,但是不是Springboot项目,所以相对来说还是比较容易的,在这里记录一下. 一.引用Maven依赖 这里使用spring自带的邮件jar包 ...

  8. 浅析C#中new、override、virtual关键字的区别

    Virtual : virtual 关键字用于修饰方法.属性.索引器或事件声明,并使它们可以在派生类中被重写. 默认情况下,方法是非虚拟的.不能重写非虚方法. virtual 修饰符不能与 stati ...

  9. firewalld简介及功能

    1. firewalld简介 firewalld是CentOS7/Red Hat7的一大特性,最大的好处有两个: 第一个支持动态更新,不用重启服务: 第二个就是加入了防火墙的zone概念 firewa ...

  10. Rsync + sersync 实时同步备份

    一      Rsync + Sersync  实时同步介绍 1.Rsync 服务搭建介绍 云机上搭建Rsync server,在本地搭建Rsync Clinet. 2. Sersync 服务搭建介绍 ...