
算法
zjj2015
这个作者很懒,什么都没留下…
展开
-
comparator comparable 区别
1. Comparator 和 Comparable 相同的地方他们都是java的一个接口, 并且是用来对自定义的class比较大小的,什么是自定义class: 如 public class Person{ String name; int age }.当我们有这么一个personList,里面包含了person1, person2, persion3....., 我们用Collect转载 2015-11-06 22:10:28 · 215 阅读 · 0 评论 -
A B 数组合并
分析 原地算法 从后往前排序 即可 需要注意的异常: 把剩余的b元素 排入 a public void mergeSortedArray(int[] A, int m, int[] B, int n) { if (A == null || B == null) return; int index = m + n - 1; whil原创 2015-10-24 18:35:10 · 387 阅读 · 0 评论 -
Rotate String
Given a string and an offset, rotate string by offset. (rotate from left to right)Have you met this question in a real interview? Yes Example Given “abcdefg”.offset=0 => “abcdefg” offset=1 => “gabcd原创 2015-11-23 14:18:26 · 689 阅读 · 0 评论 -
gray code
学习格雷码 的规律“`java public class Solution {public ArrayList<Integer> grayCode(int n) { ArrayList<Integer> result = new ArrayList<Integer>(); for (int i = 0; i < Math.pow(2,n); ++i) { result.原创 2015-11-22 19:41:24 · 227 阅读 · 0 评论 -
Largest Number
Given a list of non negative integers, arrange them such that they form the largest number.Have you met this question in a real interview? Yes Example Given [1, 20, 23, 4, 8], the largest formed numb原创 2015-11-22 19:42:57 · 202 阅读 · 0 评论 -
Huffman树
哈夫曼(HUFFMAN)树和哈夫曼编码。哈夫曼编码是哈夫曼树的一个应用。哈夫曼编码应用广泛,如JPEG中就应用了哈夫曼编码。 首先介绍什么是哈夫曼树。哈夫曼树又称最优二叉树,是一种带权路径长度最短的二叉树。所谓树的带权路径长度,就是树中所有的叶结点的权值乘上其到根结点的 路径长度(若根结点为0层,叶结点到根结点的路径长度为叶结点的层数)。树的带权路径长度记为转载 2015-11-16 23:48:51 · 258 阅读 · 0 评论 -
Fizz Buzz
Given number n. Print number from 1 to n. But:when number is divided by 3, print “fizz”. when number is divided by 5, print “buzz”. when number is divided by both 3 and 5, print “fizz buzz”. Have yo原创 2015-11-22 19:50:19 · 343 阅读 · 0 评论 -
Reverse Words in a String
Given an input string, reverse the string word by word.For example, Given s = “the sky is blue”, return “blue is sky the”.split ()函数分割public class Solution { public String reverseWords(String s)原创 2015-11-23 14:57:41 · 194 阅读 · 0 评论 -
Single Number II
Given 3*n + 1 numbers, every numbers occurs triple times except one, find it.Example Given [1,1,2,3,3,3,2,2,4,1] return 4Challenge One-pass, constant extra space.public class Solution { /**原创 2015-11-22 19:45:46 · 205 阅读 · 0 评论 -
cosineSimilarity
Cosine similarity is a measure of similarity between two vectors of an inner product space that measures the cosine of the angle between them. The cosine of 0° is 1, and it is less than 1 for any other转载 2015-11-22 19:07:08 · 1969 阅读 · 0 评论 -
strStr
For a given source string and a target string, you should output the first index(from 0) of target string in source string.If target does not exist in source, just return -1.Have you met this question原创 2015-11-22 19:49:01 · 211 阅读 · 0 评论 -
Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.Have you met this question in a real interview? Yes Example “A man, a plan, a canal: Panam原创 2015-11-23 14:58:29 · 181 阅读 · 0 评论 -
Two Strings Are Anagrams
分析统计词频即可以区分出两个字符串是否为变位词强制类型转换 为关键点charAt() 函数的使用…public class Solution { /** * @param s: The first string * @param b: The second string * @return true or false */ public b转载 2015-11-22 18:34:35 · 297 阅读 · 0 评论 -
Merge Sorted Array
Given two sorted integer arrays A and B, merge B into A as one sorted array.Example A = [1, 2, 3, empty, empty], B = [4, 5]After merge, A will be filled as [1, 2, 3, 4, 5]Note You may assume that A h原创 2015-11-22 19:47:10 · 218 阅读 · 0 评论 -
Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes’ values.Have you met this question in a real interview? Yes Example Given binary tree {1,#,2,3},1 \ 2 / 3return [1,3原创 2015-12-10 23:31:21 · 162 阅读 · 0 评论 -
Insert Node in a Binary Search Tree
Given a binary search tree and a new tree node, insert the node into the tree. You should keep the tree still be a valid binary search tree.Have you met this question in a real interview? Yes Example原创 2015-12-10 23:58:40 · 228 阅读 · 0 评论 -
Subsets -------bit
Given a set of distinct integers, return all possible subsets.Have you met this question in a real interview? Yes Example If S = [1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3],原创 2015-12-11 23:12:53 · 188 阅读 · 0 评论 -
Triangle ---DP
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.Have you met this question in a real interview? Yes Example For example, gi原创 2015-12-12 09:46:55 · 192 阅读 · 0 评论 -
Subsets------DFS
Given a set of distinct integers, return all possible subsets.Have you met this question in a real interview? Yes Example If S = [1,2,3], a solution is:[ [3], [1], [2], [1,2,3], [1,3],原创 2015-12-11 22:48:20 · 199 阅读 · 0 评论 -
Find the Connected Component in the Undirected Graph
Find the number connected component in the undirected graph. Each node in the graph contains a label and a list of its neighbors. (a connected component (or just component) of an undirected graph is a原创 2015-12-12 11:45:22 · 314 阅读 · 0 评论 -
Compare Strings
Compare two strings A and B, determine whether A contains all of the characters in B.The characters in string A and B are all Upper Case letters.Have you met this question in a real interview? Yes Exa原创 2015-12-12 11:57:06 · 229 阅读 · 0 评论 -
Anagrams
Given an array of strings, return all groups of strings that are anagrams.Have you met this question in a real interview? Yes Example Given [“lint”, “intl”, “inlt”, “code”], return [“lint”, “inlt”, “原创 2015-12-13 13:37:43 · 348 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.Have you met this question in a real interview? Yes原创 2015-12-14 23:32:10 · 193 阅读 · 0 评论 -
Find Minimum in Rotated Sorted Array II
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.The array may contain duplicates.Have you met this qu原创 2015-12-14 23:42:10 · 178 阅读 · 0 评论 -
Longest Palindromic Substring
最长回文子串暴力匹配public class Solution { /** * @param s input string * @return the longest palindromic substring */ public String longestPalindrome(String s) { // Write your cod原创 2015-11-28 20:34:06 · 178 阅读 · 0 评论 -
Space Replacement
Write a method to replace all spaces in a string with %20. The string is given in a characters array, you can assume it has enough space for replacement and you are given the true length of the string.原创 2015-11-28 20:44:57 · 205 阅读 · 0 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Example Given s = “Hello原创 2015-11-28 20:52:51 · 176 阅读 · 0 评论 -
Count and Say
The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111221, …1 is read off as “one 1” or 11.11 is read off as “two 1s” or 21.21 is read off as “one 2, then one原创 2015-11-29 13:45:25 · 189 阅读 · 0 评论 -
Remove Element
Given an array and a value, remove all occurrences of that value in place and return the new length.The order of elements can be changed, and the elements after the new length don’t matter.Have you met原创 2015-11-29 14:15:14 · 220 阅读 · 0 评论 -
Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ’ ‘, return the length of last word in the string.If the last word does not exist, return 0.Example Given s = “Hello原创 2015-11-28 21:12:50 · 158 阅读 · 0 评论 -
Subarray Sum
Given an integer array, find a subarray where the sum of numbers is zero. Your code should return the index of the first number and the index of the last number.Example Given [-3, 1, 2, -3, 4], return原创 2015-11-29 15:09:24 · 459 阅读 · 0 评论 -
Recover Rotated Sorted Array
Given a rotated sorted array, recover it to sorted array in-place.Have you met this question in a real interview? Yes Example [4, 5, 1, 2, 3] -> [1, 2, 3, 4, 5]Challenge In-place, O(1) extra space a原创 2015-11-29 15:30:35 · 352 阅读 · 0 评论 -
Partition Array
Given an array nums of integers and an int k, partition the array (i.e move the elements in “nums”) such that:All elements < k are moved to the left All elements >= k are moved to the right Return th原创 2015-11-29 16:26:38 · 225 阅读 · 0 评论 -
Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with cons原创 2015-11-29 17:02:53 · 177 阅读 · 0 评论 -
Merge Sorted Array II
Merge two given sorted integer array A and B into a new sorted integer array.Example A=[1,2,3,4]B=[2,4,5,6]return [1,2,2,3,4,4,5,6]class Solution { /** * @param A and B: sorted integer array原创 2015-11-29 21:12:30 · 193 阅读 · 0 评论 -
Update Bits
Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j)Have原创 2016-01-18 23:40:25 · 226 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
Follow up for “Remove Duplicates”: What if duplicates are allowed at most twice?For example, Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1,2,2,3].publ原创 2015-11-29 20:35:28 · 181 阅读 · 0 评论 -
Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the原创 2016-01-19 15:36:57 · 153 阅读 · 0 评论 -
Partition Array by Odd and Even
Partition an integers array into odd number first and even number second.Example Given [1, 2, 3, 4], return [1, 3, 2, 4]Challenge Do it in-place.双指针public class Solution { /** * @param nums:原创 2015-11-30 16:06:35 · 272 阅读 · 0 评论 -
gray code
gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray co原创 2015-12-01 13:23:00 · 438 阅读 · 0 评论