c#应用程序称为解决方案(solution),可以由一个或多个项目组成。c#应用程序创建的文件类型及其说明如下图
题目链接 class Solution { public void reverseString(char[] s) { int l = 0; int r = s.length - 1; while(l<r){ s[l] ^= s[r];//s[l] = a^b s[r] ^= s[l];//s[r] = b^a^b = a; s[l] ^= s[r];//s[l] = a^
题目链接 class Solution { public boolean isPowerOfTwo(int n) { if(n<=0) return false; //8的二进制1000 7的二进制0111 做个与操作结果为零 if((n&n-1)==0) return true; else return false; } }
class Solution: #参数A:有序整数数组A #参数B:有序整数数组B #返回:一个新的有序整数数组 def merge(self, A, B): i,j = 0,0 c=[] while i<len(A) and j<len(B): if A[i]<B[j]: c.append(A[i]) i+=1 else:
贪心的本质是选择每一阶段的局部最优,从而达到全局最优 贪心没有套路,说白了就是常识性推导加上举反例。 贪心算法一般分为如下四步: 将问题分解为若干个子问题 找出适合的贪心策略 求解每一个子问题的最优解 将局部最优解堆叠成全局最优解 分发饼干 class Solution { public
题目链接 class Solution { public int maxProfit(int[] prices) { if(prices.length <= 1) return 0; int low = prices[0],res = 0; for(int i=1;i<prices.length;i++){ low = Math.min(low,prices[i]);
题1: 204. 计数质数 统计所有小于非负整数 n 的质数的数量。 示例 1: 输入:n = 10 输出:4 解释:小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 示例 2: 输入:n = 0 输出:0 示例 3: 输入:n = 1 输出:0 提示: 0 <= n <= 5 * 106 1 class Solution { 2 public int c
1748. 唯一元素的和 Solution 思路:看值域范围非常小,可以直接数组存值,就数组记录出现次数即可。 class Solution { public int sumOfUnique(int[] nums) { int len = nums.length; int[] cnt = new int[100 + 1]; for (int i = 1; i <= 100; i++) {
class Solution { public: int numSubarrayProductLessThanK(vector<int>& nums, int k) { if(k<2) return 0; int n = nums.size(); int i=0,j=0; int product=1,ans=0; while(j<n) {
题目链接 左右指针,不知道为什么挺慢的4ms public class Solution { public int maxArea(int[] height) { //定义左右指针 int l = 0,r = height.length-1; int ans = 0; while(l<r){ //找最大值 int tmp = Math.min(heigh
题目链接 easy题,没有奇奇怪怪的条件限制 class Solution{ public boolean isPalindrome(int x){ if(x<0) return false; //if(x<10) return true; int num = x; int ans = 0; while(x != 0){ ans =ans*10 + x%10;
Sumsets(http://noi.openjudge.cn/ch0305/1551/) 描述 Given S, a set of integers, find the largest d such that a + b + c = d where a, b, c, and d are distinct elements of S. 输入 Several S, each consisting of a line containing an integer 1 <= n <= 1000 ind
循环 class Solution { public: bool isPowerOfTwo(int n) { for(int i = 0; i <= 31; i++){ if(pow(2,i) == n){ return true; } } return false; } }; class Solution { public: bool
283、移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 请注意 ,必须在不复制数组的情况下原地对数组进行操作。 class Solution { public void moveZeroes(int[] nums) { int slow=0,fast=0; for(int i=0;i<
#include<bits/stdc++.h> using namespace std; int n,k; int main(){ int t; cin>>t; while(t--){ scanf("%d %d",&n,&k); if(n%4==0){ printf("%d %d %d\n",n/2,n/4,n/4); }
#include<bits/stdc++.h> #define ll long long using namespace std; ll hc,hm,k,w,a,d,h,dc,dm,t; bool flag; void rec(){ } int main(){ cin>>t; while(t--){ flag=0; scanf("%lld %lld",&hc,&dc); s
This problem is very easy to solve if using bruteforece solution, the time complexity is O(n). public double myPow(double x, int n) { if (n == 0) return 1; double res = 1; int m = Math.abs(n); for (int i =
参考:https://leetcode-cn.com/u/leetcode-solution
移除元素 class Solution { public int removeElement(int[] nums, int val) { int slow=0; int fast=0; for(fast=0;fast<nums.length;fast++) { if(nums[fast]!=val) { nums[slow++]=nums[fast
题目传送门 Solution. 我们首先发现答案是具有单调性的:砸的雪球越多构成「完美单词」的机率就越大。所有我们可以进行二分答案。 然后在 check 里面进行的操作有如下几种: 统计单词序列中每个单词出现的次数 删除位置在 \(1 \sim x\) 的字母( \(x\) 为二分的咋的雪球数量) 判断区间
力扣题目链接 位运算 class Solution { public void reverseString(char[] s) { int l = 0; int r = s.length - 1; while(l<r){ s[l] ^= s[r];//s[l] = a^b s[r] ^= s[l];//s[r] = b^a^b = a; s[l] ^= s[r];//s
斐波那契数 class Solution { public int fib(int n) { if (n < 2) return n; int p = 0, q = 0, ans = 1; //滑动数组 for (int i = 2; i <= n; i ++) { p = q; q = ans; ans = p
对于此类问题来说,我们可以考虑使用哈希表来进行求解,如果使用哈希表,那么我们可以得到代码如下: class Solution { public: int findRepeatNumber(vector<int>& nums) { unordered_map<int,int>map; for(int i = 0;i<nums.size();++i){ if(map[nums[i] > 0){
文章目录 [2148. 元素计数](https://leetcode-cn.com/problems/count-elements-with-strictly-smaller-and-greater-elements/)[2149. 按符号重排数组](https://leetcode-cn.com/problems/rearrange-array-elements-by-sign/)[2150. 找出数组中的所有孤独数字](https://lee
'''在一个长为a字符串中找到第一个只出现一次的字符,并返回它的位置, 如果没有则返回 -1(需要区分大小写).(从0开始计数)'''class Solution: def FirstNotRepeatingChar(self, s): temp = OrderedDict() for i in s: if i not in temp: te