package ygy.test.week9;
/**
* Created by guoyao on 2017/10/27.
*/publicclassBestTimetoBuyandSellStockII {publicstaticvoidmain(String[] args) {
int[] prices={7, 1, 3, 4, 6, 2, 5};
System.out.println(maxProfit(prices));
}
publicstaticintmaxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return0 ;
}
int maxProfit = 0 ;
int sellPrice=prices[0];
for(int i = 1 ;i < prices.length ;i ++) {
if(prices[i] > sellPrice) {
maxProfit+=prices[i] - sellPrice;
}
sellPrice=prices[i];
}
return maxProfit;
}
}
LinkedList Cycle
package ygy.test.week9;
/**
* Created by guoyao on 2017/10/29.
*/publicclassLinkedListCycle {publicstaticvoidmain(String[] args) {
ListNode listNode1=new ListNode(1);
ListNode listNode2=new ListNode(2);
ListNode listNode3=new ListNode(3);
listNode1.next=listNode2;
listNode2.next = listNode3;
//System.out.println(hasCycle_2(listNode1));
ListNode temp = reverseList(listNode1);
while (temp != null) {
System.out.println(temp.val);
temp=temp.next;
}
}
/**
* Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
*///leetcode answaerpublicstaticbooleanhasCycle(ListNode head) {
if(head == null || head.next == null) returnfalse;
ListNode temp=head,pre=head;
while (temp!=null && temp.next != null) {
if (head == temp.next) {
returntrue;
}
temp = temp.next ;
pre.next=head;
pre = temp;
}
returnfalse;
}
publicstaticbooleanhasCycle_2(ListNode head) {
if(head == null || head.next == null) returnfalse;
ListNode fast=head,slow=head;
while (fast!=null && fast.next != null) {
slow = slow.next;
fast=fast.next.next;
if (slow == fast) {
returntrue;
}
}
returnfalse;
}
publicstatic ListNode reverseList(ListNode head) {
ListNode reverse=null;
while (head != null) {
ListNode temp = head.next ; // L2 L3 NULL //L3 NULL
head.next = reverse ; //l1 null //L2 L1 NULL
reverse=head ; // L1 NULL
head = temp ; //L2 L3 NULL //l3 null
}
return reverse ;
}
}
/**
* Definition for singly-linked list.
*/
class ListNode {
int val;
ListNode next;
ListNode(int x) {
val=x;
next=null;
}
}
Single Number
package ygy.test.week9;
/**
* Created by guoyao on 2017/10/29.
*/publicclassSingleNumber {publicstaticvoidmain(String[] args) {
}
/**
* Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
*/publicintsingleNumber(int[] nums) {
int head=nums[0];
for(int i = 1 ;i < nums.length ; i++) {
head^=nums[i];
}
return head;
}
}
Valid Palindrome
package ygy.test.week9;
/**
* Created by guoyao on 2017/10/27.
*/publicclassValidPalindrome {publicstaticvoidmain(String[] args) {
System.out.println(isPalindrome("0P"));
}
/**
*
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a palindrome.
"race a car" is not a palindrome.
*/publicstaticbooleanisPalindrome(String s) {
if (s == null || s.length() == 0) returntrue;
int head = 0 ;
int tail = s.length() - 1;
while (head <= tail) {
char headChar=s.charAt(head);
char tailChar=s.charAt(tail);
if ( !Character.isLetterOrDigit(headChar)) {
head++;
} elseif (!Character.isLetterOrDigit(tailChar)) {
tail--;
} else {
head++;
tail-- ;
if ((Character.isDigit(headChar) || Character.isDigit(tailChar)) && headChar !=tailChar )
returnfalse;
if (headChar == tailChar ||Math.abs(headChar -tailChar) == 32)
continue;
returnfalse;
}
}
returntrue;
}
}