H******7 发帖数: 1728 | 1 java里 不用charAt 直接用[] 可以么?
面试的时候这么写。 |
|
b******o 发帖数: 87 | 2 what is the charater of the current international political system?
how do the democratic peace theory, institutionalism chanllege the structural realsim?
how does the strutral realism explain why there does not exist balance of power
after the cold war? |
|
b******o 发帖数: 87 | 3 can the democracy peace theory and institutionalims change the charater
of the current international system?
according to structrual realism, the balance of power will occur because the
anarnic and self-help international system. if the anarnic international
system can not be changed, the balance of power will exist. so I am wandering
does the democracy peace theory or instituionalism can change the anarchic and
self-help system? if they can, how? |
|
s********x 发帖数: 914 | 4 if you insist:
public static boolean isNumber(String s) {
if (s == null || s.length() == 0) {
throw new IllegalArgumentException();
}
int start = 0, end = s.length() - 1;
// trim
while ((end-1) >= 0 && isWhiteSpace(s.charAt(end))) {
end--;
}
while ((start+1) <= end && isWhiteSpace(s.charAt(start))) {
start++;
}
// skip beginning + or -
if ((start+1) <= end && s.charAt(start) == '+' || s.charAt(start) == '-') {
start++;
}
start = isInteger(start, end, s);
if (start < 0) {
return false;
}
bool... 阅读全帖 |
|
c***d 发帖数: 26 | 5 这题似乎不值得用状态机,按顺序检查过去就好。
不过写这么多if/while的确很吐血。。。
还有要事先问清很多情况是否符合,比如e前后可否出现空格
public class Solution {
public boolean isNumber(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if(s==null) return false;
int len = s.length();
int i=0;
boolean isNumber = false;
while(i
if(i
while(i
isN... 阅读全帖 |
|
S*****B 发帖数: 404 | 6 public int check(String s) {
if (s.length() == 0)
return 0;
int n = s.length();
if (n == 1) {
if (s.charAt(0) == '0')
return 0; // not valid
else
return 1;
}
int[] num = new int[n];
if (s.charAt(0) == '0')
return 0; // not valid
else
num[0] = 1;
if (s.charAt(1) != '0')
num[1] += num[0];
if (s.charAt(0) == '1' || (s.charAt(... 阅读全帖 |
|
z*********e 发帖数: 10149 | 7 这是按照sdx写的valid number,
显示 Runtime Error Message: Line 5: java.lang.
StringIndexOutOfBoundsException: String index out of range: 0
Last executed input: "3"
就是说这一行有错"if(s.charAt(0) == '+' || s.charAt(0) == '-') s = s.
substring(1);"
明显不可能麻,我空string都处理了还说我越界
===============================================
public boolean isNumber(String s) {
if(s == null || s.isEmpty()) return false;
s = s.trim();
if(s.charAt(0) == '+' || s.charAt(0) == '-') s = s.substring(1);
... 阅读全帖 |
|
c*****a 发帖数: 808 | 8 感谢阿,过了
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
// Start typing your Java solution below
// DO NOT write main() function
int l1 = s1.length(), l2 = s2.length(), l3 = s3.length();
if (l1+l2 != l3) return false;
if (s1.equals("")) return s2.equals(s3);
if (s2.equals("")) return s1.equals(s3);
if(s3.charAt(l3-1) != s2.charAt(l2-1) && s3.charAt(l3-1) != s1.
charAt(l1-1))
retu... 阅读全帖 |
|
c*****a 发帖数: 808 | 9 感谢阿,过了
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
// Start typing your Java solution below
// DO NOT write main() function
int l1 = s1.length(), l2 = s2.length(), l3 = s3.length();
if (l1+l2 != l3) return false;
if (s1.equals("")) return s2.equals(s3);
if (s2.equals("")) return s1.equals(s3);
if(s3.charAt(l3-1) != s2.charAt(l2-1) && s3.charAt(l3-1) != s1.
charAt(l1-1))
retu... 阅读全帖 |
|
s********x 发帖数: 914 | 10 用同样的思路写了一个,没有测,可能有小bug。但应该是只扫一遍。
private static boolean isWhiteSpace(char c) {
if (c == ' ' || c == '\t') {
return true;
}
return false;
}
public static boolean isNumber(String s) {
if (s == null || s.length() == 0) {
throw new IllegalArgumentException();
}
int start = 0, end = s.length() - 1;
// trim
while ((end-1) >= 0 && isWhiteSpace(s.charAt(end))) {
end--;
}
while ((start+1) <= end && isWhiteSpace(s.charAt(start))) {
start++;
}
// ... 阅读全帖 |
|
n****r 发帖数: 120 | 11 俺也奔一个:
static int compare(String a, String b, char c){
if (a == null) return b == null ? 0 : -1;
if (b == null) return 1;
int i = 0, j = 0;
while (i < a.length() && j < b.length()){
while (i < a.length() && a.charAt(i) == c)
i++;
while (j < b.length() && b.charAt(j) == c)
j++;
if (i>=a.length() || j >= b.length())
break;
if (a.charAt(i) == b.charAt(j)){
i++;
j++;
}else
return a.charAt(i) - b.charAt(j);
}
int cnt = 0;
while (i < a.length()){
if (a.charAt(i) != c){
cnt += a.charAt(i) - '0';
if (... 阅读全帖 |
|
T*****n 发帖数: 82 | 12 String to Integer (atoi) 这一题
通过的code是:
public class Solution {
public int myAtoi(String str) {
if (str == null || str.length() < 1)
return 0;
// trim white spaces
str = str.trim();
char flag = '+';
// check negative or positive
int i = 0;
if (str.charAt(0) == '-') {
flag = '-';
i++;
} else if (str.charAt(0) == '+') {
i++;
}
// use double to store result
double result = 0;
// calculate value
while... 阅读全帖 |
|
J*********a 发帖数: 50 | 13 这是第二题的DP solution,不用谢。
public static int CountDistinctSubseuqence(String text, String p) {
int m = text.length();
int n = p.length();
int[][] dp = new int[n + 1][m + 1];
for (int j = 0; j <= m; j++) dp[0][j] = 1;
for (int i = 2; i <= n; i += 2) {
for (int j = (p.charAt(1) == '+' ? 2 : 4); j <= m; j++) {
char c = p.charAt(i - 2);
if (p.charAt(i - 1) == '+') {
if (text.charAt(j - 1) == c... 阅读全帖 |
|
发帖数: 1 | 14 不知道对不对 大概跑了几个test case貌似没啥问题
public static int decodeWays(String s) {
if (s == null || s.length() == 0) return 0;
int[] dp = new int[s.length() + 1];
dp[s.length()] = 1;
for (int i = s.length() - 1; i >= 0; i--) {
if (s.charAt(i) == '0') dp[i] = 0;
else if (s.charAt(i) != '*') {
dp[i] += dp[i + 1];
if (i + 1 < s.length()) {
if (s.charAt(i + 1) != '*') {
int num = (s... 阅读全帖 |
|
n****r 发帖数: 120 | 15 奔一个:
public static String removeComments(String a) {
StringBuilder sb = new StringBuilder();
int i = 0, j = 0;
while (i < a.length()) {
if (a.charAt(i) == '/' && i + 1 < a.length() && (a.charAt(i + 1) ==
'/' || a.charAt(i + 1) == '*')) {
j = i + 2;
boolean pairMatched = false;
boolean lineComment = true;
char[] pair = {'\n'};
if (a.charAt(i+1) == '*'){
lineComment = false;
... 阅读全帖 |
|
e****e 发帖数: 418 | 16 My code.
public static String removeComments( String s ) {
for ( int i = 0; i < s.length() - 1; i++ ) {
if ( s.charAt( i ) == '"' ) { // Find "
for ( int j = i + 1; j < s.length(); j++ ) {
if ( s.charAt( j ) == '"' ) { // Find "
i = j + 1;
break;
}
}
}
if ( i < s.length() - 1 && s.charAt( i ) == '/' && s.charAt( i +
1) == '/' ) { /... 阅读全帖 |
|
c******t 发帖数: 391 | 17 感谢分享!
第一轮算括号的题,想到两种解法,用counter算左右括号数,以及压栈比较。
//For '(,)' only, use counter, left and right are half of length, e.g. for "
(())", the call
is validParen(2,2,"(())",0).
public static boolean validParen(int left, int right, String str, int
index){
if(left==0&&right==0)return true; //base case
if(str.length()%2==1)return false; //length must be even
if(left>right)return false;
if(left<0||right<0)return false;
if(str.charAt(index)=='(') return validParen(le... 阅读全帖 |
|
b*2 发帖数: 94 | 18 去年还真做过这道题:
private static boolean match(String regEx, String word) {
// TODO Auto-generated method stub
if(regEx == "*")
return true;
int reIndex = 0;
int wdIndex = 0;
for(;reIndex
if(regEx.charAt(reIndex)==word.charAt(wdIndex)){
continue;
}else if(regEx.charAt(reIndex)!='?'&®Ex.charAt(reIndex)!='*'){
//simply not equivalent
return false;
}else if(regEx.charAt(reIndex)=='?'){
//deal with ?
//goto the next char
continue;
}else{
//de... 阅读全帖 |
|
t*******3 发帖数: 734 | 19 这是一个只scan一遍就可以的java code.
public static ArrayList maxRep(String s) {
if (s == null){
return null;
}
ArrayList al = new ArrayList();
int len = s.length();
if (len == 0) {
return al;
}
int max = 1;
int count = 1;
for (int i = 1; i < len; i++) {
if (s.charAt(i) == ' ' || s.charAt(i) != s.charAt(i - 1)) {
if (count < max) {
... 阅读全帖 |
|
x*********1 发帖数: 23 | 20 我写了一个,大家帮看看, 谢谢啦, 请问楼主在哪找到的第二题的code,很想学习
一下
public long stringToLong(String str) {
if (str==null||str.length()==0){
return 0;
}
int sign=1;
Long sum=(long) 0;
int i=0;
while(i
i++;
}
if (i==str.length()){
return 0;
}
if (str.charAt(i)=='+'){
... 阅读全帖 |
|
y*******d 发帖数: 1674 | 21 1, If p.charAt(j) == s.charAt(i) : dp[i][j] = dp[i-1][j-1];
2, If p.charAt(j) == '.' : dp[i][j] = dp[i-1][j-1];
3, If p.charAt(j) == '*':
here are two sub conditions:
1 if p.charAt(j-1) != s.charAt(i) : dp[i][j] = dp[i][j-2]
//in this case, a* only counts as empty
2 if p.charAt(j-1) == s.charAt(i) or p.charAt(j-1) == '.':
dp[i][j] = dp[i-1][j] //in this case, a*
counts as multiple a
or dp[i][j] = d... 阅读全帖 |
|
a*********0 发帖数: 2727 | 22 recursion version
boolean StringMatch(String reg, int i, String input, int j){
if(i>=reg.length && j>=input.length){
return true;
}
if(i>=reg.length && j
return false;
if(i+1
while(j
){
if(StringMatch(reg, i+2, input, j+1))
retur... 阅读全帖 |
|
a*********0 发帖数: 2727 | 23 泪奔,我都没调,现在work了,full code
import java.util.*;
public class SimpleRegMatch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("reg:?");
Scanner in=new Scanner(System.in);
String reg=in.nextLine();
System.out.println("string:?");
String input=in.nextLine();
if(StringMatch(... 阅读全帖 |
|
t****s 发帖数: 1 | 24 a Java version for regex match as below, let me know if there is any issue.
boolean isMatch(String s, String p) throws Exception{
if (p==null || p.length()==0)
return s==null || s.length()==0;
return isMatch(s,p,0,0);
}
boolean isMatch(String s, String p, int is, int ip) throws Exception{
if (ip>=p.length())
return s==null || is>=s.length();
if (ip==p.length()-1 || p.charAt(ip+1)!='*'){
if (p.charAt(ip)=='*')
throw new Exception("illegal");
if (is>=s.lengt... 阅读全帖 |
|
z****h 发帖数: 164 | 25 private void printstringwithprefix(String[] ss, String prefix) {
if(ss == null) return;
if(prefix == null || prefix.isEmpty()) return;
int low = 0;
int high = ss.length -1;
for(int i = 0; i< prefix.length() && low <= high; i++)
{
int mid = (low+high)/2;
if(i >= ss[mid].length())
{
return;
}
if(ss[mid].charAt(i) < prefix.charAt(i))
... 阅读全帖 |
|
z******e 发帖数: 82 | 26 借用eswine的idea:
public static String test1(String str) {
StringBuilder sb = new StringBuilder();
int len = str.length();
boolean escape = false;
for (int i = 0; i < len; i++) {
char c = str.charAt(i);
// everything between ' '
if (c == '\'') {
escape = true;
while (i < len) {
c = str.charAt(i);
sb.append(c);
if (c == '\'' && !escape) {... 阅读全帖 |
|
c*****a 发帖数: 808 | 27 呃。。刚刚写的, 过不了 Progress: 47/48 test cases passed.
大牛看看有啥bug
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
// Start typing your Java solution below
// DO NOT write main() function
int l1 = s1.length(), l2 = s2.length(), l3 = s3.length();
if (l1+l2 != l3) return false;
if (s1.equals("")) return s2.equals(s3);
if (s2.equals("")) return s1.equals(s3);
while(l3 >0 && l2 >0 && l1>0){
if(s3.ch... 阅读全帖 |
|
c********t 发帖数: 5706 | 28 啊,我的small要576 ms, 差距太大了。帮我看看我的时间空间复杂度是多少
public static boolean isInterleave(String s1, String s2, String s3) {
assert (s1 != null && s2 != null && s3 != null);
if (s3.length() != s1.length() + s2.length()) return false;
if (s1.isEmpty()) return s2.equals(s3);
if (s2.isEmpty()) return s1.equals(s3);
if (s3.charAt(0) == s1.charAt(0) && s3.charAt(0) == s2.charAt(0))
return isInterleave(s1.substring(1), s2, s3.substring(1))
|| isInterleave(s1, s2.substring... 阅读全帖 |
|
c*****a 发帖数: 808 | 29 呃。。刚刚写的, 过不了 Progress: 47/48 test cases passed.
大牛看看有啥bug
public class Solution {
public boolean isInterleave(String s1, String s2, String s3) {
// Start typing your Java solution below
// DO NOT write main() function
int l1 = s1.length(), l2 = s2.length(), l3 = s3.length();
if (l1+l2 != l3) return false;
if (s1.equals("")) return s2.equals(s3);
if (s2.equals("")) return s1.equals(s3);
while(l3 >0 && l2 >0 && l1>0){
if(s3.ch... 阅读全帖 |
|
c********t 发帖数: 5706 | 30 啊,我的small要576 ms, 差距太大了。帮我看看我的时间空间复杂度是多少
public static boolean isInterleave(String s1, String s2, String s3) {
assert (s1 != null && s2 != null && s3 != null);
if (s3.length() != s1.length() + s2.length()) return false;
if (s1.isEmpty()) return s2.equals(s3);
if (s2.isEmpty()) return s1.equals(s3);
if (s3.charAt(0) == s1.charAt(0) && s3.charAt(0) == s2.charAt(0))
return isInterleave(s1.substring(1), s2, s3.substring(1))
|| isInterleave(s1, s2.substring... 阅读全帖 |
|
d*********g 发帖数: 154 | 31 练习一个:
public class Solution {
public boolean isInterleave(String s1, String s2, String s3)
{
if(s1 == null || s2 == null || s3 == null) return false;
if(s1.length() + s2.length() != s3.length()) return false;
boolean[][] matrix = new boolean[s1.length()+1][s2.length()+1];
matrix[0][0] = true;
for(int i = 1; i < matrix.length; ++i)
{
matrix[i][0] = (s3.charAt(i-1) == s1.charAt(i-1)) ? true :
false;
... 阅读全帖 |
|
d****n 发帖数: 233 | 32 public class Solution {
public boolean isMatch(String s, String p) {
if (p=="") return s == "";
int m = s.length() + 1;
int n = p.length() + 1;
boolean[][] result = new boolean[n][m];
// Initialization part
result[0][0] = true;
result[1][0] = false;
for( int i = 2; i < n; i++)
result[i][0] = p.charAt(i-1) == '*' ? result[i-2][0] : false;
for( int j = 1; j < m; j++){... 阅读全帖 |
|
c********p 发帖数: 1969 | 33 请大牛帮忙看看。
第一个是longest palindrome的,我按leetcode discussion的c++代码改的java的,怎
么就过不了了呢?一行行对过了阿。。。我知道有o(n)的算法,但这个dp的我就奇怪怎
么过不了呢,哪个细节写错了。。。
public class Solution {
public String longestPalindrome(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if(s == null || s.length() <= 1){
return s;
}
int[][] P = new int[s.length()][s.length()];
int max = 1;
int begin = 0;
for(int i =... 阅读全帖 |
|
b********g 发帖数: 28 | 34 public class Solution {
public boolean isMatch(String s, String p) {
// Start typing your Java solution below
// DO NOT write main() function
if(s == null || p == null) return false;
int m = s.length(), n = p.length();
boolean[][] match = new boolean[m + 1][n + 1];
match[0][0] = true;
for(int i = 1; i <= m; i++){
match[i][0] = false;
}
for(int j = 1; j <= n; j++){
if(p.charAt(j - 1) == '*'){
... 阅读全帖 |
|
p*****3 发帖数: 488 | 35 低手可以不...
public class Solution {
public boolean isMatch(String s, String p) {
if (s == null || p == null)
return false;
int ls = s.length();
int lp = p.length();
boolean[][] dp = new boolean[ls+1][lp+1];
dp[0][0] = true;
for (int i = 1; i <= p.length(); i++) {
dp[0][i] = (p.charAt(i-1) == '*' ? dp[0][i-2] : false);
}
for (int i = 1; i <= ls; i++) {
for (int j = 1; j... 阅读全帖 |
|
l*****4 发帖数: 267 | 36 recursive和iterative各写了一下Java版本。提供的所有测试例子都通过了。
public static String remove(String s) {
if (s == null || s.length() <= 1) {
return s;
}
for (int i = 0; i < s.length() - 1; i++) {
if (s.charAt(i) == s.charAt(i + 1)) {
int j = i + 1;
while (j < s.length() - 1 && s.charAt(j) == s.charAt(j + 1))
j++;
String left = s.substring(0, i);
String right = s.substring(j + 1);
... 阅读全帖 |
|
S*******C 发帖数: 822 | 37 a+b不match b,但很多test case通不过,看看怎么回事
public class Solution {
public boolean match(String s, String p) {
return match(s, 0, p, 0);
}
private boolean match(String s, int i, String p, int j) {
if (j >= p.length() - 1) {
return i >= s.length() - 1;
} else if (i >= s.length() - 1) {
return j >= p.length() - 1;
}
if (p.charAt(j + 1) == '*') {
// match(s+1, p) - match next char in s.
// match(s, p+2) - mat... 阅读全帖 |
|
I**********s 发帖数: 441 | 38 这涉及到C++转换成Java的一点小技巧。这样改就可以都通过了:
private boolean match(String s, int i, String p, int j) {
if (j == p.length()) return i == s.length();
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
// match(s+1, p) - match next char in s.
// match(s, p+2) - match exactly nothing in s.
if (i < s.length() && s.charAt(i) == p.charAt(j))
return match(s, i + 1, p, j) || match(s, i, p, j + 2);
else
return match(s... 阅读全帖 |
|
发帖数: 1 | 39 简化版只输出一个答案可以用counter做。
如果是301原题输出所有解那就要dfs了,dfs比bfs省空间,并且不需要set来去重,看
起来更elegant。DFS之前要先count一下需要删除多少个。时间复杂度是O(N^k), k是需
要删除的括号数。当然是需要删除的越多,解就越多,复杂度就越高。上code:
class Solution {
public List removeInvalidParentheses(String s) {
int leftRemove = 0;
int rightRemove = 0;
int open = 0;
for (int i = 0; i < s.length(); ++i) {
char cur = s.charAt(i);
if (cur == '(') {
open++;
} else if (cur == ')') {
... 阅读全帖 |
|
g**********y 发帖数: 14569 | 40 写了个Java版的:
public boolean matches(String pattern, String str) {
int i = 0;
while (i
charAt(i)!='*') i++;
if (i == pattern.length()) return pattern.equals(str);
char c = pattern.charAt(i-1);
if (pattern.charAt(i) == '?') {
return pattern.substring(0, i-2).equals(str.substring(0, i-2)) &&
(equals(c, str, i-1) && matches(pattern.substring(i+1),
str.substring... 阅读全帖 |
|
v****a 发帖数: 236 | 41 public static boolean isIsomorphic(String inputString1, String inputString2)
{
int length1 = inputString1.length();
int length2 = inputString2.length();
if (length1 != length2) {
return false;
}
if (length1 == 1) {
return true;
}
Map map = new HashMap();
for (int i = 0; i < length1; i++) {
if (!map.containsKey(inputString1.charAt(i)) && !map.
containsVal... 阅读全帖 |
|
j*****8 发帖数: 3635 | 42 题目如下:
Given a string that contains only digits 0-9 and a target value, return all
possibilities to add binary operators (not unary) +, -, or * between the
digits so they evaluate to the target value.
给的几个例子:
"123", 6 -> ["1+2+3", "1*2*3"]
"232", 8 -> ["2*3+2", "2+3*2"]
"105", 5 -> ["1*0+5","10-5"]
"00", 0 -> ["0+0", "0-0", "0*0"]
"3456237490", 9191 -> []
下面是我的java code,有个test case一直超时,求大牛指点优化。我的思路很简单,
先生成所有可能的计算式,然后对每个计算式求值与target比较。
public List addOperators(String num, int target) {
... 阅读全帖 |
|
h**********8 发帖数: 267 | 43 题目:
The Game of Master Mind is played as follows:
The computer has four slots containing balls that are red (R), yellow (Y),
green (G) or blue (B). For example, the computer might have RGGB (e.g., Slot
#1 is red, Slots #2 and #3 are green, Slot #4 is blue).
You, the user, are trying to guess the solution. You might, for example,
guess YRGB.
When you guess the correct color for the correct slot, you get a “hit”. If
you guess a color that exists but is in the wrong slot, you get a “pseudo-
hit”. F... 阅读全帖 |
|
g**********y 发帖数: 14569 | 44 sigh, 边界条件又写错了,修改版:
public boolean matches(String pattern, String str) {
int i = 0;
while (i < pattern.length() && pattern.charAt(i) != '?'
&& pattern.charAt(i) != '*')
i++;
if (i == pattern.length())
return pattern.equals(str);
char c = pattern.charAt(i - 1);
if (pattern.charAt(i) == '?') {
return pattern.substring(0, i - 1).equals(str.substring(0, i - 1
))
&& (matches(c + pattern.sub... 阅读全帖 |
|
g**********y 发帖数: 14569 | 45 没仔细看后面回帖,贴一个Java解,就是实现next_permute()。对任意输入串,把字母
排序,然后调用就行。
这里的main()就是给了个简单例子。
public class NextPermute {
/**
* Return next permute number/string in sequence. If already highest,
* return null.
*
* @param number
* @return
*/
public String nextPermute(String s) {
int N = s.length();
StringBuilder sb = new StringBuilder(s);
int head = N - 2;
while (head >= 0 && sb.charAt(head) >= sb.charAt(head+1)) head--;
if (h... 阅读全帖 |
|
s**********e 发帖数: 326 | 46 贴个我之前写的代码,仿照java里面的源码写的
public static long stringToLong(String str) throws Exception{
if(!chekValid(str)){
throw new Exception("invalid input!!");
}
long limit;
boolean isNegative = false;
int curIndex = 0;
if(str.charAt(0) == '-'){
limit = Long.MIN_VALUE;
isNegative = true;
curIndex = 1;
}else{
limit = -1 * Long.MAX_VALUE;
}
long preLimit = limit/10;
... 阅读全帖 |
|
p*****2 发帖数: 21240 | 47
是。因为做过很多类似的DP题,上来就往DP那里想了。做的时候才发觉DP帮助不明显。
但是测试用例也都过了,就提交了。这题证明greedy可以也得花一些时间。不是很直观
。如果只是有思路的话,还是会在greedy和dp只见纠缠。感觉比赛的时候很难掌握呀。
另外,上一下greedy的代码。
public class test2 {
static String s;
static int k;
static HashSet pairs = new HashSet();
static boolean isForbidden(char[] arr) {
Arrays.sort(arr);
return pairs.contains(new String(arr));
}
static int Play(String str) {
int count = 0;
int count1 = 0;
int count2 = 0;
int j = 0;
for (int... 阅读全帖 |
|
l***m 发帖数: 339 | 48 JAVA 不太熟,尝试着快速写了下这两道题,请各位大牛帮忙看看啊,有没有什么
问题。当然我这里没有考虑大数问题,这个呆会我再想想,或者谁教我也好。
public class RomansToInt {
public static HashMap map;
public static int RomanToInt(String input) {
if (input == null || input.length() == 0) {
System.out.println("The input is not valid");
return -1;
}
int len = input.length();
if (len == 1) {
return map.get(input.charAt(0));
}
int result = 0;
int i =... 阅读全帖 |
|
w***o 发帖数: 109 | 49 大牛们很忙,让我来给你解释解释。我两水平差不多,我的思路对你可能容易理解一点
。这题主要是要逼你写DP,而且是Buttomup的。我没有二爷那么牛,可以直接写
buttomup的DP,我是一步一步来的。不好意思C++早忘了,java的,你凑合看吧。
先来recursive without DP。
public int numDistinct(String S, String T) {
if(T.length() == 0)
return 1;
if(S.length() < T.length())
return 0;
int ret = 0;
if(S.charAt(0) == T.charAt(0))
ret += numDistinct(S.substring(1), T.substring(1));
ret += numDistinct(S.substring(1), T);... 阅读全帖 |
|
A****L 发帖数: 138 | 50 贴一个 O(n) 解法, java code 用了KMP 里的prefix function. 思路是前面有人提到
的 topcoder连接里讲解的。
public boolean checkRepetition(String s) {
int m = s.length();
if(m<4) return false;
for(int i=1;i
if(s.charAt(i)!=s.charAt(i-1)) break;
if(i==m-1) return false;
}
int[] pattern = prefixFunction(s);
int p = pattern[m];
while(p>1) {
if(p%(m-p)==0) return true;
p=pattern[p];
}
return f... 阅读全帖 |
|