将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为 0 或者字符串不是一个合法的数值则返回 0
数据范围:字符串长度满足 0 \le n \le 100 \0≤n≤100
进阶:空间复杂度 O(1) \O(1) ,时间复杂度 O(n) \O(n)
注意:
①字符串中可能出现任意符号,出现除 +/- 以外符号时直接输出 0 ;
②字符串中可能出现 +/- 且仅可能出现在字符串首位。

这里演示的是一种最普通的情况 , 即不带符号的正数 . 如果是带符号的正数/负数呢 ?
以+123为例 :

至于最后元素的符号 , 则容易处理 , 可以设置标志位 , 在最后输出整数时进行判断 !
import java.util.*;
public class Solution {
public int StrToInt(String str) {
if (str == null || str.length() == 0) {
return 0;
}
char[] arr = str.toCharArray();
boolean flag = true;//默认为+
if (str.charAt(0) == '-') {
flag = false;
arr[0] = '0';
}
if (str.charAt(0) == '+') {
arr[0] = '0';
}
int num = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] >= '0' && arr[i] <= '9') {
num = num * 10 + (arr[i] - '0');
} else {
return 0;
}
}
return flag ? num : -num;
}
}
二货小易有一个W*H的网格盒子,网格的行编号为0H-1,网格的列编号为0W-1。每个格子至多可以放一块蛋糕,任意两块蛋糕的欧几里得距离不能等于2。
对于两个格子坐标(x1,y1),(x2,y2)的欧几里得距离为:
( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) ) 的算术平方根
小易想知道最多可以放多少块蛋糕在网格盒子里。

import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int m = scanner.nextInt();
int n = scanner.nextInt();
int[][] array = new int[m][n];
int count = 0;
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
if(array[i][j] == 0){
count++;
if(j+2 < n){
array[i][j+2] = 1;
}
if(i+2 < m){
array[i+2][j] = 1;
}
}
}
}
System.out.println(count);
}
}