You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
s[i] ~s[j]的上一组s[i+1] ~s[j-1]仍然是回形字符串。或者是碰到“bb”这种情况或“bab”,此时的j - i < 3。
采用动态规划仍然会有O(n2)的时间复杂度。
算法从后向前遍历,防止在边界条件时,s[i+1] ~s[j-1]溢出。
publicstatic String longestPalindrome(String s){ int n = s.length(); String ans = null; boolean [][] dp = newboolean[n][n]; for (int i = n-1; i >= 0 ; i--) { for (int j = i; j < n; j++){ if (s.charAt(i) == s.charAt(j) && (j - i < 3 || dp[i + 1][j - 1])){ dp[i][j] = true; }else { dp[i][j] = false; }
if (dp[i][j] && (ans == null || j - i + 1 > ans.length())){ ans = s.substring(i, j+1); } }
} return ans;
6. ZigZag Conversion
Description
The string"PAYPALISHIRING"is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N A P L S I I G Y I R
And then read line by line:"PAHNAPLSIIGYIR"
Write the code that will take a string and make this conversion given a number of rows:
string convert(string s, int numRows);
Example:
Input: s = "PAYPALISHIRING", numRows = 3 Output: "PAHNAPLSIIGYIR"
Input: s = "PAYPALISHIRING", numRows = 4 Output: "PINALSIGYAHRPI" Explanation:
public String convert(String s, int numRows){ if (numRows == 1){ return s; } StringBuffer ans = new StringBuffer(); StringBuffer strBuff = new StringBuffer(s); StringBuffer strBuff2 = new StringBuffer(); int n = s.length(), oneSize = numRows * 2 - 2; int m = n / oneSize;
if (n % oneSize > 0){ for (int i = 0; i < oneSize - n % oneSize; i++) { strBuff.append("*"); } m++; }
for (int i = 0; i < m; i++) { strBuff2.append(strBuff.substring(i*oneSize, numRows + i * oneSize)) .append("*") .append(strBuff.substring(numRows + i * oneSize, oneSize + i * oneSize)) .append("*"); }
for (int i = 0; i < numRows; i++) { for (int j = 0; j < m; j++){ if (strBuff2.charAt(i + j * (oneSize + 2)) != '*'){ ans.append(strBuff2.charAt(i + j * (oneSize + 2))); } if (strBuff2.charAt(oneSize - i + 1 + j * (oneSize + 2)) != '*'){ ans.append(strBuff2.charAt(oneSize - i + 1 + j * (oneSize + 2))); } } } return ans.toString(); }
分好周期后分情况讨论。
public String convert(String s, int numRows){ if(numRows == 0 || s == null || s == "") return""; if(numRows == 1) return s;
StringBuilder sb = new StringBuilder(); int size = 2 * numRows - 2; for(int i = 0; i < numRows; i++) { for(int j = i; j < s.length(); j += size) { sb.append(s.charAt(j)); if(i != 0 && i != numRows - 1) { int temp = j+ size - i * 2; if(temp < s.length()) { sb.append(s.charAt(temp)); } } } } return sb.toString(); }
12. Integer to Roman
Description
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.
Example
Input: 3 Output: "III"
Input: 4 Output: "IV"
Input: 9 Output: "IX"
Input: 58 Output: "LVIII" Explanation: C = 100, L = 50, XXX = 30 and III = 3.
Input: 1994 Output: "MCMXCIV" Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
Symbol Value I 1 V 5 X 10 L 50 C 100 D 500 M 1000
For example, two is written as II in Roman numeral, just two one’s added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
Example
Input: "III" Output: 3
Input: "IV" Output: 4
Input: "IX" Output: 9
Input: "LVIII" Output: 58 Explanation: C = 100, L = 50, XXX = 30 and III = 3.
Input: "MCMXCIV" Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Solution
将罗马数字转换为整形数字,具体思路为:动态规划
将每一个字母对应为整型数字
动态规划,从后向前判断当前数字和其后面的数字的大小
classSolution{ publicintromanToInt(String s){ int temp = charToInt(s.charAt(s.length()-1)); int ans = temp; for (int i = s.length()-2; i >= 0; i--) { if (charToInt(s.charAt(i)) < temp){ ans = ans - charToInt(s.charAt(i)); }else { ans += charToInt(s.charAt(i)); } temp = charToInt(s.charAt(i)); } return ans; }
publicintcharToInt(char c){ int num = 0; switch (c){ case'I': num = 1; break; case'V': num = 5; break; case'X': num = 10; break; case'L': num = 50; break; case'C': num = 100; break; case'D': num = 500; break; case'M': num = 1000; break; default: break; } return num; } }