Topic Links:
http://blog.csdn.net/xiaoranlr/article/details/43963933
1. Calculation of inverse Polish style
The subject requirements are as follows:
["2", "1", "+", "3", "*"], ((2 + 1) * 3), 9
["4", "+", "5", "/", "+"], (4 + (13/5)), 6
That is, given an inverse Polish-style array, the result is computed.
Such as:
Input: ["2", "1", "+", "3", "*"]
Output: 9
Obviously we can consider using stacks.
Ideas are as follows:
Iterates through the input array, when the array member is a number, and when the array member is an operator, the number in the stack is taken.
The number left in the stack after the completion of the array traversal is the result of the calculation.
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, then T is a number if (!oper Ators.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)); 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. Find the text string
The simplest way to traverse a string is to find the largest palindrome of its length:
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;maxpal Inlength = 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.L Ength ()-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 dynamic programming: Establish a two-dimensional table, where t[i][j] is used to denote whether the substring from I to J in the string T is a palindrome (1 is a palindrome and 0 is a non-palindrome).
1. Initialize: Set up a two-dimensional matrix with a long width of t.length, and set the diagonal t[i][i] 1
2. If the 22 adjacent characters are equal, it is also a palindrome, so check the adjacent characters, that is, t[i][i+1] Assignment
3. If t[i+1][j-1] = = 1 && s.charat (i) = = S.charat (j), then t[i][j] = = 1
public class Test {public static String longestPalindrome2 (string s) {if (s = = null) return Null;if (S.length () <= 1) RET Urn 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.substrin G (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.cha RAt (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);}}
Finally there is another:
construct helper function: public string Helper (string s, int begin, int end), Its function is to find a string of palindrome that is centered at begin and end, then iterate through all the bits 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 Istri ng 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 letter,//Find longest palindromepublic static string helper (string s, int beg In, 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);}}
Interview 10 Big algorithm Rollup-strings and Arrays 1