目录
文章目录
前言
一、给定一个数组将所有0,移动到末尾,但是其他数字顺序不动
二、两数之和
三、字符串操作
前言
前言
双指针+字符串
一、给定一个数组将所有0,移动到末尾,但是其他数字顺序不动
package double_point2;
import java.util.Arrays;
public class TestOne {
public void moveZeroes(int[] nums) {
int len = nums.length-1;
int count = 0;
int i = 0;
while(i<=len){
if(nums[i]!=0){
nums[i-count] = nums[i];
}else{
count++;
}
i++;
}
while(count>0){
nums[len-count+1] = 0;
count--;
}
}
public static void main(String[] args) {
int[] nums = {0, 1, 0, 3, 12};
TestOne testOne = new TestOne();
testOne.moveZeroes(nums);
System.out.println(Arrays.toString(nums));
}
}
二、两数之和
package double_point2;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
public class TestTwo {
public int[] twoSum(int[] numbers, int target) {
for(int i = 0,j = numbers.length-1;i map = new HashMap<>();
// int[] result = new int[2];
// for(int i = 0;i
三、字符串操作
package double_point2;
import com.sun.source.tree.NewArrayTree;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class TestThree {
public String[] findWords(String[] words) {
String s1 = "qwertyuiopQWERTYUIOP";
String s2 = "asdfghjklASDFGHJKL";
String s3 = "zxcvbnmZXCVBNM";
List list = new ArrayList<>();
for(String str:words){//对字符串数组进行遍历,如果字符串满足isContents中的条件则list添加该字符串
if(isContents(str,s1,s2,s3)){
list.add(str);
}
}
String[] result = new String[list.size()];
for(int i = 0;i
二、两数之和



