找到缺失的第一个正数
public int firstMissingPositive(int[] nums) {
HashMap hash = new HashMap();
for(int i=0;i<nums.length;i++){
hash.put(nums[i],0);
}
for(int i = 1;i<=nums.length;i++){
if (hash.containsKey(i)){
continue;
}
return i ;
}
return nums.length+1;
}
字符串相加
public String addStrings(String num1, String num2) {
int nums1length = num1.length();
int nums2length = num2.length();
int carry = 0;
StringBuilder sb = new StringBuilder();
while(nums1length >0 || nums2length >0 || carry >0){
int value1 = nums1length<=0 ? 0:num1.charAt(nums1length -1) - '0';
int value2 = nums2length<=0 ? 0:num2.charAt(nums2length-1) - '0';
int k = (value1 + value2 + carry)%10;
carry = (value1 + value2+ carry)/10;
sb.append(k);
nums1length--;
nums2length--;
}
return sb.reverse().toString();
}
排序数组重复数量
public int search(int[] nums, int target) {
HashMap has = new HashMap();
for(int i = 0;i<nums.length;i++){
has.put(nums[i], (int)has.getOrDefault(nums[i],0)+1);
}
if(has.containsKey(target)){
return (int)has.get(target);
}
return 0;
}
网友评论