Leetcode 8 Two Pointers
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的更多相关文章
- 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 = ...
- 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 ...
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- 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 ...
- Leetcode 笔记 116 - Populating Next Right Pointers in Each Node
题目链接:Populating Next Right Pointers in Each Node | LeetCode OJ Given a binary tree struct TreeLinkNo ...
- [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 ...
- [LeetCode] Populating Next Right Pointers in Each Node 每个节点的右向指针
Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode *nex ...
- [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 ...
- 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 ...
随机推荐
- Windows环境下安装配置Mosquitto服务及入门操作介绍
关键字:在windows安装mosquitto,在mosquitto中配置日志,在mosquitto中配置用户账号密码 关于Mosquitto配置的资料网上还是有几篇的,但是看来看去,基本上都是基于L ...
- [转]Have a query in Blue prism coding stage and collection stage.
本文转自:https://www.rpaforum.net/threads/have-a-query-in-blueprism-coding-stage-and-collection-stage.48 ...
- 史上最全office2016 激活码
现在office版本已经更新到2016了,现在给大家分享Office 2016 各个版本的激活码,记得断网激活哈.(本人使用的是家庭版) Office 2016 Pro Retail 版激活密钥: [ ...
- MySQL 笔记整理(12) --为什么我的MySQL会“抖”一下?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 12) --为什么我的MySQL会“抖”一下? 断更了一段时间,因为这几 ...
- MySQL 笔记整理(10) --MySQL为什么有时会选错索引?
笔记记录自林晓斌(丁奇)老师的<MySQL实战45讲> (本篇内图片均来自丁奇老师的讲解,如有侵权,请联系我删除) 10) --MySQL为什么有时会选错索引? MySQL中的一张表上可以 ...
- QQ登录的那些坑
这几天在项目上面实现qq登录的功能,当功能做好后发现,同一个qq号登录之后腾讯返回的openid并不一样....(天啦噜啊~)然后查询文档以及咨询客服才知道注册申请时是有一个固定的套路的(不得不说,如 ...
- 章节十、6-CSS---用CSS 定位子节点
以该网址为例(https://learn.letskodeit.com/p/practice) 一.通过子节点定位元素 1.例如我们需要定位这个table表格 2.当我们通过table标签直接定位时, ...
- dede后台编辑器更改
1.下载百度开发的UEditor编辑器(对应版本): 2. 解压下载的zip文件: 3.将解压后得到的文件夹拷贝到您网站目录下的include文件夹下并改名为ueditor: 4.将inc文件夹里边的 ...
- Hadoop Yarn框架原理解析
在说Hadoop Yarn的原理之前,我们先来看看Yarn是怎样出现的.在古老的Hadoop1.0中,MapReduce的JobTracker负责了太多的工作,包括资源调度,管理众多的TaskTrac ...
- SQL Server数据库文件与文件组总结
文件和文件组概念 关于文件与文件组,简单概括如下,详情请参考官方文档"数据库文件和文件组Database Files and Filegroups"或更多相关资料: 数据文件概念: ...