欢迎使用CSDN-markdown编辑器
来源:互联网 发布:mathematics是什么软件 编辑:程序博客网 时间:2024/06/16 04:29
package com.jason.test;import java.awt.Font;import java.math.BigInteger;import java.util.ArrayDeque;import java.util.ArrayList;import java.util.Collections;import java.util.HashMap;import java.util.LinkedList;import java.util.Map;import java.util.Map.Entry;import java.util.Queue;import java.util.Stack;import javax.print.attribute.standard.Sides;class ListNode { int val; ListNode next; public ListNode(int val) { this.val = val; }}class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}public class Solution { public boolean Find1(int[][] array, int target) { if (array == null) return false; boolean found = false; int rows = 0; int cols = array[0].length; while (rows < array.length && cols > 0) { if (array[rows - 1][cols] == target) { found = true; break; } else if (array[rows][cols - 1] < target) { rows++; } else { cols--; } } return found; } public double Power(double base, int exponent) { if (equalZore(base) && exponent < 0) return 0.0; int absExponent = exponent < 0 ? -exponent : exponent; double result = PowerWithExpnent(base, absExponent); if (exponent < 0) { return 1.0 / result; } return result; } public boolean equalZore(double number) { if (number < 0.00000001 && number > -0.00000001) return true; return false; } public double PowerWithExpnent(double base, int exponent) { if (exponent == 0) return 1; if (exponent == 1) return base; double result = PowerWithExpnent(base, exponent); result *= result; if ((exponent & 1) == 1) { result *= base; } return result; } public ListNode Merge(ListNode list1, ListNode list2) { if (list1 == null) return list2; if (list2 == null) return list1; ListNode mergeHead = null; if (list1.val < list2.val) { mergeHead = list1; mergeHead.next = Merge(list1.next, list2); } else { mergeHead = list2; mergeHead.next = Merge(list1, list2.next); } return mergeHead; } public ListNode ReverseList(ListNode head) { if (head == null) return null; ListNode newHead = null; ListNode p = head; ListNode q = p; while (p != null && q != null) { q = p.next; p.next = newHead; newHead = p; p = q; } if (p != null) { p.next = newHead; newHead = p; } return newHead; } public ArrayList<Integer> printMatrix(int[][] matrix) { if (matrix == null) return null; int col = matrix[0].length - 1; int row = matrix.length - 1; int c = 0; int r = 0; ArrayList<Integer> list = new ArrayList<Integer>((col + 1) * (row + 1)); while (col >= c && row >= r) { if (col > c) { for (int i = c; i <= col; i++) { list.add(matrix[r][i]); } r++; } if (row > r) { for (int i = r; i <= row; i++) { list.add(matrix[i][col]); } col--; } if (col > c && row > r) { for (int i = col; i >= c; i--) { list.add(matrix[row][i]); } row--; } if (row > r && col >= c) { for (int i = row; i >= r; i--) { list.add(matrix[i][c]); } c++; } } return list; } Stack<Integer> stack = new Stack<Integer>(); Stack<Integer> stackMin = new Stack<Integer>(); public void push(int node) { if (stack.isEmpty()) { stackMin.push(node); } else { if (stackMin.peek() > node) { stackMin.push(node); } else { stackMin.push(stackMin.peek()); } } stack.push(node); } public void pop() { stack.pop(); stackMin.pop(); } public int top() { return stack.peek(); } public int min() { return stackMin.peek(); } public boolean isPalindrome(ListNode pHead) { if (pHead == null) return false; boolean result = true; Stack<ListNode> stack = new Stack<ListNode>(); ListNode p = pHead; ListNode q = pHead.next; stack.push(p); while (q != null) { p = p.next; q = q.next; if (q != null) { if (q.next != null) { stack.push(p); q = q.next; } else { p = p.next; break; } } else break; } while (!stack.isEmpty()) { if (stack.pop().val != p.val) { result = false; break; } p = p.next; } return result; } public ListNode plusAB(ListNode a, ListNode b) { if (a == null) return b; if (b == null) return a; int nTakeOver = 0; int nSum = 0; ListNode result = a; ListNode tail = null; while (a != null && b != null) { nSum = a.val + b.val + nTakeOver; if (nSum >= 10) { nSum -= 10; nTakeOver = 1; } else { nTakeOver = 0; } a.val = nSum; tail = a; a = a.next; b = b.next; } while (a != null) { nSum = a.val + nTakeOver; if (nSum >= 10) { nSum -= 10; nTakeOver = 1; a.val = nSum; tail = a; a = a.next; } else { nTakeOver = 0; break; } } while (b != null) { nSum = b.val + nTakeOver; if (nSum >= 10) { nSum -= 10; nTakeOver = 1; b.val = nSum; a = b; tail = a; b = b.next; } else { b.val = nSum; tail.next = b; nTakeOver = 0; break; } } if (nTakeOver == 1) tail.next = new ListNode(1); return result; } public String reverseString(String iniString) { if (iniString == null) return null; char[] array = new char[iniString.length()]; iniString.getChars(0, iniString.length(), array, 0); char temp; for (int i = 0; i < array.length / 2; i++) { temp = array[i]; array[i] = array[array.length - 1 - i]; array[array.length - 1 - i] = temp; } return new String(array); } public String ReverseSentence(String str) { if (str == null) return null; if (str.length() <= 1) return str; char[] array = new char[str.length()]; str.getChars(0, array.length, array, 0); Reverse(array, 0, array.length); int j = 0; for (int i = 0; i < array.length; i++) { if (array[i] == ' ' || i == array.length - 1) { if (i != 0) { Reverse(array, j, i - 1); j = i + 1; } } } return new String(array); } public void Reverse(char[] array, int s, int e) { for (int i = s; i < (s + e) / 2; i++) { char temp = array[i]; array[i] = array[e - i - 1]; array[e - i - 1] = temp; } } public int[][] transformImage(int[][] mat, int n) { if (mat == null) return null; int i = 1; for (int index = 0; index < n; index++) { while (i < index) { swap(mat, i, index, index, i); i++; } i = 0; } return mat; } private void swap(int[][] mat, int row1, int col1, int row2, int col2) { int temp = mat[row1][col1]; mat[row1][col1] = mat[row2][col2]; mat[row2][col2] = temp; } public String replaceSpace(String iniString, int length) { if (iniString == null) return null; if (length <= 0) return ""; int count = 0; for (int i = 0; i < length; i++) { if (iniString.charAt(i) == ' ') { count++; } } char[] array = new char[length + 2 * count]; iniString.getChars(0, length, array, 0); for (int i = length - 1, j = array.length - 1; i >= 0; i--) { if (array[i] == ' ') { array[j--] = '0'; array[j--] = '2'; array[j--] = '%'; } else { array[j--] = array[i]; } } return new String(array); } public ArrayList<Integer> printListFromTailToHead(ListNode listNode) { ArrayList<Integer> list = new ArrayList<Integer>(0); if (listNode == null) return list; ListNode p = listNode; while (p != null) { list.add(p.val); p = p.next; } if (list.size() == 0) return list; int temp; for (int i = 0; i < list.size() / 2; i++) { temp = list.get(i); list.set(i, list.get(list.size() - i - 1)); list.set(list.size() - i - 1, temp); } return list; } public boolean checkDifferent(String iniString) throws Exception { if (iniString == null) throw new Exception("iniString can't be null"); boolean[] hash = new boolean[256]; for (int i = 0; i < 256; i++) { hash[i] = false; } for (int i = 0; i < iniString.length(); i++) { if (hash[iniString.charAt(i)]) return false; else hash[iniString.charAt(i)] = true; } return true; } public boolean checkSam(String stringA, String stringB) { if (stringA == null || stringB == null || stringA.length() != stringB.length()) return false; int hash[] = new int[256]; for (int i = 0; i < 256; i++) { hash[i] = 0; } for (int i = 0; i < stringA.length(); i++) { hash[stringA.charAt(i)]++; } for (int i = 0; i < stringB.length(); i++) { hash[stringB.charAt(i)]--; } for (int i = 0; i < 256; i++) { if (hash[i] != 0) return false; } return true; } public boolean removeNode(ListNode pNode) { if (pNode == null) return false; if (pNode.next == null) return false; ListNode toBeDeleted = pNode.next; pNode.val = toBeDeleted.val; pNode.next = toBeDeleted.next; toBeDeleted.next = null; return true; } public boolean isBalance(TreeNode root) { if (root == null) return true; if (isBalance(root.left) && isBalance(root.right)) return (treeHeight(root.left) - treeHeight(root.right) <= 1) && (treeHeight(root.right) - treeHeight(root.left)) >= -1; return false; } private int treeHeight(TreeNode root) { if (root == null) return 0; return treeHeight(root.left) > treeHeight(root.right) ? treeHeight(root.left) + 1 : treeHeight(root.right) + 1; } public boolean Find(int[][] array, int target) { if (array == null) return false; int row = array.length - 1; int col = array[0].length - 1; int r = 0; int c = 0; while (r <= row && col >= 0) { if (array[r][col] == target) return true; else if (array[r][col] > target) col--; else r++; } return false; } public ListNode partition(ListNode pHead, int x) { // write code here if (pHead == null) return null; ListNode current = pHead; ListNode ls = pHead; ListNode pre = null; ListNode smallHead = null; ListNode smallTail = null; while (current != null) { if (current.val < x) { smallHead = current; smallTail = current; break; } else { ls = current; } pre = current; current = current.next; } if (smallHead == null || pre == null) return pHead; while (current != null) { if (current.val < x) { pre.next = current.next; smallTail.next = current; smallTail = smallTail.next; current = pre.next; } else { pre = pre.next; current = current.next; } } smallTail.next = pHead; return smallHead; } public ListNode createList(String iniString) { if (iniString == null) return null; ListNode fnode = null; ListNode tnode = null; String[] array = iniString.split(","); for (String value : array) { ListNode node = new ListNode(Integer.valueOf(value)); if (fnode == null) { fnode = node; tnode = fnode; } else { tnode.next = node; tnode = tnode.next; } } return fnode; } public boolean listAEqualB(ListNode la, ListNode lb) { if (la == null) return lb == null; if (lb == null) return la == null; return false; } public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) { ArrayList<Integer> result = new ArrayList<Integer>(); if (input == null || input.length == 0) return result; if (k > input.length) return result; quickSort(input, 0, input.length - 1); for (int i = 0; i < k; i++) { result.add(input[i]); } return result; } public void quickSort(int[] input, int low, int high) { if (low < high) { int pivot = partition(input, low, high); quickSort(input, low, pivot - 1); quickSort(input, pivot + 1, high); } } public int partition(int[] input, int low, int high) { int pivot = input[low]; while (low < high) { while (low < high && pivot <= input[high]) high--; input[low] = input[high]; while (low < high && pivot >= input[low]) low++; input[high] = input[low]; } input[low] = pivot; return low; } public int FirstNotRepeatingChar(String str) { if (str == null || str.length() == 0) return -1; str = str.toUpperCase(); int[] hash = new int[26]; for (int i = 0; i < str.length(); i++) { hash[str.charAt(i) - 'A']++; } for (int i = 0; i < hash.length; i++) { if (hash[i] == 1) return i; } return -1; } public ListNode Merge2(ListNode list1, ListNode list2) { if (list1 == null) return list2; if (list2 == null) return list1; ListNode p1 = list1; ListNode p2 = list2; ListNode head = p1.val < p2.val ? p1 : p2; ListNode tail = head; while (p1 != null && p2 != null) { if (p1.val < p2.val) { tail.next = p1; tail = tail.next; p1 = p1.next; } else { tail.next = p2; tail = tail.next; p2 = p2.next; } } if (p1 == null) tail.next = p2; if (p2 == null) tail.next = p1; return head; } public void Mirror2(TreeNode root) { if (root == null) return; TreeNode temp = root.left; root.left = root.right; root.right = temp; Mirror(root.left); Mirror(root.right); } public void Mirror(TreeNode root) { if (root == null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); stack.push(root); TreeNode temp = null; while (!stack.isEmpty()) { TreeNode treeNode = stack.pop(); if (treeNode != null) { temp = treeNode.left; treeNode.left = treeNode.right; treeNode.right = temp; stack.push(treeNode.left); stack.push(treeNode.right); } } } /** * 对传入数组重新排序,使得奇数排在偶数的前面 测试用例: [1,2,3,4,5,6,7] 对应输出应该为: [1,3,5,7,2,4,6] * * @param array * 待排数组 */ public void reOrderArrayWithRelativePositinChangable(int[] array) { if (array == null) return; int low = 0; int high = array.length - 1; while (low < high) { while (low < high && (array[low] & 1) == 1) low++; while (low < high && (array[high] & 1) == 0) high--; int temp = array[high]; array[high] = array[low]; array[low] = temp; low++; high--; } } public void reOrderArray(int[] array) { if (array == null) return; int low = 0; int high = 0; for (; low < array.length && (array[low] & 1) == 1; low++) ; if (low == array.length) return; high = low + 1; while (high < array.length) { if ((array[high] & 1) == 1) { int temp = array[high]; for (int i = high; i > low; i--) { array[i] = array[i - 1]; } array[low] = temp; low = low + 1; } high++; } } public int calcCost(int A, int B) { // write code here int count = 0; if (A > B) { while (A != 0) { if (((A & 1) == 1 & (B & 1) == 1) || ((A & 1) == 1 & (B & 1) == 0)) { } else { count++; } A = A >> 1; B = B >> 1; } } else { while (B != 0) { if (((A & 1) == 1 & (B & 1) == 1) || ((A & 1) == 1 & (B & 1) == 0)) { } else { count++; } A = A >> 1; B = B >> 1; } } return count; } public int findKth(int k) { // write code here if (k <= 0) return 0; int[] numbers = new int[k + 1]; numbers[0] = 1; int n3 = 0; int n5 = 0; int n7 = 0; int index = 1; while (index < k + 1) { int min = min(numbers[n3] * 3, numbers[n5] * 5, numbers[n7] * 7); numbers[index] = min; while (numbers[n3] * 3 <= min) n3++; while (numbers[n5] * 5 <= min) n5++; while (numbers[n7] * 7 <= min) n7++; index++; } int result = numbers[index - 1]; numbers = null; return result; } private int min(int i, int j, int k) { int temp = i < j ? i : j; return temp < k ? temp : k; } public double antsCollision(int n) { if (n < 3) return 1; return 1 - (double) (1 / Math.pow(2, n - 1)); } public ArrayList<ArrayList<Integer>> getSubsets(int[] A, int n) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if (n <= 0) return result; if (A == null) return result; for (int i = 0; i < n; i++) { // i标示趟数 for (int j = i + 1, start = 0; j < n; j++) { System.out.println(j); } } return result; } /*********** 堆排序 ************/ /** * 堆调整 * * @param numbers * @param s * 待调父节点 * @param m * 调整位置 */ public void heapAdjust(int[] numbers, int s, int m) { int rc = numbers[s]; for (int i = 2 * s; i <= m; i = i * 2) { if (i < m && numbers[i] < numbers[i + 1]) i++; if (rc >= numbers[i]) break; numbers[s] = numbers[i]; s = i; } numbers[s] = rc; } public void heapSort(int[] numbers) { if (numbers == null) return; for (int i = numbers.length / 2; i >= 0; i--) heapAdjust(numbers, i, numbers.length - 1);// for (int j = numbers.length - 1; j >0; j--) { int temp = numbers[0]; numbers[0] = numbers[j]; numbers[j] = temp; heapAdjust(numbers, 0, j - 1); } } /************* 堆排序算法end ****************/ /*********** KMP算法 *************/ /** * KMP字符串匹配 * * @param str * 母串 * @param match * 字串 * @return 匹配子字串首字符在母串中的下标 */ public static int KMPMatch(String str, String match) { if (str == null || match == null || str.length() < match.length()) return -1; int[] next = getNextArray(match); int end = 0; int index = 0; while (index < str.length() && end < match.length()) { if (str.charAt(index) == match.charAt(end)) { index++; end++; } else if (next[end] == -1) { index++; } else { end = next[end]; } } return end == match.length() ? index - end : -1; } /** * 生成next数组 * * @param match * 字串 * @return next数组,记录字串中i位置的前部分串的相同的前缀和后缀的最大长度 */ private static int[] getNextArray(String match) { if (match == null) return null; if ("".equals(match)) return new int[0]; int[] next = new int[match.length()]; next[0] = -1; next[1] = 0; int index = 2; int cn = 0; while (index < next.length) { if (match.charAt(index - 1) == match.charAt(cn)) { next[index++] = ++cn; } else { if (cn > 0) { cn = next[cn]; } else { next[index++] = 0; } } } return next; } /************* KMP算法end ****************/ /************* 2-路归并排序 **************/ /** * 2-路归并排序 将数组从i~m与m+1~n合并排序 * * @param sArray * 数据源数组(原数组) * @param tArray * 临时暂存数组 * @param begin * @param mid * @param end */ public static void twoRoadMerge(int[] sArray, int[] tArray, int begin, int mid, int end) { int start = begin; int j = mid + 1; int k = begin; while (j <= end && begin <= mid) { if (sArray[begin] <= sArray[j]) { tArray[k] = sArray[begin++]; } else { tArray[k] = sArray[j++]; } k++; } while (begin <= mid) { tArray[k++] = sArray[begin++]; } while (j <= end) { tArray[k++] = sArray[j++]; } // 将已经排序的片段复制到原数组中 while (start <= end) { sArray[start] = tArray[start++]; } } public static void merge(int[] sArray, int[] tArray, int s, int t) { if (s == t) { return; } int mid = (s + t) / 2; merge(sArray, tArray, s, mid); merge(sArray, tArray, mid + 1, t); twoRoadMerge(sArray, tArray, s, mid, t); } public static void mergeSort(int sArray[]) { int[] tArray = new int[sArray.length]; merge(sArray, tArray, 0, sArray.length - 1); } /************ 2-路归并排序end **************/ /************* 素数伴侣 ***************/ /** * 判断a是否为素数 * * @param a * @return */ public boolean isPrime(int a) { if (a < 2) {// 素数不小于2 return false; } else { for (int i = 2; i <= Math.sqrt(a); i++) { if (a % i == 0) {// 若能被整除,则说明不是素数,返回false return false; } } } return true; } public int getPrimePartnerCount(int[] numbers) { int[] dp = new int[numbers.length + 1]; int current = 0; for (int i = numbers.length - 2; i >= 0; i--) { for (int j = numbers.length - 1; j > i; j--) { int sum = numbers[i] + numbers[j]; if (sum % 6 == 1 || sum % 6 == 5 || sum == 2 || sum == 3) { if (isPrime(numbers[i] + numbers[j])) { current = dp[i + 1] - dp[j - 1] + dp[j + 1] + 1; } else { current = dp[i + 1]; } } if (current > dp[i]) dp[i] = current; } } return dp[0]; } /************* 素数伴侣end ***************/ /********* 查找数组中最小未出现的正整数 ***********/ public int getSmallestMissingPositiveNum(int[] numbers) { int l = 0; int r = numbers.length; while (l < r) { if (numbers[l] == l + 1) { l++; } else if (numbers[l] <= l || numbers[l] > r || numbers[numbers[l] - 1] == l) { numbers[l] = numbers[--r]; } else { swap(numbers, l, numbers[l] - 1); } } return l + 1; } public void swap(int[] numbers, int i, int j) { int temp = numbers[i]; numbers[i] = numbers[j]; numbers[j] = temp; } /******** 查找数组中最小未出现的正整数end **********/ /************** 旋转数组的最小数字 **************/ /** * @author jason 旋转数组的最小数字 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。 * 输入一个非递减序列的一个旋转,输出旋转数组的最小元素。 例如数组{3,4,5,1, * 2}为{1,2,3,4,5}的一个旋转,该数组的最小值为1。 * * @param array * @return * @throws Exception */ public int minNumberInRotateArray(int[] array) throws Exception { if (array == null) throw new Exception("array can't be null"); if (array.length == 0) return 0; int low = 0; int high = array.length - 1; int mid = 0; while (array[low] >= array[high]) { if (high - low == 1) { mid = high; break; } mid = (low + high) / 2; if (array[mid] >= array[low]) { low = mid; } else if (array[mid] <= array[high]) { high = mid; } else if (array[mid] == array[low] && array[mid] == array[high]) { return minNumberInArray(array, low, high); } } return array[mid]; } public int minNumberInArray(int[] array, int low, int high) { int min = array[low]; for (int i = low + 1; i <= high; i++) { if (min > array[i]) min = array[i]; } return min; } /***************** 旋转数组的最小数字end *****************/ /*********** 最长递增数组 **************/ /** * 【题目】 给定数组arr,返回arr的最长递增子序列。 【举例】 * arr=[2,1,5,3,6,4,8,9,7],返回的最长递增子序列为[1,3,4,8,9]。 * 【要求】如果arr长度为N,请实现时间复杂度为O(N*logN)的方法。 */ public int[] getLongestIncreasingSeq(int[] array) { if (array == null || array.length == 0) return null; int[] dp = getDpArray(array); return generateResult(array, dp); } /** * @param array * @param dp * @return */ private int[] generateResult(int[] array, int[] dp) { int index = 0; int length = 0; for (int i = 0; i < array.length; i++) { if (dp[i] > length) { length = dp[i]; index = i; } } int[] result = new int[length]; result[--length] = array[index]; for (int i = index; i >= 0; i--) { if (array[i] < array[index] && dp[i] == dp[index] - 1) { result[--length] = array[i]; index = i; } } return result; } /** * 生成dp数组 dp[i]表示数组0~i位置中最长递增序列的长度 * * @param array * @return */ private int[] getDpArray(int[] array) { int[] dp = new int[array.length]; // ends[i]表示最长递增序列长度为i+1的序列末尾的最小值 int[] ends = new int[array.length]; ends[0] = 0; dp[0] = 1; int low = 0; int high = 0; int rightBoundary = 0;// 数组右边界,为0表示数组为空 int mid = 0; for (int i = 0; i < array.length; i++) { low = 0; high = rightBoundary; // 二分查找ends数组中第一个比array[i]大的值 while (low <= high) { mid = (low + high) / 2; if (array[i] > array[mid]) { low = mid + 1; } else { high = mid - 1; } } rightBoundary = Math.max(rightBoundary, low); // 更新ends[low]的值 ends[low] = array[i]; // 设置dp[i]为low+1; dp[i] = low + 1; } return dp; } /*********** 最大递增数组的长度end **************/ /************ 判断二叉树是否为二叉查找树 **************/ public boolean checkBST(TreeNode root) { if (root == null) return false; if (root.left != null) if (root.left.val > root.val) return false; else checkBST(root.left); if (root.right != null) if (root.right.val < root.val) return false; else checkBST(root.right); return true; } /************ 判断二叉树是否为二叉查找树end **************/ public ListNode getTreeLevel(TreeNode root, int dep) { if (root == null || dep <= 0) return null; Queue<TreeNode> nodesQueue = new ArrayDeque<TreeNode>(); nodesQueue.add(root); int nCount = 1; int current = 0; int nextLevelCount = 0; int level = 1; TreeNode treeNode = null; boolean isFirst = true; ListNode head = null; ListNode tail = head; while (!nodesQueue.isEmpty()) { treeNode = nodesQueue.poll(); current++; if (level == dep) { if (isFirst) { head = new ListNode(treeNode.val); tail = head; isFirst = false; } else { ListNode newNode = new ListNode(treeNode.val); tail.next = newNode; tail = newNode; } } if (level > dep) break; if (treeNode.left != null) { nodesQueue.add(treeNode.left); nextLevelCount++; } if (treeNode.right != null) { nodesQueue.add(treeNode.right); nextLevelCount++; } if (current == nCount) { level++; nCount = nextLevelCount; nextLevelCount = 0; current = 0; } } return head; } public void midOrder(TreeNode root, int p, int[] numbers, int index) { if (root == null) return; midOrder(root.left, p, numbers, ++index); numbers[index] = root.val; midOrder(root.right, p, numbers, ++index); } /*** * 创建完全二叉树 * * @param root * @param numbers */ public void createBinaryTree(TreeNode root, int[] numbers) { if (root == null || numbers == null || numbers.length == 0) return; Queue<TreeNode> nodeQueue = new ArrayDeque<TreeNode>(); nodeQueue.add(root); int count = 1; while (!nodeQueue.isEmpty() && count <= numbers.length / 2) { TreeNode node = nodeQueue.poll(); TreeNode left = new TreeNode(numbers[count * 2 - 1]); node.left = left; nodeQueue.add(left); TreeNode right = new TreeNode(numbers[count * 2]); node.right = right; nodeQueue.add(right); count++; } } /** * 对于一个元素各不相同且按升序排列的有序序列, 请编写一个算法,创建一棵高度最小的二叉查找树。 给定一个有序序列int[] * vals,请返回创建的二叉查找树的高度。 * * @param vals * @return */ public int buildMinimalBST(int[] vals) { return 0; } /** * * @param n * @return */ public int countWays(int n) { if (n <= 0) return 0; if (n == 1) return 1; if (n == 2) return 2; if (n == 3) return 4; int fb1 = 1; int fb2 = 2; int fb3 = 4; int fbn = 0; int i = 4; while (i <= n) { fbn = ((fb1 + fb2) % 1000000007 + fb3) % 1000000007; fb1 = fb2; fb2 = fb3; fb3 = fbn; i++; } return fbn; } /** * * @param A * @param guess * @return */ public int[] calcResult(String A, String guess) { if (A == null || A.length() == 0 || guess == null || guess.length() == 0 || A.length() != guess.length()) return null; Map<Character, Integer> sourceMap = new HashMap<Character, Integer>(); Map<Character, Integer> guessMap = new HashMap<Character, Integer>(); int[] result = new int[2]; result[0] = 0; result[1] = 0; for (int i = 0; i < A.length(); ++i) { if (A.charAt(i) == guess.charAt(i)) { result[0]++; } else { sourceMap.put( A.charAt(i), sourceMap.get(A.charAt(i)) == null ? 1 : sourceMap .get(A.charAt(i)) + 1); guessMap.put( guess.charAt(i), guessMap.get(guess.charAt(i)) == null ? 1 : guessMap .get(guess.charAt(i)) + 1); } } for (Character key : sourceMap.keySet()) { int sourceCount = sourceMap.get(key) == null ? 0 : sourceMap .get(key); int guessCount = guessMap.get(key) == null ? 0 : guessMap.get(key); if (sourceCount <= guessCount) { result[1] += sourceCount; } else { result[1] += guessCount; } } return result; } /** * * @param n * @return */ public int getFactorSuffixZero(int n) { if (n <= 0) return 0; int zeroCnt = 0; int multipleOf5 = 5; while (n >= multipleOf5) { zeroCnt += n / multipleOf5; multipleOf5 *= 5; } return zeroCnt; } /** * 有一个整数数组,请编写一个函数,找出索引m和n,<br> * 只要将m和n之间的元素排好序,整个数组就是有序的。<br> * 注意:n-m应该越小越好,也就是说, 找出符合条件的最短序列。<br> * 给定一个int数组A和数组的大小n,请返回一个二元组,代表所求序列的起点和终点。<br> * (原序列位置从0开始标号,若原序列有序,返回 [0,0])。保证A中元素均为正整数。<br> * 测试样例: [1,4,6,5,9,10],6 返回:[2,3] * * @param A * @param n * @return */ public int[] findSegment(int[] A, int n) { if (A == null || A.length == 0 || n != A.length) return null; int[] result = new int[2]; int N = 0; int M = 0; int max = A[0]; int min = A[n - 1]; for (int i = 0; i < n; i++) { if (A[i] >= max) { max = A[i]; } else { N = i; } } for (int j = n - 1; j >= 0; j--) { if (A[j] <= min) { min = A[j]; } else { M = j; } } result[0] = M; result[1] = N; return result; } public static BigInteger getTheKthFB(int k) { int result[] = {1,1}; if(k<=0) return new BigInteger("0"); if(k<=2) return new BigInteger(result[k-1]+""); BigInteger fb1 = new BigInteger(1+""); BigInteger fb2 = new BigInteger(1+""); BigInteger fbk = fb1.add(fb2); for(int i = 2;i<k;i++){ fbk = fb1.add(fb2); fb2 = fb1; fb1 = fbk; } return fbk; } /** * * @param article * @param n * @param word * @return */ public int getFrequency(String[] article, int n, String word) { if (article == null || word == null || "".equals(word) || article.length != n) return 0; Map<String, Integer> countMap = new HashMap<String, Integer>(); for (String str : article) { countMap.put(str, countMap.get(str) == null ? 1 : countMap.get(str) + 1); } return countMap.get(word) == null ? 0 : countMap.get(word); } /** * * @param A * @param n * @param sum * @return */ public int countPairs(int[] A, int n, int sum) { if (A == null || A.length != n) return 0; quickSort(A, 0, n - 1); return countPairsInOrderedArray(A, sum); } /** * * @param A * @param n * @param sum * @return */ public int countPairsInOrderedArray(int[] A, int sum) { int low = 0; int high = A.length - 1; int count = 0; int countLow = 1; int countHigh = 1; while (low < high) { if (A[low] + A[high] == sum) { if (A[low] == A[high]) { count = count + (high - low + 1) * (high - low) / 2; break; } else { for (int i = low + 1; i < high; i++) { if (A[low] == A[i]) countLow++; } for (int j = high - 1; j > low; j--) { if (A[j] == A[high] && A[low] != A[high]) countHigh++; } count += countLow * countHigh; low = low + countLow; high = high - countHigh; countLow = 1; countHigh = 1; } } else if (A[low] + A[high] > sum) high--; else low++; } return count; } public void permutation(int numbers[]){ if(numbers == null) return ; permutation(numbers,0); } private void permutation(int[] numbers, int begin) { if(begin == numbers.length-1){ for(int i = 0 ;i< numbers.length;i++){ System.out.print(numbers[i]); } System.out.println(); } else{ for(int j = begin;j<numbers.length;j++){ swap(numbers, begin, j); permutation(numbers, begin+1); swap(numbers, begin, j); } } } public String serialTree(TreeNode root) { if (root == null) return "$,"; String str = root.val + ","; str += serialTree(root.left); return str += serialTree(root.right); } public void deSerialTree(TreeNode root,String str,int len){ } public boolean HasSubtree(TreeNode root1, TreeNode root2) { if(root1 == null|| root2 == null) return false; String tree1Str = serialTree(root1); tree1Str = tree1Str.substring(0, tree1Str.length()-2); String tree2Str = serialTree(root1); tree2Str = tree2Str.substring(0, tree2Str.length()-2); if(serialTree(root1).contains(tree2Str)) return true; return false; } public void reverseStack(Stack<Integer> stack) { if (stack.isEmpty()) return; int top = stack.pop(); reverseStack(stack); stack.push(top); } public Stack<Integer> createStackFromArray(int[] numbers) { if (numbers == null) return null; Stack<Integer> result = new Stack<Integer>(); for (int i = 0; i < numbers.length; ++i) { result.push(numbers[i]); } return result; } public static void main(String[] args) throws Exception { Solution solution = new Solution(); int[] numbers = {1,2,3 }; solution.permutation(numbers); } private void print(int[] numbers){ if(numbers == null) return ; for(int i :numbers){ System.out.println(i); } } public void change(String str, char ch[]) { str = "test ok"; ch[0] = 'g'; }}
0 0
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- 欢迎使用CSDN-markdown编辑器
- python处理文件
- 打开fiddler之后,电脑端浏览器无法访问页面
- 1.7 CentOS7 配置AMP环境之PHP
- clean code - 错误处理
- 2015/9/26(心情贴)
- 欢迎使用CSDN-markdown编辑器
- 编程之美--1.8--小飞的电梯调度问题--扩展问题--2--(M层电梯选择K层停靠)
- poj 3254 Corn Fields(状态压缩DP)
- iOS开发 ----- 网络请求1 ----- 概览
- iOS开发 ----- 网络请求2 ----- 请求数据
- iOS开发 ----- 网络请求3 ----- 上传数据
- iOS开发 ----- 网络请求4 ----- 下载数据以及断点续传
- 怎么根据LVDS的时序规格计算TTL的前肩后肩
- cocos-js 3.8 控件触摸事件处理