提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
题目
1、Valid Parentheses
Write a function that takes a string of parentheses, and determines if the order of the parentheses is valid. The function should return true if the string is valid, and false if it’s invalid.
Examples
“()” => true
“)(()))” => false
“(” => false
“(())((()())())” => true
Constraints
0 <= input.length <= 100
Along with opening (() and closing ()) parenthesis, input may contain any valid ASCII characters. Furthermore, the input string may be empty and/or not contain any parentheses at all. Do not treat other forms of brackets as parentheses (e.g. [], {}, <>).
我的方案:
public class Solution{
public static boolean validParentheses(String parens)
{
//Put code below
int x = 0;
for(int i = 0; i < parens.length() ; i++){
if("(".equals(Character.toString(parens.charAt(i)))){
x++;
}
if(")".equals(Character.toString(parens.charAt(i)))){
x--;
}
if(x< 0){
return false;
}
}
return x == 0;
}
}
别人的解题方案:
import java.util.regex.*;
public class Solution {
public static boolean validParentheses(String parens) {
try {
Pattern.compile(parens.replaceAll("[^()]", ""));
return true;
} catch (PatternSyntaxException e) {
return false;
}
}
}
我使用贪心法解决问题,因为顺序为左右括号为一对,匹配到左括号则计数加1,如果为右括号则消除左括号,如果计数小于0则说明有右括号没有匹配,直接返回false,如果到最后没有计数小于0,则判断最后计数是否等于0,为0则正好匹配完全,如果不为0则有括号没有匹配。
然后看大佬的写法:使用parens.replaceAll(“[^()]”, “”) 消除除括号之外的所有内容,结果用作下一个方法的正则表达式。
Patterns.compile(REGEX) 尝试使用第一步中的 REGEX 创建一个新模式。如果 REGEX 无效(例如,不匹配的括号),它会抛出一个 PatternSyntaxException,该异常被捕获并返回 false。Pattern 类是正则表达式的容器,而 Pattern.compile() 方法采用它给定的文本正则表达式并为 Pattern 加载它。它通过将字符串(减去非括号的所有内容)发送到 Pattern.compile() 方法,利用了 Java 语言中已经内置的括号检查器。编译必须确保它的格式正确,这需要括号正确匹配。
2、Persistent Bugger
Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
For example (Input --> Output):
39 --> 3 (because 39 = 27, 27 = 14, 14 = 4 and 4 has only one digit)
999 --> 4 (because 999 = 729, 729 = 126, 126 = 12, and finally 12 = 2)
4 --> 0 (because 4 is already a one-digit number)
我的解题方案:
class Persist {
public static int persistence(long n) {
// your code
int a ;
int sum = 0;
while (n >= 10){
a= 1;
String s = String.valueOf(n);
for (int i = 0 ; i < s.length(); i++) {
Integer integer = Integer.valueOf(String.valueOf(s.charAt(i)));
a *= integer;
}
n = a;
sum++;
}
return sum;
}
}
别人的解题方案:
class Persist {
public static int persistence(long n) {
long m = 1, r = n;
if (r / 10 == 0)
return 0;
for (r = n; r != 0; r /= 10)
m *= r % 10;
return persistence(m) + 1;
}
}
class Persist {
public static int persistence(long n) {
int times = 0;
while (n >= 10) {
n = Long.toString(n).chars().reduce(1, (r, i) -> r * (i - '0'));
times++;
}
return times;
}
}
我想尝试使用字符串强转成integer类型,然后直接乘就行了,取余数的方法之前做过,想换种写法。看了别人的解题方案,大佬就是大佬,直接使用steram流,char数字减‘0’就是其数值,虽然想法一样,但是代码更简洁优雅。还有个解题就是取余递归获取次数。
总结
今天公司中的需求要完成,下午做了两道题,看了别人steram流使用的很厉害,想想我自己只是基本会用一点,但对函数式编程不是很了解,所以剩下时间去查看java8的介绍和函数式编程的思想,目前还在看,后续应该会写一篇文章来记录下自己的感想和收获。