LeeCode C#代码随想录 数组

1.两数之和 暴力解和hashmap (数组)
    //hashmap法
    public int[] TwoSum(int[] nums, int target)
    {    
        Dictionary<int, int> hashdic = new Dictionary<int, int>();

        for(int i = 0; i < nums.Length; i++)
        {    
            //q放需要在map中找的值 q=key时找到 key存值 value存下标
            
            int q = target - nums[I];
            hashdic[q]不能是他自己 必须是别人才能凑够两个数返回
            if (hashdic.ContainsKey(q) && hashdic[q]!=i)
            {
                return new int[] { i, hashdic[q] };
            }
            if (!hashdic.ContainsKey(q))
            {
                hashdic.Add(nums[i], i);
            }
        }
        return new int[] { 0, 0 };
    }
    //暴力法   
    public int[] TwoSum(int[] nums, int target) {
        int len = nums.Length;
        for(int i  = 0;i<len-1;i++){
            for(int j = i+1;j<len;j++){
                if(target==nums[i]+nums[j]){
                    return new int[]{i,j};
                }
            }
        }
        return new int[]{0,0};
    }
26.删除有序数组中的重复项 快慢指针法 (数组)
public class Solution {
    public int RemoveDuplicates(int[] nums) {
        int slow = 0;
        int fast = 1;
        while(fast<nums.Length){
            if(nums[slow]!=nums[fast]){
                slow++;
                nums[slow] = nums[fast];
            }
            fast++;
        }
        return slow+1;
    }
}
27.移除元素 快慢指针法 (数组)
public class Solution {
    public int RemoveElement(int[] nums, int val) {
        
        int fst = 0, lst = 0;
        while (fst < nums.Length) {
            if(nums[fst]==val){
                fst++;
            }else{
                nums[lst] = nums[fst];
                fst++;
                lst++;
            }
        }
        
        return lst; 
    }
}

704.二分查找
public class Solution {
    public int Search(int[] nums, int target) {
        int left,right,mid;
        left=0;
        right=nums.Length-1;
        while(left<=right){
            mid=(left+right)/2;
            int middle = nums[mid];
            if(middle>target){
                right=mid-1;
            }else if(middle<target){
                left=mid+1;
            }else return mid;
                
       }
        return -1;
}
}
977.有序数组的平方
public class Solution {
    public int[] SortedSquares(int[] nums) {
        int len = nums.Length;
        int[] mid = new int[len];
        for(int i = 0;i<len;i++){
           mid[i] = nums[i]*nums[i];
        }
        for(int i = 0;i<nums.Length;i++){
            for(int q = i+1;q<nums.Length;q++){
            if(mid[q]<mid[i]){
                int m = mid[i];
                mid[i] = mid[q];
                mid[q] = m;
            }       
            }
        }
        //Array.Sort(mid);
        return mid;    
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值