阿华代码,不是逆风,就是我疯
你们的点赞收藏是我前进最大的动力!!
希望本文内容能够帮助到你!!
目录
第一题:移动零
class Solution {
public void moveZeroes(int[] nums) {
int dest = -1;
int cur = 0;
int tem = 0;
while(cur < nums.length){
if(nums[cur] != 0){
dest++;
tem = nums[dest] ;
nums[dest] = nums[cur];
nums[cur] = tem;
}
cur++;
}
}
}
第二题:复写零
class Solution {
public void duplicateZeros(int[] arr) {
int cur = 0 , dest = -1 , n = arr.length;
while(cur <= n){//dest位置不确定所以不能用作判断循环的条件
if(arr[cur] != 0){
dest++;
}else{
dest += 2;
}
if(dest >= n-1){
break;
}
cur++;
}
if(dest == n){
arr[n-1] = 0;
dest -= 2;
cur--;
}
//开始从后往前复写
while(cur >= 0 ){
if(arr[cur] != 0){
arr[dest] = arr[cur];
cur--;
dest--;
}else{
arr[dest] = arr[cur];
dest--;
arr[dest] = arr[cur];
cur--;
dest--;
}
}
}
}
第三题:快乐数
class Solution {
public static int sumResult(int n){
int sum = 0;
while(n != 0){
//int tem = n % 10;
//sum += tem * tem;
sum += Math.pow(n%10,2);
n = n/10;
}
return sum;
}
public boolean isHappy(int n) {
int slow = n ,fast = sumResult(n);
while(slow != fast){
slow = sumResult(slow);
fast = sumResult(sumResult(fast));
}
return slow == 1;
}
}
第四题:盛最多水的容器
class Solution {
public int maxArea(int[] height) {
int left = 0 ,right = height.length -1 , ret = 0;
while(left < right){
int v = Math.min(height[left],height[right]) * (right - left);
ret = Math.max(ret,v);
if(height[left] < height[right]){
left++;
}else{
right--;
}
}
return ret;
}
}
第五题:有效三角形的个数
class Solution {
public int triangleNumber(int[] nums) {
int end = nums.length-1;
Arrays.sort(nums);
int count = 0;
for( ; end >= 2 ; end--){
int right = end-1;
int left = 0;
while(left < right){
int tem = nums[left] + nums[right];
if(tem > nums[end]){
count += right - left;
right--;
}else{
left++;
}
}
}
return count;
}
}
第六题:和为s的两个数
LCR 179. 查找总价格为目标值的两个商品 - 力扣(LeetCode)
class Solution6 {
public int[] twoSum(int[] price, int target) {
int n = price.length;
int left = 0 , right = n-1;
int[] car = {-1,-1};
while(left < right){
int result = sum(price[left],price[right]);
if(result < target){
left++;
}else if(result > target){
right--;
}else{
car[0] = price[left];
car[1] = price[right];
return car;
}
}
return car;
}
public int sum(int a , int b){
int sum = a + b;
return sum;
}
}
第七题:三数之和
class Solution {
public List<List<Integer>> threeSum(int[] nums) {
Arrays.sort(nums);
int n = nums.length;
List<List<Integer>> ret = new ArrayList<>();
for(int i = 0 ; i < n ;){
if(nums[i] > 0){
break;
}
int left = i+1 , right = n-1 ,target = -nums[i];
while(left < right){
int sum = sum(nums[left] , nums[right]);
if(sum > target){
right--;
}else if(sum < target){
left++;
}else{
ret.add(Arrays.asList(nums[left] , nums[right] , nums[i]));
left++;
right--;
while(left < right && nums[left] == nums[left - 1]){
left++;
}
while(left < right && nums[right] == nums[right + 1]){
right--;
}
}
}
i++;
while(i < n && nums[i] == nums[i-1]){
i++;
}
}
return ret;
}
public int sum(int a , int b){
return a+b;
}
}
第八题:四数之和
强烈建议先把三数之和看完
class Solution {
public List<List<Integer>> fourSum(int[] nums, int target) {
List<List<Integer>> list = new ArrayList();
Arrays.sort(nums);
int n = nums.length;
for(int i = 0 ; i < n ; ){
//第一层循环遍历固定a遍历数组
int a = nums[i];
for(int j = i+1 ; j < n ; ){
int b = nums[j] , left = j+1 ,right = n-1;
long tem = (long)target - a - b;
while(left < right){
long sum = sum(nums[left],nums[right]);
if(sum > tem){
right--;
}else if(sum < tem){
left++;
}else{
list.add(Arrays.asList(a,b,nums[left],nums[right]));
left++;
right--;
while(left < right && nums[left] == nums[left-1]){
left++;
}
while(right > left && nums[right] == nums[right+1]){
right--;
}
}
}
j++;
while(j < n-2 && nums[j] == nums[j-1]){
j++;
}
}
i++;
while(i < n-1 && nums[i] == nums[i-1]){
i++;
}
}
return list;
}
public int sum(int a , int b){
return a+b;
}
}