1 计算正整数的算术平方根
/** * 正整数算术平方根 * @param n * @return */ public static double sqrt(int n) throws Exception{ if (n <= 0) { throw new Exception("illegal input paramter,n=" + n); } //二分查找的思想查找最接近的整数 int left = 0 ; int right = n / 2; int mid = (left + right) / 2 ; while (true) { if (left >= right) { break; } if (mid * mid < n) { left = mid + 1; mid = (left + right) / 2; } else if (mid * mid == n) { break; } else { right = mid - 1; mid = (left + right) / 2; } } double precision = 1; double result = Double.valueOf(mid); while (true) { if (precision == 0.0001) { break; } precision = precision / 10; for (int j = 1 ; j < 10 ; j++) { if (result * result == n) { return result; } else if (result * result > n) { result = result - precision; } else { result = result + precision; } } } return result; }
2 斐波那契数列算法优化
/** * 斐波那契数列 * @param n * @return */ public static long fib(long n){ // n = 45 the result is =1836311903,cost time : 5856ms // n > 45耗时过长以至于无法忍受 // if (n == 0 || n == 1) { // return 1l; // } // return fib(n - 1) + fib(n - 2); // n=100000,the result is =-4040291346873926563,cost time : 36ms Map<Long , Long> map = new HashMap<Long, Long>(); map.put(0l , 1l); map.put(1l , 1l); for (long i = 2 ; i <= n ; i++) { map.put(Long.valueOf(i) , map.get(Long.valueOf(i - 1)) + map.get(Long.valueOf(i - 2))); } return map.get(n); }
3 子数列最大和 ,动态规划,该算法用到了最佳子结构
/** * 子数列最大和 * @param a * @return */ public static int maxSubArray(int[] a){ if (null == a) { return -1; } if (a.length == 1) { return a[0]; } int[] dp = new int[a.length]; for (int i = 0 ; i < a.length ; i++) { dp[i] = a[i]; } int max = a[0]; for (int i = 1 ; i < a.length ; i++) { if (dp[i - 1] > 0) { dp[i] = dp[i - 1] + a[i]; } else { dp[i] = a[i]; } max = Math.max(max, dp[i]); } return max; }
4 回文序列:
/** * 回文序列 * @param str * @return */ public static String palindromeStr(String str){ if (Strings.isNullOrEmpty(str)) { return null; } if (str.length() == 1) { return str; } char[] chars = str.toCharArray(); String result = String.valueOf(chars[0]); String temp = null; for (int i = 0 ; i < chars.length ; i++) { for (int j = chars.length - 1 ; j > i; j--) { if (chars[i] == chars[j]) { temp = subPalindromeStr(String.valueOf(chars, i, j - i + 1)); if (Strings.isNullOrEmpty(temp)) { continue; } if (temp.length() > result.length()) { result = temp; } } } } return result; } /** * 判断是否回文序列,如果是返回串,否则返回null * @param str * @return */ private static String subPalindromeStr(String str){ char[] chars = str.toCharArray(); for (int i = 0 ; i < chars.length / 2 ; i++) { if (chars[i] != chars[chars.length - i - 1]) { return null; } } return String.valueOf(chars); }
5 单向链表,删除倒数第n(n >= 2)个节点
public static Node removeLastNNode(int n) { Node head = buildLinedList(); Node currentNode = head; Node nextN = null; while (true) { nextN = currentNode.getNext(); for (int i = 0 ; i < n; i++) { nextN = nextN.getNext(); } if (nextN == null) { currentNode.setNext(currentNode.getNext().getNext()); break; } currentNode = currentNode.getNext(); } return head; }
6 给定一个整数,判断整数各位数是否回文序列
/** * 整数是否回文序列 * @param num * @return */ public static boolean isPalindrome(int num) { if (num < 0) { return false; } if (num == 0) { return true; } // reverse // int result = 0; // int source = num; // while (num != 0) { // result = result * 10 + num % 10 ; // num = num / 10; // } // if (source == result) { // return true; // } // return false; // 比较最高位和最低位的值是否相等 int div = 1; while ((num / div) >= 10) { div *= 10; } int l; int r; while (num > 0) { l = num / div; r = num % 10; if (l != r) { return false; } num = (num % div) / 10; div /= 100; } return true; }