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. 浅谈基于Intellij IDEA Maven的配置与使用

    在java开发中,引入jar包的方式从种类上划分,可分为自动导入和手动导入,然而,手动导入繁琐,不是很适合当前开发模式,手动导入也被自动导入所取代. 当前,Maven和Gradle是比较主流的自动导入 ...

  2. 简单多播委托Demo

    namespace ConsoleApp4 { class Program { static void Main(string[] args) { Mum mum = new Mum(); Dad d ...

  3. nginx系列7:处理HTTP请求的11个阶段

    处理HTTP请求的11个阶段 如下图: 序号 阶段 指令 备注 1 POST_READ realip 获取客户端真实IP 2 SERVER_REWRITE rewrite 3 FIND_CONFIG ...

  4. 虚拟机安装CentOS7(一)

    软件环境 虚拟机:VMware Workstation Linux:CentOS-7-x86_64-DVD-1708.iso镜像文件 虚拟机所在电脑系统:win7 安装步骤 安装VMware 下载Li ...

  5. vue鼠标悬停事件

    v-bind:title="message" <!DOCTYPE html> <html lang="en"> <head> ...

  6. flex-手机端主页布局实例---构造页面结构

    Flexbox页面布局实例,成本效果图如下, 源码下载在最下面. 源码下载:https://pan.baidu.com/s/18o5hVuWtflUpgvMk3LzQ5w  提取码:wiyc样本地址: ...

  7. 用app.net Core搞掂多国语言网站

    Asp.net Core 中文文档很少,你可以看英文的,不过英文的也是说的有点乱.这篇文章是干货. 1. 配置好你的WebApplication,使他可以支持国际化语言,修改文档Startup.cs ...

  8. Android为TV端助力:intent传递消息

    我们都知道一个activity向另外一个activity传递消息可以用intent来传递 现在需求如下,一个不断接收消息服务的service,一个收到消息进行处理的activity service第一 ...

  9. Numpy库的学习(三)

    今天我们继续学习一下Numpy库的学习 废话不多说 ,开始讲 比如我们现在想创建一个0-14这样一个15位的数组 可以直接写,但是很麻烦,Numpy中就给我们了一个方便创建的方法 numpy中有一个a ...

  10. C学习笔记(自增)

    自增 (1)后缀:与Turbo C相同,在语句结束之前或者说分号之前才会执行自增. (2)前缀: 前两个自增统一取值,后面的自增即为取值. int i=2,j; j=++i+(++i)+(++i); ...