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;
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) {
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 ;
}
}
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(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;
}
}