打印从1到最大的数
题目描述:https://leetcode-cn.com/problems/da-yin-cong-1dao-zui-da-de-nwei-shu-lcof/
class Solution {
public int[] printNumbers(int n) {
String zeros = "";
for(int i = 0; i < n; i++) {
zeros += "0";
}
int max = Integer.valueOf("1"+zeros);
int[] res = new int[max-1];
for(int i =0; i < res.length; i++) {
res[i] = i+1;
}
return res;
}
}
表示数值的字符串
题目描述:https://leetcode-cn.com/problems/biao-shi-shu-zhi-de-zi-fu-chuan-lcof/
class Solution {
public boolean isNumber(String s) {
if(s == null || s.length() == 0){
return false;
}
//面向测试用例编程,末尾有空格算数字?不解
s = s.trim();
try{
double a = Double.parseDouble(s);
}catch (Exception e){
return false;
}
char c = s.charAt(s.length()-1);
//特,末尾有f,d,D这些不算,但是3.算数字(面向测试用例编程)
return (c >= '0' && c <= '9') || c == '.';
}
}
正则表达式的匹配
题目描述:https://leetcode-cn.com/problems/zheng-ze-biao-da-shi-pi-pei-lcof/
class Solution {
public boolean isMatech(String s, String p) {
int m = s.length() + 1, n = p.length() + 1;
boolean[][] dp = new boolean[m][n];
dp[0][0] = true;
for(int j = 2; j < n; j += 2)
dp[0][j] = dp[0][j - 2] && p.charAt(j - 1) == '*';
for(int i = 1; i < m; i++) {
for(int j = 1; j < n; j++) {
dp[i][j] = p.charAt(j - 1) == '*' ?
dp[i][j - 2] || dp[i - 1][j] && (s.charAt(i - 1) == p.charAt(j - 2) || p.charAt(j - 2) == '.') :
dp[i - 1][j - 1] && (p.charAt(j - 1) == '.' || s.charAt(i - 1) == p.charAt(j - 1));
}
}
return dp[m-1][n-1];
}
}
数字序列中某一位的数字
题目描述:https://leetcode-cn.com/problems/shu-zi-xu-lie-zhong-mou-yi-wei-de-shu-zi-lcof/
class Solution {
public int findNthDigit(int n) {
int digit = 1;
long start = 1;
long count = 9;
while(n > count) {
n -= count;
digit += 1;
start *= 10;
count = digit * start * 9;
}
long num = start + (n - 1)/ digit;
return Long.toString(num).charAt((n-1) % digit)- '0';
}
}
把数字翻译成字符串
题目描述:https://leetcode-cn.com/problems/ba-shu-zi-fan-yi-cheng-zi-fu-chuan-lcof/
class Solution{
public int translateNum(int num) {
String s = String.valueOf(num);
int a = 1, b = 1;
for(int i = 2; i <= s.length(); i++) {
String tmp = s.substring(i-2, i);
int c = tmp.compareTo("10") >= 0 && tmp.compareTo("25")<= 0 ? a + b :a;
b = a;
a = c;
}
return a;
}
}
扑克牌顺子
题目描述:https://leetcode-cn.com/problems/bu-ke-pai-zhong-de-shun-zi-lcof/
//充分必要条件,1.不重复 2.最大最小值相差不大于5
class Solution{
public boolean isStraight(int[] nums) {
Set<Integer> repeat = new HashSet<>();
int max = 0, min = 14;
for(int num : nums) {
if(num == 0) continue;
max = Math.max(max, num);
min = Math.min(min, num);
if(repeat.contains(num)) return false;
repeat.add(num);
}
return max - min < 5;
}
}
1+2+3+4+...+n
题目描述:https://leetcode-cn.com/problems/qiu-12n-lcof/
/**
* 递归思想,&&短路连接符
* @author asus
*
*/
class Solution{
int res = 0;
public int sumNums(int n) {
boolean x = n > 1 && sumNums(n-1) > 0;
res += n;
return res;
}
}
不用加减乘除做加法
题目描述:https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/
class Solution{
public int add(int a, int b) {
while(b != 0) {
int c = (a & b) << 1;
a ^= b;
b = c;
}
return a;
}
}