class Solution {
public int strToInt(String str) {
if (str.length()==0){
return 0;
}
int idx = -1;
while (++idx<str.length() && str.charAt(idx)==' '){}
if (idx==str.length()){
return 0;
}
if (str.charAt(idx)!='-' && str.charAt(idx)!='+' && (str.charAt(idx)-'0'<0 || str.charAt(idx)-'0'>9)){
return 0;
}
boolean flag = true;
if (str.charAt(idx)=='-' || str.charAt(idx)=='+'){
flag = str.charAt(idx) != '-';
idx++;
}
int res = 0;
idx--;
while (++idx<str.length() && str.charAt(idx)-'0'>=0 && str.charAt(idx)-'0'<=9){
if (flag){
if (res > Integer.MAX_VALUE/10 || (res == Integer.MAX_VALUE/10 && str.charAt(idx)-'0'> 7)){
return Integer.MAX_VALUE;
}
}else{
if (res > Integer.MAX_VALUE/10 || (res == Integer.MAX_VALUE/10 && str.charAt(idx)-'0'> 8)){
return Integer.MIN_VALUE;
}
}
res = res * 10 + ( str.charAt(idx)-'0');
}
return flag?res:-res;
}
}
今天重新做了一次,比之前应该好看点代码
class Solution {
public int strToInt(String str) {
int idx = -1;
while (++idx < str.length() && str.charAt(idx)==' '){}
if (idx == str.length()){
return 0;
}
int flag = 1;
if (str.charAt(idx) == '-'){
idx++;
flag = -1;
}else if (str.charAt(idx) == '+'){
idx++;
}
int ans = 0;
int limitL = Integer.MIN_VALUE/10;
int limitLL = -Integer.MIN_VALUE%10;
int limitR = Integer.MAX_VALUE/10;
int limitRR = Integer.MAX_VALUE%10;
while (idx < str.length() && str.charAt(idx)- '0' >= 0 && str.charAt(idx)-'9' <= 0){
int num = flag * (str.charAt(idx)- '0');
if (ans < limitL || (ans == limitL && num < limitLL)){
return Integer.MIN_VALUE;
}
if (ans > limitR || (ans == limitR && num > limitRR)){
return Integer.MAX_VALUE;
}
ans *=10;
ans += num;
idx++;
}
return ans;
}
}
class Solution {
public int distributeCandies(int[] candyType) {
HashSet<Integer> hashSet = new HashSet<>();
for (int i : candyType) {
hashSet.add(i);
if (hashSet.size()>=candyType.length/2){
return hashSet.size();
}
}
return hashSet.size();
}
}
class Solution {
public String[] findWords(String[] words) {
String s1 = "qwertyuiopQWERTYUIOP";
String s2 = "asdfghjklASDFGHJKL";
String s3 = "zxcvbnmZXCVBNM";
HashMap<Character,Integer> hashMap = new HashMap<>();
for (int i = 0; i < s1.length(); i++) {
hashMap.put(s1.charAt(i),1);
}
for (int i = 0; i < s2.length(); i++) {
hashMap.put(s2.charAt(i),2);
}
for (int i = 0; i < s3.length(); i++) {
hashMap.put(s3.charAt(i),3);
}
List<String> res = new ArrayList<>();
for (String word : words) { ;
if (word.length()==1){
res.add(word);
continue;
}
int i = 1;
for (; i < word.length(); i++) {
if (!hashMap.get(word.charAt(i)).equals(hashMap.get(word.charAt(i - 1)))){
break;
}
if (i==word.length()-1){
res.add(word);
}
}
}
return res.toArray(new String[0]);
}
}
key=0, value = 1
key=2, value = 3
key=3, value = 2
key=4, value = 3
1的左边有0,长度为1,同时右边有2,长度为3,所以能够组合得到的长度是
左边的长度1 + 右边的长度3 + 1数字本身的长度1 = 长度5
最终这里的哈希表中的值的情况如下
key=0, value = 5
key=1, value = 1
key=2, value = 3
key=3, value = 2
key=4, value = 5
在这个结果中,如果又多了一个数字5,需要修改的应该是key=0, value = 6; key=5, value = 6
到这里其实应当已经非常明了了,下面直接看代码吧
代码
class Solution {
public int longestConsecutive(int[] nums) {
if (nums.length==0)return 0;
HashMap<Integer,Integer> map = new HashMap<>();
int max = 1;
for (int num : nums) {
if (map.containsKey(num)){
continue;
}
map.put(num,1);
int l = num;
int r = num;
int length = 1;
if (map.containsKey(num-1)){
l = num - map.get(num-1);
length += map.get(l);
}
if (map.containsKey(num+1)){
r = num + map.get(num+1);
length += map.get(num+1);
}
map.put(l,length);
map.put(r,length);
max = Math.max(max,length);
}
return max;
}
}