leetcode-month2-week9

Best Time to Buy and Sell Stock II

package ygy.test.week9;

/**
 * Created by guoyao on 2017/10/27.
 */
public class BestTimetoBuyandSellStockII {

    public static void main(String[] args) {
        int[] prices={7, 1, 3, 4, 6, 2, 5};
        System.out.println(maxProfit(prices));

    }

    public static int maxProfit(int[] prices) {

        if (prices == null || prices.length == 0) {
            return  0 ;
        }

        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.
 */
public class LinkedListCycle {

    public static void main(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 answaer
    public static boolean hasCycle(ListNode head) {
        if(head == null || head.next == null) return false;
        ListNode temp=head,pre=head;
        while (temp!=null && temp.next != null) {
            if (head == temp.next) {
                return true;
            }
            temp = temp.next ;
            pre.next=head;
            pre = temp;
        }
        return false;
    }
    public static boolean hasCycle_2(ListNode head) {
        if(head == null || head.next == null) return false;
        ListNode fast=head,slow=head;
        while (fast!=null && fast.next != null) {
            slow = slow.next;
            fast=fast.next.next;
            if (slow == fast) {
                return true;
            }
        }
        return false;
    }

    public static 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.
 */
public class SingleNumber {

    public static void main(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?
     */

    public int singleNumber(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.
 */
public class ValidPalindrome {

    public static void main(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.
     */
    public static boolean isPalindrome(String s) {

        if (s == null || s.length() == 0) return true;

        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++;
            } else if (!Character.isLetterOrDigit(tailChar)) {
                tail--;
            } else {
                head++;
                tail-- ;
                if ((Character.isDigit(headChar) || Character.isDigit(tailChar)) && headChar !=tailChar )
                    return false;

                if (headChar == tailChar ||Math.abs(headChar -tailChar) == 32)
                    continue;

                return false;
            }
        }
        return true;
    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值