数组
给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。
将最终结果插入 nums 的前 k 个位置后返回 k 。
不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
思路及代码
遍历数组的每一个数,如果当前元素和下一个元素不一样,那么就加一.
1 2 3 4 5 6 7 8 9 10 11 12
| class Solution { public int removeDuplicates(int[] nums) { int index=0; for(int i=0;i<nums.length;i++){ if(nums[i]!=nums[index]){ if(nums[index+1]=nums[i]; index++; } return index+1; } } }
|
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
思路及代码
默认第一天买是价格最低的时候,最大利润为零.遍历数组,如果当前的价格低于初始的价格,就把当前加个赋值给最低价,否则就判断当前价格减去买入的最低价的差值和最大利润比较.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| public class Solution { public int maxProfit(int prices[]) { int minprice = Integer.MAX_VALUE; int maxprofit = 0; for (int i = 0; i < prices.length; i++) { if (prices[i] < minprice) { minprice = prices[i]; } else if (prices[i] - minprice > maxprofit) { maxprofit = prices[i] - minprice; } } return maxprofit; } }
|
给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。
在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。
返回 你能获得的 最大 利润 。
思路及代码
有一个讨巧的方法,只要后面的数字比前面的数字大就把两者的差值加起来
1 2 3 4 5 6 7 8 9 10 11
| class Solution { public int maxProfit(int[] prices) { int ans=0; for(int i=1;i<=prices.length-1;i++){ if(prices[i]>prices[i-1]){ ans+=prices[i]-prices[i-1]; } } return ans; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| class Solution { public int maxProfit(int[] prices) { int[][] dp = new int[prices.length][2];
dp[0][0] = 0; dp[0][1] = -prices[0];
for (int i = 1; i < prices.length; i++) { dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]); dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]); }
return dp[prices.length - 1][0]; } }
|
给你一个数组,将数组中的元素向右轮转 k_ 个位置,其中 k _是非负数。
思路及代码
创建一个临时数组,赋值,然后统一对原数组进行取余赋值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
class Solution { public void rotate(int nums[], int k) { int length = nums.length; int temp[] = new int[length]; for (int i = 0; i < length; i++) { temp[i] = nums[i]; } for (int i = 0; i < length; i++) { nums[(i + k) % length] = temp[i]; } } }
class Solution { public void rotate(int[] nums, int k) { int length = nums.length; k %= length; reverse(nums, 0, length - 1); reverse(nums, 0, k - 1); reverse(nums, k, length - 1); }
public void reverse(int[] nums, int start, int end) { while (start < end) { int temp = nums[start]; nums[start++] = nums[end]; nums[end--] = temp; } } }
|
给你一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false 。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public boolean containsDuplicate(int[] nums) { Arrays.sort(nums); int n = nums.length; for (int i = 0; i < n - 1; i++) { if (nums[i] == nums[i + 1]) { return true; } } return false; } }
public boolean containsDuplicate(int[] nums) { Set<Integer> res = new HashSet<Integer>(); for(int i:nums) res.add(i); return res.size()<nums.length?true:false; }
|
给你一个整数数组 nums 和一个整数 k ,判断数组中是否存在两个 不同的索引 i 和 j ,满足 nums[i] == nums[j] 且 abs(i - j) <= k 。如果存在,返回 true ;否则,返回 false
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { Map<Integer, Integer> map = new HashMap<Integer, Integer>(); int length = nums.length; for (int i = 0; i < length; i++) { int num = nums[i]; if (map.containsKey(num) && i - map.get(num) <= k) { return true; } map.put(num, i); } return false; } }
|
给你一个整数数组 nums 和两个整数 k 和 t 。请你判断是否存在 两个不同下标 i 和 j,使得 abs(nums[i] - nums[j]) <= t ,同时又满足 abs(i - j) <= k 。
如果存在则返回 true,不存在返回 false。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| class Solution { public boolean containsNearbyAlmostDuplicate(int[] nums, int indexDiff, int valueDiff) { int len = nums.length; for(int i=len-1;i>0;i--){ int a = i-indexDiff; if(a>=0 && i>=indexDiff){ for(int j=i-1;j>=a;j--){ if(Math.abs(nums[i]-nums[j])<=valueDiff){ return true; } } } else{ for(int j=i-1;j>=0;j--){ if(Math.abs(nums[i]-nums[j])<=valueDiff){ return true; } } } } return false; } }
|
给你一个 非空 整数数组 nums ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。你必须设计并实现线性时间复杂度的算法来解决此问题,且该算法只使用常量额外空间。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
class Solution { public int singleNumber(int[] nums) { int len=nums.length; int sum=0; for(int i=0;i<len;i++){ sum=sum^nums[i]; } return sum; } }
class Solution { public int singleNonDuplicate(int[] nums) { int l=0,r = nums.length-1,m; while(l<r){ m=l+(r-l)/2; if(m%2==1){ m--; } if(nums[m]==nums[m+1]){ l=m+2; }else{ r=m; } } return nums[l]; } }
class Solution { public int singleNumber(int[] nums) { Arrays.sort(nums); int n=nums.length; for(int i=0;i<n;i+=2){ if(i==n-1) return nums[n-1]; if(nums[i]!=nums[i+1]) return nums[i]; } return -1;
} }
|
给你一个整数数组 nums ,除某个元素仅出现 一次 外,其余每个元素都恰出现 三次 。请你找出并返回那个只出现了一次的元素。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| class Solution { public int singleNumber(int[] nums) { int one = 0, two = 0, three; for (int num : nums) { two |= (one & num); one ^= num; three = (one & two); two &= ~three; one &= ~three; } return one; } }
class Solution { public int singleNumber(int[] nums) { Map<Integer , Integer> map = new LinkedHashMap<>(); for(Integer num : nums){ if(!map.containsKey(num)){ map.put(num, 1); }else{ map.put(num, map.get(num) + 1); } } for(Integer key : map.keySet()){ if(map.get(key) == 1){ return key; } } return 0; } }
class Solution { public int singleNumber(int[] nums) { Arrays.sort(nums); int ans = -1; int len = nums.length; if (len == 1){ return nums[0]; } for(int i = 0; i< len;){ if (nums[i] != nums[i+2]){ ans = nums[i]; break; } i = i+3; if (i >= len-1){ ans = nums[i]; break; } } return ans; } }
|
给你一个整数数组 nums,其中恰好有两个元素只出现一次,其余所有元素均出现两次。 找出只出现一次的那两个元素。你可以按 任意顺序 返回答案。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| class Solution { public int[] singleNumber(int[] nums) { int xorsum = 0; for (int num : nums) { xorsum ^= num; } int lsb = (xorsum == Integer.MIN_VALUE ? xorsum : xorsum & (-xorsum)); int type1 = 0, type2 = 0; for (int num : nums) { if ((num & lsb) != 0) { type1 ^= num; } else { type2 ^= num; } } return new int[]{type1, type2}; } }
class Solution { public int[] singleNumber(int[] nums) { Map<Integer, Integer> freq = new HashMap<Integer, Integer>(); for (int num : nums) { freq.put(num, freq.getOrDefault(num, 0) + 1); } int[] ans = new int[2]; int index = 0; for (Map.Entry<Integer, Integer> entry : freq.entrySet()) { if (entry.getValue() == 1) { ans[index++] = entry.getKey(); } } return ans; } }
class Solution { public int[] singleNumber(int[] nums) { Map<Integer , Integer> map = new LinkedHashMap<>(); int[] res=new int[2]; for(Integer num : nums){ if(!map.containsKey(num)){ map.put(num, 1); }else{ map.put(num, map.get(num) + 1); } } int i=0; for(Integer key : map.keySet()){ if(map.get(key) == 1){ res[i++]=key; } } return res; } }
|
给定两个数组 nums1 和 nums2 ,返回 它们的交集 。输出结果中的每个元素一定是 唯一 的。我们可以 不考虑输出结果的顺序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| class Solution { public int[] intersection(int[] nums1, int[] nums2) { if (nums1 == null || nums1.length == 0 || nums2 == null || nums2.length == 0) { return new int[0]; } Set<Integer> set1 = new HashSet<>(); Set<Integer> resSet = new HashSet<>(); for (int i : nums1) { set1.add(i); } for (int i : nums2) { if (set1.contains(i)) { resSet.add(i); } } int[] resArr = new int[resSet.size()]; int index = 0; for (int i : resSet) { resArr[index++] = i; } return resArr; } }
class Solution { public int[] intersection(int[] nums1, int[] nums2) { Arrays.sort(nums1); Arrays.sort(nums2); int length1 = nums1.length, length2 = nums2.length; int[] intersection = new int[length1 + length2]; int index = 0, index1 = 0, index2 = 0; while (index1 < length1 && index2 < length2) { int num1 = nums1[index1], num2 = nums2[index2]; if (num1 == num2) { if (index == 0 || num1 != intersection[index - 1]) { intersection[index++] = num1; } index1++; index2++; } else if (num1 < num2) { index1++; } else { index2++; } } return Arrays.copyOfRange(intersection, 0, index); } }
|
给你两个整数数组 nums1 和 nums2 ,请你以数组形式返回两数组的交集。返回结果中每个元素出现的次数,应与元素在两个数组中都出现的次数一致(如果出现次数不一致,则考虑取较小值)。可以不考虑输出结果的顺序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| class Solution { public int[] intersect(int[] nums1, int[] nums2) { Map<Integer, Integer> map = new HashMap<>(); for(int num : nums1){ Integer value = map.get(num) == null ? 1 : map.get(num) + 1; map.put(num, value); } List<Integer> list = new ArrayList<>(); for(int num : nums2){ Integer value = map.get(num); if(value != null && value != 0){ list.add(num); map.put(num, value-1); } } int[] result = new int[list.size()]; int i = 0; for(Integer num : list){ result[i] = num; i++; } return result; } }
|