leetcode-month1-week2

LongestCommon Prefix

package ygy.test.week2;

/**
 * Created by guoyao on 2017/9/10.
 */
public class LongestCommonPrefix {

    public static void main(String[] args) {

        String [] arrs = {"abc","abcd","abcde","ab"};
        System.out.println(longestCommonPrefix(arrs));
    }

    /**
     * Write a function to find the longest common prefix string amongst an array of strings.
     */
    public  static  String longestCommonPrefix(String[] strs) {

        if (strs == null || strs.length == 0) {
            return  "" ;
        }
        String longestPrefix="";
        int index=0;
        for (String str : strs) {
            if (str == null || str.length()==0) {
                return "";
            }
            if (index == 0) {
                longestPrefix = str ;
                index++;
                continue;
            }
            longestPrefix = findCommonPreFix(longestPrefix,str);
            index ++;
        }
        return  longestPrefix ;
    }

    public static String findCommonPreFix(String longestPrefix, String str) {

        String temp="";
        for (int i = 1 ;i <= longestPrefix.length();i++ ) {
            temp=longestPrefix.substring(0, i);
            if (!str.startsWith(temp)) {
                return str.substring(0, i-1);
            }
            continue;
        }
        return temp;
    }
}

Palindrome LinkedList

package ygy.test.week2;

/**
 * Created by guoyao on 2017/9/9.
 */
public class PalindromeLinkedList {

    public static void main(String[] args) {
        ListNode listNode1=new ListNode(1);
        ListNode listNode2=new ListNode(2);
        ListNode listNode3=new ListNode(3);
        ListNode listNode4=new ListNode(2);
        ListNode listNode5=new ListNode(1);
        listNode1.next = listNode2 ;
        listNode2.next = listNode3 ;
        listNode3.next = listNode4 ;
        listNode4.next = listNode5 ;
        listNode5.next = null ;
        System.out.println(isPalindrome(listNode1));
    }
    //正解
    public static boolean isPalindrome(ListNode head) {
        if(head == null ){
            return true ;
        }
        ListNode temp1=head ,temp2 = head;
        //temp1 比temp快一倍,获取整个head的中间链表
        while (temp1 != null && temp1.next != null ) {
            temp1 = temp1.next.next ;
            temp2 =temp2.next ;
        }
        //翻转后比对数据
        ListNode reverseNode=reverseNode(temp2);
        while (reverseNode != null) {
            if (reverseNode.val != head.val) {
                return false;
            }
            reverseNode = reverseNode.next ;
            head=head.next;
        }
        return true;
    }

    private static ListNode reverseNode(ListNode head) {
        ListNode temp = null ;
        while (head != null) {
            ListNode nextNode = head.next ;
            head.next = temp ;
            temp = head ;
            head = nextNode ;
        }
        return temp ;
    }
}

class ListNode {
    int val;
    ListNode next;

    ListNode(int x) {
        val=x;
    }
}

Palindrome Number

package ygy.test.week2;

/**
 * Created by guoyao on 2017/9/9.
 */
public class PalindromeNumber {

    public static void main(String[] args) {

        System.out.println(isPalindrome(0));
    }

    /**
     * //判断回文
     * Determine whether an integer is a palindrome.
     * Do this without extra space.  不能使用额外空间
     */
    public static boolean  isPalindrome(int x) {

        //去除负数
        //去除10的整数
        if (x < 0 || (x %10 ==0 && x!= 0)) {
            return  false ;
        }

        int temp = 0 ;
        while (x > temp) {
            temp=temp * 10 + x % 10;
            x=x / 10;
        }
        return temp == x || temp /10 == x ;

        //方法1
        //if (x == ReverseInteger.reverse(x)) {
        //    return true;
        //}
        //return false;
    }
}

Reverse Integer

package ygy.test.week2;

/**
 * Created by guoyao on 2017/9/9.
 */
public class ReverseInteger {

    public static void main(String[] args) {
        //System.out.println(Integer.MAX_VALUE);
        //System.out.println(Integer.MIN_VALUE);
        System.out.println(reverse(Integer.MIN_VALUE));

    }

    /**
     * Reverse digits of an integer.
     * Example1: x = 123, return 321
     * Example2: x = -123, return -321
     */
    public static int reverse(int x) {

        long num = (long) x;
        long temp=Math.abs(num);
        if (temp == 0) {
            return 0;
        }
        StringBuilder sb=new StringBuilder();
        while (temp >= 10) {
            sb.append(temp % 10);
            temp=temp / 10;
        }
        sb.append(temp % 10);
        //翻转可能会出现溢出的问题
        long resultInt=Long.valueOf(sb.toString());
        if (resultInt > Integer.MAX_VALUE) {
            return  0;
        }

        int result  = (int )resultInt ;
        return x > 0 ? result : 0 - result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值