随笔分类 - leetcode-string
摘要:1 public class Solution { 2 public int evalRPN(String[] tokens) { 3 String operations ="+-*/"; 4 Stack stack = new Stack(); 5 for(String s:tokens){ 6 if(!operations.contains(s)){ 7 stack.push(s); 8 } 9 else{10 ...
阅读全文
posted @ 2014-02-24 03:44
krunning
摘要:Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
阅读全文
posted @ 2014-02-22 13:21
krunning
摘要:Given an absolute path for a file (Unix-style), simplify it.For example,path="/home/", =>"/home"path="/a/./b/../../c/", =>"/c"Did you consider the case wherepath="/../"?In this case, you should return"/".Another corner case is the pat
阅读全文
posted @ 2014-02-19 05:26
krunning
摘要:Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.For"(()", the longest valid parentheses substring is"()", which has length = 2.Another example is")()())", where the longest
阅读全文
posted @ 2014-02-19 00:28
krunning
摘要:You are given a string,S, and a list of words,L, that are all of the same length. Find all starting indices of substring(s) in S that is a concatenation of each word in L exactly once and without any intervening characters.For example, given:S:"barfoothefoobarman"L:["foo", "
阅读全文
posted @ 2014-02-19 00:26
krunning
摘要: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 NA P L S I I GY I RAnd then read line by line:"PAHNAPLSIIGYIR"Write the code that will take a string and
阅读全文
posted @ 2014-02-17 01:44
krunning
摘要:1 public class Solution { 2 public String longestCommonPrefix(String[] strs) { 3 int len = strs.length; 4 if(len<=0) return ""; 5 if(len==1) return strs[0]; 6 String pre = strs[0]; 7 for(int i=1;i<len;i++){ 8 String cur = strs[i]; 9 ...
阅读全文
posted @ 2014-02-17 01:43
krunning
摘要:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S="ADOBECODEBANC"T="ABC"Minimum window is"BANC". 1 public class Solution { 2 public String minWindow(String S, String T) { 3 int sLen
阅读全文
posted @ 2014-02-12 03:58
krunning
摘要:Given an array of words and a lengthL, format the text such that each line has exactlyLcharacters and is fully (left and right) justified.You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces' 'when necessary so that each line
阅读全文
posted @ 2014-02-10 15:30
krunning
摘要:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama"is a palindrome."race a car"isnota palindrome. 1 public class Solution { 2 public boolean isPalindrome(String s) { 3 int len =
阅读全文
posted @ 2014-02-06 15:04
krunning
摘要:1 public class Solution { // don't forget 'break' 'if(s.length()<=0) return false;' should be after trim 2 public boolean isNumber(String s) { 3 s = s.trim(); 4 if(s.length()<=0) return false; 5 boolean eFound = false; 6 boolean dFound = false; 7 int end ...
阅读全文
posted @ 2014-02-06 14:34
krunning
摘要:Implement wildcard pattern matching with support for'?'and'*'.'?' Matches any single character.'*' Matches any sequence of characters (including the empty sequence).The matching should cover the entire input string (not partial).The function prototype should be:bool i
阅读全文
posted @ 2014-02-06 14:15
krunning
摘要:Returns a pointer to the first occurrence of needle in haystack, ornullif needle is not part of haystack. 1 public class Solution { 2 public String strStr(String haystack, String needle) { 3 if(haystack==null) return null; 4 int len1= haystack.length(); 5 int len2 = needl...
阅读全文
posted @ 2014-02-06 14:07
krunning
摘要:Given a string containing just the characters'(',')','{','}','['and']', determine if the input string is valid.The brackets must close in the correct order,"()"and"()[]{}"are all valid but"(]"and"([)]"are not. 1
阅读全文
posted @ 2014-02-06 05:34
krunning
摘要:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999. 1 public class Solution { 2 public int romanToInt(String s) { 3 if(s.length()0 && get(s.charAt(i-1))<cur) 8 res += cur-2*get(s.charAt(i-1)); 9 else{10 ...
阅读全文
posted @ 2014-02-06 05:03
krunning
摘要:Given an integer, convert it to a roman numeral. 1 public class Solution { 2 public String intToRoman(int num) { 3 char[] sym = {'I','V','X','L','C','D','M'}; 4 int scale = 1000; 5 StringBuilder sb = new StringBuilder(); 6 for(int i=6;i>=0;i
阅读全文
posted @ 2014-02-06 05:01
krunning
摘要:Implement regular expression matching with support for'.'and'*'.'.' Matches any single character.'*' Matches zero or more of the preceding element.The matching should cover the entire input string (not partial).The function prototype should be:bool isMatch(const char
阅读全文
posted @ 2014-02-06 04:59
krunning
摘要:Implementatoito convert a string to an integer. 1 public class Solution { 2 public int atoi(String str) { 3 char[] s = str.trim().toCharArray(); 4 if(s.length==0){ 5 return 0; 6 } 7 long num = 0; 8 int sign =1; 9 int len = s.length;10 ...
阅读全文
posted @ 2014-02-06 04:58
krunning
浙公网安备 33010602011771号