算法-两根指针类问题
来源:互联网 发布:诊疗指南软件下载 编辑:程序博客网 时间:2024/05/05 01:40
1 有序数组去重
给出有序数组,就地移除重复元素,保证无重复,并返回新数组长度;
举例:
已知:A=[1,1,2,3,3] 输出长度3, A=[1,2,3]
思路图:
public static int RemoveDuplicate(int[] arr) { if (arr == null || arr.Length == 0) return 0; int i = 0, j = 1; while (j < arr.Length) { if (arr[i] == arr[j]) { j++; } else { i++; arr[i] = arr[j]; j++; } } return i + 1; }
-----------------------------------------------------------------------
2 2数之和
原题:https://leetcode.com/problems/two-sum/
给出一个整形数组,找出2个元素的组合, 组合之和为某个制定的值. 要求在结果集合中不含有重复的组合。
数组可以是无序的;
举例: 输入{1,5,7,3} ,目标值为10; 从中找出3和7;
思路1: brute soution: n^2
思路2:
2.1 排序 nlogn复杂度
2.2 两根指针,i从左往右,j从右往左;
2.3 循环终止条件:i,j相遇或者达到目标值; 循环内容:sum<target->i右移; sum>target->j左移;
注意点:
1 边界条件中,需要把length<2
2 需要设置Dictionary,并且根据value取key;
public int[] TwoSum(int[] nums, int target) { int[] resArr = new int[2]; if (nums == null || nums.Length < 2) return resArr; //记录排序前索引 Dictionary<int, int> dic = new Dictionary<int, int>(); for (int k = 0; k < nums.Length; k++) { dic.Add(k, nums[k]); } Array.Sort(nums); int i = 0, j = nums.Length - 1; while (i < j) { int sum = nums[i] + nums[j]; if (sum == target) { if (nums[i] != nums[j]) { resArr[0] = GetKeyWithValue(dic, nums[i])[0]; resArr[1] = GetKeyWithValue(dic, nums[j])[0]; } else { resArr[0] = GetKeyWithValue(dic, nums[i])[0]; resArr[1] = GetKeyWithValue(dic, nums[j])[1]; } break; } else if (sum < target) { i++; } else if (sum > target) { j--; } } return resArr; } private List<int> GetKeyWithValue(Dictionary<int, int> dic, int value) { var result = dic.Where(r => r.Value == value) .Select(r => r.Key) .ToList(); foreach (var tmp in result) Console.WriteLine(tmp); return result; } }
-------------------------------------------------------
变种: 3数之和
题目要求:给出一个整形数组,找出所有的三个元素的组合,要求其组合之和为0.要求在结果集合里不含有重复的组合;
先排序,固定第一个元素,然后元素2、3使用i,j进行处理;
举例:
数组A =[-2,1,-2,2,1], 三数之和为零的有:{-2,1,1}
brute solution: n^3
Correct solution
1 排序
2 固定第0元素
3 i, j指针指向[1]元素和[n-1]元素
小结:复杂度为O(N^2),主要花在移动首尾指针上;
---------------------------------------------------
3 股票买卖
给出一个数组,第i个元素代表第i天骨架。假设最多允许一次买卖,求可能的最大利润;
这道题转换为: 求max(A[j]-A[i]), j>i时;
原题: https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
思路:一次循环解决问题
1 定义变量Min=0,profit=0;
2 循环for(int i=0;i<arr.length;i++)
3 定义min值和profit值;
代码如下:
public int MaxProfit(int[] prices) { if (prices.Length == 0 || prices == null) return 0; int min = prices[0], profit = 0; for (int i = 1; i < prices.Length; i++) { if (prices[i] < min) min = prices[i]; profit = Math.Max(profit, prices[i] - min); } return profit; }
0 0
- 算法-两根指针类问题
- 两道算法问题
- 算法练习:两指针之三色排序
- 迷宫问题两种算法
- 算法练习:两指针之三数之和为0
- 算法练习:两指针之有序数组去重
- 算法练习:两指针之三数之和为0
- 蒙特卡罗算法解决两集合相等问题
- 两类背包问题
- 算法学习--2 俩端指针问题
- 基于opencv的神经网络算法实现两类分类问题的可视化演示
- 基类指针+子类指针问题
- 两指针(1)
- 两指针(2)
- 两指针(3)
- 两指针(4)
- 两指针(5)
- 两指针(6)
- 队列 链表实现 Java
- java btbtc商城
- 情定柒氿
- 【LeetCode】 059. Spiral Matrix II
- 程序员写作能赚多少钱,怎么赚
- 算法-两根指针类问题
- 【LeetCode】 240. Search a 2D Matrix II
- 79. Word Search
- 【LeetCode】 334. Increasing Triplet Subsequence
- 1.15三道水题题解
- 周有光2017年1月14日去世,享年112岁
- python模拟开发一个网站
- The operation couldn’t be completed. Unable to log in with account ''. An unexpected failure occurre
- 【LeetCode】 077. Combinations