提示:都是自己做的,不一定是最优的解决方法!
删除排序数组中的重复项
思路分析:运用快慢指针,初始的时候二者的位置一样,当快指针指向的内容和慢指针指向的内容不一样的时候,慢指针移动,覆盖此时快指针的值。重复上面的过程即可!如果题目中不需要原地的话,也可以使用set方法!
class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null||nums.length==0){
return 0;
}
int index0=0;//慢指针!
for(int index1=0;index1<nums.length;index1++){
if(nums [index0]!=nums[index1]){
index0++;
nums[ index0]=nums[index1];
}
}
return ++index0;
}
}
买卖股票的最佳时机 ||
思路分析:遍历整个股票交易日价格列表 price,策略是所有上涨交易日都买卖(赚到所有利润),所有下降交易日都不买卖(永不亏钱)。
- 设 tmp 为第 i-1 日买入与第 i 日卖出赚取的利润,即 tmp = prices[i] - prices[i - 1] ;
- 当该天利润为正 tmp > 0,则将利润加入总利润 profit;当利润为 00 或为负,则直接跳过;
- 遍历完成后,返回总利润 profit。
class Solution {
public int maxProfit(int[] prices) {
int profit = 0;//利益
for (int i = 1; i < prices.length; i++) {
int tmp = prices[i] - prices[i - 1];
if (tmp > 0) profit += tmp;
}
return profit;
}
}
旋转数组
思路分析1:
使用额外的数组
首先新建一个数组,并复制过去;之后再根据输入的数旋转!
class Solution {
public void rotate(int[] nums, int k) {
int [] copyNum = new int [nums.length];
for(int i = 0;i < copyNum.length;i++){
copyNum[i] = nums[i];
}
k = k % nums.length;
for(int i = 0;i < nums.length;i++){
int x = 0;
if(i < k){
x = nums.length - k + i;
nums[i] = copyNum[x];
}else{
x=i-k;
nums[i]=copyNum[x];
}
}
}
}
思路分析2:
最简单的方法是旋转 k 次,每次将数组旋转 1 个元素。
public class Solution {
public void rotate(int[] nums, int k) {
int temp, previous;
for (int i = 0; i < k; i++) {
previous = nums[nums.length - 1];
for (int j = 0; j < nums.length; j++) {
temp = nums[j];
nums[j] = previous;
previous = temp;
}
}
}
}
存在重复元素
思路分析:
使用Set的无重复规则。
class Solution {
public boolean containsDuplicate(int[] nums) {
HashSet<Integer> hs = new HashSet<>();
for(int i = 0 ;i < nums.length;i++){
if(hs.contains(nums[i])){
return true;
}else{
hs.add(nums[i]);
}
}
return false;
}
}
只出现一次的数字
暴力法,循环两次
class Solution {
public int singleNumber(int[] nums) {
for(int i = 0 ;i < nums.length; i++){
int x = 1;
for(int j = 0 ; j < nums.length;j++ ){
if(i != j && nums[i] == nums [j]){
x++;
}
}
if(x == 1){
return nums[i];
}
}
return 0;
}
}
使用HashMap,将数据和它的次数存到哈希表里就可以了!只需要找到出现一次的那个数返回就可以了。
class Solution {
public int singleNumber(int[] nums) {
//特殊情况
if (nums.length == 1) {
return nums[0];
}
//HashMap
HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
//将其存入哈希表中,含义为,若该元素不存在则存入表中,并计数为1,若已经存在获取次数并加1.
for (int x : nums) {
map.put(x , map.getOrDefault(x,0) + 1);
}
//遍历出出现次数为1的情况
for (int y : map.keySet()) {
if(map.get(y) == 1){
return y;
}
}
return 0;
}
}
两个数组的交集||
思路分析:先对数组进行排序,再比较!
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
List<Integer> temp = new ArrayList<>();
Arrays.sort(nums1);
Arrays.sort(nums2);
for(int i = 0, j = 0; i < nums1.length && j < nums2.length; ){
if(nums1[i] > nums2[j]) j++;
else if(nums1[i] < nums2[j]) i++;
else {
temp.add(nums1[i]);
i++;
j++;
}
}
int[] res = new int[temp.size()];
for(int i = 0; i < res.length; i++)
res[i] = temp.get(i);
return res;
}
}
加一
public int[] plusOne(int[] digits) {
for (int i = digits.length - 1; i >= 0; i--) {
if (digits[i] == 9) {
digits[i] = 0;
} else {
digits[i] += 1;
return digits;
}
}
//如果所有位都是进位,则长度+1
digits= new int[digits.length + 1];
digits[0] = 1;
return digits;
}
移动零
public void moveZeroes(int[] nums) {
int index = 0;
for(int i = 0;i < nums.length;i++){
if(nums[i] != 0){
nums[index++] = nums[i];
}
}
for(int i = index;i < nums.length;i++){
nums[i] = 0;
}
}
}
两数之和
class Solution {
public int[] twoSum(int[] nums, int target) {
int [] temp = new int[2];
for(int i = 0;i < nums.length;i++){
for(int j = i+1;j < nums.length;j++ ){
if(nums[i] + nums[j] == target){
temp[0] = i;
temp[1] = j;
}
}
}
return temp;
}
}