最长不重复子串


public class Solution {
public int lengthOfLongestSubstring(String s) {
if(s==null || s.length()==0){return 0;}
int len = s.length(),maxLen=0,slow=0,fast=0;
HashMap<Character,Integer> map = new HashMap<Character,Integer>();
for(;fast<len;fast++){
char currentChar = s.charAt(fast);
if(map.get(currentChar) == null){
map.put(currentChar,fast);
maxLen = Integer.max(maxLen,fast-slow + 1);
}else{
int lastPosition = map.get(currentChar);
if(lastPosition < slow){
maxLen = Integer.max(maxLen,fast - slow + 1);
map.put(currentChar,fast);
}else{
slow = lastPosition + 1;
map.put(currentChar,fast);
}
}
}
return maxLen;
}
}

String to Integer


public class Solution {
public int myAtoi(String str) {
int p = 0, ret = 0;
if(p == str.length()) return 0;
while(Character.isWhitespace(str.charAt(p)) && p<str.length()) p++;
boolean isNeg = (str.charAt(p) == '-') ? true : false;
if(str.charAt(p) == '+' || str.charAt(p) == '-') p++;
for(;p<str.length();p++){
if(str.charAt(p)>'9' || str.charAt(p)<'0'){
break;
}else{
int digit = str.charAt(p) - '0';
if(!isNeg && ret > (Integer.MAX_VALUE - digit)/10) return Integer.MAX_VALUE;
else if(isNeg && ret < (Integer.MIN_VALUE + digit)/10) return Integer.MIN_VALUE;
ret = ret * 10 + (isNeg ? -digit : digit);
}
}
return ret;
}
}

Reverse LinkedList


public class Solution {
public ListNode reverseList(ListNode head) {
if(head==null) return head; ListNode newhead = new ListNode(0);
newhead.next = head; while(head.next!=null) {
ListNode tmp = head.next;
head.next = head.next.next; tmp.next = newhead.next;
newhead.next = tmp;
}
return newhead.next;
}
}

Reverse LinkedList II


public class Solution {
public ListNode reverseBetween(ListNode head, int m, int n) {
if(head == null) return null;
ListNode dummy = new ListNode(0); // create a dummy node to mark the head of this list
dummy.next = head;
ListNode pre = dummy; // make a pointer pre as a marker for the node before reversing
for(int i = 0; i<m-1; i++) pre = pre.next;
ListNode start = pre.next; // a pointer to the beginning of a sub-list that will be reversed
ListNode then = start.next; // a pointer to a node that will be reversed
// 1 - 2 -3 - 4 - 5 ; m=2; n =4 ---> pre = 1, start = 2, then = 3
// dummy-> 1 -> 2 -> 3 -> 4 -> 5
for(int i=0; i<n-m; i++)
{
start.next = then.next;
then.next = pre.next;
pre.next = then;
then = start.next;
}
// first reversing : dummy->1 - 3 - 2 - 4 - 5; pre = 1, start = 2, then = 4
// second reversing: dummy->1 - 4 - 3 - 2 - 5; pre = 1, start = 2, then = 5 (finish)
return dummy.next;
}
}

Reverse Nodes in K-Group


public class Solution {
public ListNode reverseKGroup(ListNode head, int k) {
ListNode begin;
if (head==null || head.next ==null || k==1)
return head;
ListNode dummyhead = new ListNode(-1);
dummyhead.next = head;
begin = dummyhead;
int i=0;
while (head != null){
i++;
if (i%k == 0){
begin = reverse(begin, head.next);
head = begin.next;
} else {
head = head.next;
}
}
return dummyhead.next;
}
public ListNode reverse(ListNode begin, ListNode end){
ListNode curr = begin.next;
ListNode next, first;
ListNode prev = begin;
first = curr;
while (curr!=end){
next = curr.next;
curr.next = prev;
prev = curr;
curr = next;
}
begin.next = prev;
first.next = curr;
return first;
}
}

Linked List Cycle


public class Solution {
public boolean hasCycle(ListNode head) {
ListNode p = head,pre = head;
while(p != null && p.next != null){
if (p.next == head) return true;
p = p.next;
pre.next = head;
pre = p;
}
return false;
}
}

Linked List Cycle II


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

Sort List


public class Solution {
ArrayList<Integer> theArray=new ArrayList<Integer>();
public ListNode sortList(ListNode head) {
if(head==null)return head; ListNode focusNode=head;
while(focusNode!=null)
{
theArray.add(focusNode.val);
focusNode=focusNode.next;
} quickSort(0,theArray.size()-1); focusNode=head;
for(int i=0;focusNode!=null;i++)
{
focusNode.val=theArray.get(i);
focusNode=focusNode.next;
} return head;
} public void quickSort(int left,int right)
{
if(right-left<=0)
return;
else {
int pivot=theArray.get(right); int partition=partioning(left, right, pivot);
quickSort(left, partition-1);
quickSort(partition+1, right);
}
}
public int partioning(int left,int right,long pivot)
{
int leftptr=left-1;
int rightptr=right;
while(true)
{
while(theArray.get(++leftptr)<pivot)
;
while(rightptr>0 &&theArray.get(--rightptr)>pivot)
;
if(leftptr>=rightptr)
break;
else {
swap(leftptr,rightptr);
} }
swap(leftptr,right);
return leftptr;
}
public void swap(int indx1,int indx2)
{
int temp=theArray.get(indx1);
theArray.set(indx1, theArray.get(indx2));
theArray.set(indx2, temp);
}
}

