10 algorithm questions for interviews-string and array 1, algorithm Array
Question link:
Http://blog.csdn.net/xiaoranlr/article/details/43963933
1. Calculate the inverse Polish Formula
The requirements are as follows:
["2", "1", "+", "3", "*"]-> (2 + 1) * 3)-> 9
["4", "13", "5", "/", "+"] (4 + (13/5)-> 6
That is to say, a given inverse Polish array is used to calculate the result.
For example:
Input: ["2", "1", "+", "3", "*"]
Output: 9
Obviously, we can consider using stacks.
The idea is as follows:
Traverse the input array. When the array member is a number, it is written into the stack. When the array member is an operator, the number in the stack is taken out for calculation.
After the array is traversed, the number left in the stack is the calculation result.
Code:
Import java. util. stack; public class test {public static int GetResult (String [] tokens) {int returnValue = 0; String operators = "+ -*/"; stack <String> stack = new Stack <String> (); for (String t: tokens) {// If t is not a character in the operators String, it indicates that t is a number if (! Operators. contains (t) {stack. push (t);} else {int a = Integer. valueOf (stack. pop (); int B = Integer. valueOf (stack. pop (); switch (t) {case "+": stack. push (String. valueOf (a + B); break; case "-": stack. push (String. valueOf (a-B); break; case "*": stack. push (String. valueOf (a * B); break; case "/": stack. push (String. valueOf (a/B); break ;}} returnValue = Integer. valueOf (stack. pop (); return returnValue;} public static void main (String [] args) {// TODO Auto-generated method stubString [] tokens = new String [] {"2", "1", "+", "3 ","*"}; int Revresult = GetResult (tokens); System. out. println ("Reuslt:" + Revresult );}}
2. Retrieve the input string
The simplest way is to traverse the string and find the maximum length of the text back:
public class test {public static String longestPalindrome(String s) {int maxPalinLength = 0;String longestPalindrome = null;int length = s.length();// check all possible sub stringsfor (int i = 0; i < length; i++) {for (int j = i + 1; j < length; j++) {int len = j - i;String curr = s.substring(i, j + 1);if (isPalindrome(curr)) {if (len > maxPalinLength) {longestPalindrome = curr;maxPalinLength = len;}}}}return longestPalindrome;}public static boolean isPalindrome(String s) {for (int i = 0; i < s.length() - 1; i++) {if (s.charAt(i) != s.charAt(s.length() - 1 - i)) {return false;}}return true;}public static void main(String[] args) {// TODO Auto-generated method stubString tokens = "aabcdc";String Revresult = longestPalindrome(tokens);System.out.println("Reuslt:" + Revresult);}}
The second solution is the Dynamic Programming Method: create a two-dimensional table, where t [I] [j] is used to indicate whether the substring from I to j in string t is a back-to-text (1 indicates a back-to-text, 0 indicates non-return ).
1. Initialization: create a two-dimensional matrix with the length and width t. length, and set the diagonal t [I] [I] to 1.
2. If the two adjacent characters are equal, the return is also the same. Therefore, check the adjacent characters, that is, t [I] [I + 1] is assigned a value.
3. if t [I + 1] [J-1] = 1 & s. charAt (I) = s. charAt (j), t [I] [j] = 1
public class test {public static String longestPalindrome2(String s) {if (s == null)return null;if (s.length() <= 1)return s;int maxLen = 0;String longestStr = null;int length = s.length();int[][] table = new int[length][length];for (int i = 0; i < length; i++) {table[i][i] = 1;}for (int i = 0; i <= length - 2; i++) {if (s.charAt(i) == s.charAt(i + 1)) {table[i][i + 1] = 1;longestStr = s.substring(i, i + 2);}}for (int l = 3; l <= length; l++) {for (int i = 0; i <= length - l; i++) {int j = i + l - 1;if (s.charAt(i) == s.charAt(j)) {table[i][j] = table[i + 1][j - 1];if (table[i][j] == 1 && l > maxLen)longestStr = s.substring(i, j + 1);} else {table[i][j] = 0;}}}return longestStr;}public static void main(String[] args) {// TODO Auto-generated method stubString tokens = "aabcdc";String Revresult = longestPalindrome2(tokens);System.out.println("Reuslt:" + Revresult);}}
The last one is:
Construct the helper function: public String helper (String s, int begin, int end). The function is to find the String centered on begin and end, and then traverse all the digits in the String.
public class test {public static String longestPalindrome(String s) {if (s.isEmpty()) {return null;}if (s.length() == 1) {return s;}String longest = s.substring(0, 1);for (int i = 0; i < s.length(); i++) {// get longest palindrome with center of iString tmp = helper(s, i, i);if (tmp.length() > longest.length()) {longest = tmp;}// get longest palindrome with center of i, i+1tmp = helper(s, i, i + 1);if (tmp.length() > longest.length()) {longest = tmp;}}return longest;}// Given a center, either one letter or two letter,// Find longest palindromepublic static String helper(String s, int begin, int end) {while (begin >= 0 && end <= s.length() - 1&& s.charAt(begin) == s.charAt(end)) {begin--;end++;}return s.substring(begin + 1, end);}public static void main(String[] args) {// TODO Auto-generated method stubString tokens = "aabcdc";String Revresult = longestPalindrome(tokens);System.out.println("Reuslt:" + Revresult);}}