面試10大演算法題匯總-字串和數組1,演算法數組

來源:互聯網
上載者:User

面試10大演算法題匯總-字串和數組1,演算法數組

題目連結:

http://blog.csdn.net/xiaoranlr/article/details/43963933

 

1. 計算逆波蘭式

題目要求如下:

["2","1", "+", "3", "*"] -> ((2 + 1) * 3)-> 9

["4","13", "5", "/", "+"] -> (4 + (13 /5)) -> 6

也就是說給定一個逆波蘭式數組,計算其結果。

如:

輸入:["2","1", "+", "3", "*"]

輸出:9

顯然我們可以考慮使用棧。

思路如下:

遍曆輸入數組,當數群組成員為數字時,則入棧;當數群組成員為運算子時,取出棧中的數字進行運算。

數組遍曆完成後棧中留下的數字即為計算結果。

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) {// 若t不是operators字串中的某個字元,則說明t是數字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.求迴文字串

最簡單的方法為遍曆字串,求出長度最大的迴文:

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);}}

第二種解法為動態規劃法:建立一個二維表,其中t[i][j]用於表示字串t中從i到j的子串是否為迴文(1表示是迴文,0表示非迴文)。

1.初始化:建立長寬為t.length的二維矩陣,將對角線t[i][i]置1

2.若兩兩相鄰的字元相等,則也為迴文,因此檢查相鄰字元,即為t[i][i+1]賦值

3.若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);}}

最後還有一個:

構造helper函數:public String helper(String s, int begin, int end),其功能為求出以begin、end為中心的迴文的字串,之後遍曆字串中所有位。

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);}}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.