回文数判断
//回文数判断
public static boolean isPalindrome(int x) {
String s = String.valueOf(x); //int转换为String
char[] arr = new char[s.length()]; //s.length() -->返回字符串长度
for (int i = 0, len = arr.length; i < len; i++) {
arr[i] = s.charAt(i); //遍历字符串每个字符,存入字符数组arr中
}
//System.out.println(Arrays.toString(arr));
char[] arr1 = new char[s.length()];
for (int i = 0, len = arr1.length; i < len; i++) {
arr1[i] = s.charAt(len-i-1);
}
//System.out.println(Arrays.toString(arr1));
for (int i = 0;i<s.length();i++){
if (arr[i]!=arr1[i]){
return false;
}
}
return true;
}
两数相加
public int[] twoSum(int[] nums, int target) {
int[] arr = new int[2];
for(int i = 0;i < nums.length;i++){
for(int j = i+1;j < nums.length;j++){
if(nums[i]+nums[j]==target){
arr[0]=i;
arr[1]=j;
break;
}
}
}
return arr;
}
罗马数字
//罗马数字
public static int romanToInt(String s) {
char[] arr = new char[s.length()]; //s.length() -->返回字符串长度
for (int i = 0, len = arr.length; i < len; i++) {
arr[i] = s.charAt(i); //遍历字符串每个字符,存入字符数组arr中
}
System.out.println(Arrays.toString(arr));
int result = 0;
for (int i = 0; i < arr.length; i++) {
switch (arr[i]) {
case 'I':
if (arr[i+1]!='V' && arr[i+1]!='X'){
result += 1;
}
if (arr[i+1]=='V'){
result += 4;
i++;
}
if (arr[i+1]=='X'){
result += 9;
i++;
}
break;
case 'V':
result += 5;
break;
case 'X':
result += 10;
break;
case 'L':
result += 50;
break;
case 'C':
result += 100;
break;
case 'D':
result += 500;
break;
case 'M':
result += 1000;
break;
//你可以有任意数量的case语句
default:
System.out.println("输入有误!");
break;
}
}
return result;
}
整数反转
public static int reverse(int x) {
if(x==0){
return 0;
}
String s = String.valueOf(x); //int转换为String
int len = s.length();
//判断如果为负数,需截取掉前面的符号,长度减一
//不能使用Math.abs取绝对值,-2147483648取绝对值后还是为负数
if (x<0){
s = s.substring(1,len);
len = len-1;
}
char[] arr = new char[len]; //s.length() -->返回字符串长度
for (int i = 0; i < len; i++) {
arr[i] = s.charAt(len - i - 1); //遍历字符串每个字符,倒序存入字符数组arr中
}
StringBuilder sb = new StringBuilder();
int flag = 0; //标志第一个0
for (int i = 0; i < len; i++) {
if (arr[i] != '0') {
flag = 1;
}
if (flag == 1) {
sb.append(arr[i]);
}
}
String ss = sb.toString(); //StringBuilder转字符串
long result = Long.parseLong(ss); //因为ss转为数值后可能会大于2^31,所以不能使用int储存
if (x < 0) {
result = -Math.abs(result);
}
//2,147,483,648
if (result > (Math.pow(2, 31) - 1) || result < Math.pow(-2, 31)) {
result = 0;
}
int xxx = (int)result; //long转int
return xxx;
}
字符串转整数
//字符串转整数
public static int myAtoi(String s) {
System.out.println(s);
// if (s.isBlank()){
// return 0;
// }
StringBuilder sb = new StringBuilder();
s = s.trim(); //去除空格
int flag = 0; //标志为正
if (s.charAt(0) >= 58 || s.charAt(0) <= 47) {
if (s.charAt(0) == 45) {
flag = 1; //标志为负
} else if (s.charAt(0)==43){
flag = 0;
}else{
return 0;
}
}
int flag1 =0;
for (int i = 0, len = s.length(); i < len; i++) {
if (s.charAt(i) < 58 && s.charAt(i) > 47) {
sb.append(s.charAt(i));
flag1 =1;
}else if (i!=0){
if (flag1==0){
sb.append('0');
}
break;
}
}
StringBuilder sb1 = new StringBuilder();
if (flag == 1&&s.length()!=1) {
sb1.append("-").append(sb);
} else {
sb1.append(sb);
}
String str = sb1.toString();
System.out.println("str:"+str);
if (s.length()>19){
return 0;
}
long result = Long.parseLong(str); //18,446,744,073,709,551,616
if (result > (Math.pow(2, 31) - 1)) {
result = 2147483647;
} else if (result < Math.pow(-2, 31)) {
result = -2147483648;
}
int xxx = (int) result;
return xxx;
}
用两个栈实现队列
栈—>只能在栈顶执行添加或删除操作.
class CQueue {
//两个栈,一个出栈,一个入栈
private Stack<Integer> s1; //执行添加
private Stack<Integer> s2; //执行删除
private int size; //记录删除添加元素的操作次数
public CQueue() {
//初始化
s1 = new Stack<>();
s2 = new Stack<>();
}
public void appendTail(int value) {
s1.push(value);
size++; //增加一次
}
public int deleteHead() {
//先判断
if (size == 0) {
return -1;
}
//s2栈 为空
if (s2.empty()) {
//当s1栈不为空,将s1的所有元素 倒给 s2栈 **
//--> s1栈就变为空,s2栈为原s1栈 倒置
while (!s1.empty()) {
s2.push(s1.pop());
}
}
size--; //删除一次
return s2.pop();
}
}
public static void main(String[] args){
CQueue obj = new CQueue();
int param_0 = obj.deleteHead();
obj.appendTail(5);
obj.appendTail(2);
int param_1 = obj.deleteHead();
int param_2 = obj.deleteHead();
}
剑指 Offer 10- I. 斐波那契数列
递归效率极低!!!会超时
方式一:使用BigDecimal数组,可储存2的64次方以上的数,使用循环,计算出结果再取余
/*
斐波那契数列
n=[0,100]
*/
public static void main(String[] args) {
long start = System.currentTimeMillis();
//System.out.println(fib(50)); //32214毫秒
System.out.println(fib1(95)); //1毫秒
long end = System.currentTimeMillis();
System.out.println("耗时:" + (end - start) + "毫秒");
}
public static int fib(int n) {
if (n < 2) {
return n;
}
return fib(n - 1) + fib(n - 2);
}
public static int fib1(int n) {
int mod = 1000000007; //取余
int len = 101;
BigDecimal[] arr = new BigDecimal[len];
arr[0] = BigDecimal.valueOf(0);
arr[1] = BigDecimal.valueOf(1);
//只计算到 arr[n]
for (int i = 2; i < (n + 1); i++) {
arr[i] = arr[i - 1].add(arr[i - 2]); //add 相加
}
//divideAndRemainder 返回一个数组: result[0]为商,result[1]为余
BigDecimal[] result = arr[n].divideAndRemainder(BigDecimal.valueOf(mod));
return result[1].intValue(); //转为int返回
}
public static int fib2(int n) {
int mod = 1000000007; //取余
int[] arr = new int[n + 1];
//arr[0] = 0; //可省-->默认值为0
arr[1] = 1;
for (int i = 2; i < (n + 1); i++) {
arr[i] = arr[i - 1] + (arr[i - 2]);
//每当下一项大于mod时,减去mod,即相当于进行取余操作,以减法代替取余 效率更高
if (arr[i] > mod) {
arr[i] -= mod;
}
}
return arr[n];
}
public static int fib3(int n) {
if (n <= 1) {
return n;
}
int mod = 1000000007; //取余
int f = 0; //first
int s = 1; //second
int result = 0;
while (--n > 0) {
result = f + s;
if (result > mod) {
result -= mod;
}
f = s;
s = result;
}
return result;
}