Reorder List


public class Solution {
public void reorderList(ListNode head) {
if(head==null||head.next==null) return; //Find the middle of the list
ListNode p1=head;
ListNode p2=head;
while(p2.next!=null&&p2.next.next!=null){
p1=p1.next;
p2=p2.next.next;
} //Reverse the half after middle 1->2->3->4->5->6 to 1->2->3->6->5->4
ListNode preMiddle=p1;
ListNode preCurrent=p1.next;
while(preCurrent.next!=null){
ListNode current=preCurrent.next;
preCurrent.next=current.next;
current.next=preMiddle.next;
preMiddle.next=current;
} //Start reorder one by one 1->2->3->6->5->4 to 1->6->2->5->3->4
p1=head;
p2=preMiddle.next;
while(p1!=preMiddle){
preMiddle.next=p2.next;
p2.next=p1.next;
p1.next=p2;
p1=p2.next;
p2=preMiddle.next;
}
}
}

Copy List with Random Pointer


public class Solution {
public RandomListNode copyRandomList(RandomListNode head) {
if(head==null){
return null;
}
RandomListNode n = head;
while (n!=null){
RandomListNode n2 = new RandomListNode(n.label);
RandomListNode tmp = n.next;
n.next = n2;
n2.next = tmp;
n = tmp;
} n=head;
while(n != null){
RandomListNode n2 = n.next;
if(n.random != null)
n2.random = n.random.next;
else
n2.random = null;
n = n.next.next;
} //detach list
RandomListNode n2 = head.next;
n = head;
RandomListNode head2 = head.next;
while(n2 != null && n != null){
n.next = n.next.next;
if (n2.next == null){
break;
}
n2.next = n2.next.next; n2 = n2.next;
n = n.next;
}
return head2; }
}

LeetCode(三)的更多相关文章

  1. LeetCode 三数之和 — 优化解法

    LeetCode 三数之和 - 改进解法 题目:给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复 ...

  2. Leetcode(三)无重复字符的最长子串

    3. 无重复字符的最长子串 题目描述 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 示例: 输入: "abcabcbb" 输出: 3 解释: 因为无重复字符的最 ...

  3. [leetcode]三数之和

    三数之和 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组. 注意:答案中不可以包含重复 ...

  4. Leetcode | N-Queens I & II

    N-Queens I The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no ...

  5. [LeetCode] 3Sum Smaller 三数之和较小值

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

  6. [LeetCode] 3Sum Closest 最近三数之和

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  7. [LeetCode] 3Sum 三数之和

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

  8. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  9. LeetCode 259. 3Sum Smaller (三数之和较小值) $

    Given an array of n integers nums and a target, find the number of index triplets i, j, k with 0 < ...

随机推荐

  1. MySQL查询今天/昨天/本周、上周、本月、上个月份数据的sql代码

    MySQL查询本周.上周.本月.上个月份数据的sql代码 作者: 字体:[增加 减小] 类型:转载 时间:2012-11-29我要评论 MySQL查询的方式很多,下面为您介绍的MySQL查询实现的是查 ...

  2. [系统开发] Django Admin上传图片简单校验

    我的 models里有个ImageField字段,用来保存用户头像,希望通过Django Admin上传时校验头像大小,如果太大就报错,并且不保存. 网上有不少方法,有的通过第三方软件实现,有的通过自 ...

  3. C++模板元编程 - 函数重载决议选择工具(不知道起什么好名)完成

    这个还是基于之前实现的那个MultiState,为了实现三种类型“大类”的函数重载决议:所有整数.所有浮点数.字符串,分别将这三种“大类”的数据分配到对应的Converter上. 为此实现了一些方便的 ...

  4. android html 图片处理类--加载富文本工具类

    在android开发中,一些资讯类页面,里面有html标签和图片,html 标签一般通过Html.fromHtml方法,即可以解决,但是如果html 有图片标签,那么,Html.fromHtml 好像 ...

  5. Oracle时间戳 与时间之间的相互转换

    Unix时间戳记是从'1970-01-01 00:00:00'GMT开始的秒数,表现为整数型. Oracle中的时间是Date型,以下函数提供了两种时间转换的Oracle函数 (1)从Unix时间戳记 ...

  6. [DFNews] EIFT更新至1.2,支持iPhone4s及iPhone5物理获取

    俄罗斯厂商Elcomsoft近日更新了其旗下的iOS取证软件Elcomesoft iOS Forensic Toolkit,更新后的1.2版本支持针对iOS 4-6的iPhone 4s.iPhone5 ...

  7. Sublime Text对Python代码加注释的快捷键

    一直在Coursera上补基础课,发现很多课程都用Python作为教学语言,学了一下感觉果然好,简直是用英语在写代码.(我建Python目录的时候发现去年学过一点点Python,居然一点都不记得了= ...

  8. autoit 中_GUICtrlStatusBar_SetBkColor失效的解决办法

    #include <GuiConstantsEx.au3> #include <GuiStatusBar.au3> #include <WinAPI.au3> #i ...

  9. APK Downgrade Method working fine on LINE latest version 6.7.1

    Line is one of the most popular messaging Apps, especially in Asia. On March 3 I downgraded the app ...

  10. windows 10启动盘制作工具

    Rufus 官方网站:http://rufus.akeo.ie/