Given a string, find the length of the longest substring without repeating characters.

Example 1:

Input: “abcabcbb” Output: 3 Explanation: The answer is “abc”, with the length of 3.

Example 2:

Input: “bbbbb” Output: 1 Explanation: The answer is “b”, with the length of 1.

Example 3:

Input: “pwwkew” Output: 3 Explanation: The answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

privatestaticintlengthOfLongestSubstring(String s){ Set<Character> substring = new HashSet<>(); int res = 0;

if (s == null) { return res; }

for (int i = 0; i < s.length(); i++) { int temp = 0; int j = i; while (j < s.length()) { if (!substring.contains(s.charAt(j))) { substring.add(s.charAt(j++)); temp++; } else { substring.clear(); break; } } res = Math.max(temp, res); }

privatestaticintplus(String s){ int res = 0; Set<Character> set = new HashSet<>(); int i = 0, j = i; while (j < s.length()) { if (!set.contains(s.charAt(j))) { set.add(s.charAt(j++)); res = Math.max(res, set.size()); } else { set.remove(s.charAt(i++)); } }

Given a string containing just the characters ‘(‘, ‘)’, ‘{‘, ‘}’, ‘[‘ and ‘]’, determine if the input string is valid.

An input string is valid if:

Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Note that an empty string is also considered valid.

classSolution{ publicstatic String longestCommonPrefix(String[] strs){ if (strs == null) return"";

String res = strs[0]; char[] resChar = res.toCharArray();

for (int i = 0; i < strs.length; i++) { char[] tempChar = strs[i].toCharArray(); for (int j = 0; j < tempChar.length & j < resChar.length; j++) { if (resChar[j] != tempChar[j]) { res = strs[i].substring(0, j); resChar = res.toCharArray(); break; } } }

return res; } }

LeetCode Solution

1 2 3 4 5 6 7 8 9 10

public String longestCommonPrefix(String[] strs){ if (strs.length == 0) return""; String prefix = strs[0]; for (int i = 1; i < strs.length; i++) while (strs[i].indexOf(prefix) != 0) { prefix = prefix.substring(0, prefix.length() - 1); if (prefix.isEmpty()) return""; } return prefix; }

Nice idea to use substring to find out what index is it. If strs doesn’t have a substring the index can not be 0. All we need to do is cut the prefix‘s length.

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 1:

Input: “III” Output: 3

Example 2:

Input: “IV” Output: 4

Example 3:

Input: “IX” Output: 9

Example 4:

Input: “LVIII” Output: 58 Explanation: L = 50, V= 5, III = 3.

Example 5:

Input: “MCMXCIV” Output: 1994 Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

classSolution{ publicintromanToInt(String s){ char[] array = s.toCharArray(); int sum = 0;

for (int i = 0; i < array.length - 1; i++) { if (trans(array[i]) >= trans(array[i + 1])) { sum += trans(array[i]); } else { sum -= trans(array[i]); } }

if (array.length - 1 >= 0) { sum += trans(array[array.length - 1]); }

return sum; }

publicinttrans(char c){

switch (c) { case'I': return1; case'V': return5;

case'X': return10;

case'L': return50;

case'C': return100;

case'D': return500;

case'M': return1000; default: return0; } } }

We just need to get right number what each character meaning for. And judge the next character implies whether larger than the current character, then we can get it.