力扣算法题简单(三)

    1. 写字符串需要的行数
    public int[] NumberOfLines(int[] widths, string S)
    {
        int[] res = new int[2];
        if (S.Length == 0) return res;
        res[0] = 1;
        int total = 0;
        for (int i = 0; i < S.Length; i++)
        {
            int t = widths[S[i] - 'a'];
            if ((total + t) > 100)
            {
                res[0]++;
                total = t;
            }
            else
            {
                total += t;
            }
        }
        res[1] = total;
        return res;
    }
    1. 回文链表
    public bool IsPalindrome(ListNode head)
    {
        if (head == null) return true;
        Stack<int> stack = new Stack<int>();
        List<int> list = new List<int>();

        while (head != null)
        {
            stack.Push(head.val);
            list.Add(head.val);
            head = head.next;
        }

        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] != stack.Pop())
            {
                return false;
            }
        }
        return true;
    }
    1. 学生出勤记录 I
    public bool CheckRecord(string s)
    {
        if (s.Contains("LLL"))
        {
            return false;
        }

        int aCount = 0;
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == 'A')
            {
                aCount++;
            }
            if (aCount > 1)
            {
                return false;
            }
        }
        return true;
    }
    1. 旋转字符串
   public bool RotateString(string A, string B)
    {
        if (A.Length != B.Length) return false;
        string k = $"{A}{A}";
        if (k.Contains(B))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    1. 找不同
    public char FindTheDifference(string s, string t)
    {
        int m = 0;
        int n = 0;
        for (int i = 0; i < s.Length; i++)
        {
            m += s[i];
        }
        for (int i = 0; i < t.Length; i++)
        {
            n += t[i];
        }
        return (char)(n - m);
    }
    1. 反转字符串中的元音字母
        Stack<char> stack = new Stack<char>();
        for (int i = 0; i < s.Length; i++)
        {
            if(s[i]=='a' || s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u'|| s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
            {
                stack.Push(s[i]);
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.Length; i++)
        {
            if (s[i] == 'a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'o' || s[i] == 'u' || s[i] == 'A' || s[i] == 'E' || s[i] == 'I' || s[i] == 'O' || s[i] == 'U')
            {
                sb.Append(stack.Pop());
            }else
            {
                sb.Append(s[i]);
            }
        }

        return sb.ToString();
    1. 颠倒二进制位
    public uint reverseBits(uint n)
    {
        string k = Convert.ToString(n, 2);
        if (k.Length < 32)
        {
            int differenceValue = 32 - k.Length;
            for (int i = 0; i < differenceValue; i++)
            {
                k = $"0{k}";
            }
        }

        StringBuilder sb = new StringBuilder();
        Stack<char> stack = new Stack<char>();
        for (int i = 0; i < k.Length; i++)
        {
            stack.Push(k[i]);
        }

        for (int i = 0; i < k.Length; i++)
        {
            sb.Append(stack.Pop());
        }
        return Convert.ToUInt32(sb.ToString(), 2);
    }

    1. 验证回文串
    public bool IsPalindrome(string s)
    {
        s = s.ToLower();
        StringBuilder sb = new StringBuilder();
        Stack<char> stack = new Stack<char>();
        for (int i = 0; i < s.Length; i++)
        {
            char c = s[i];
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z'))
            {
                sb.Append(c);
                stack.Push(c);
            }
        }
        string ss = sb.ToString();
        for (int i = 0; i < ss.Length; i++)
        {
            char c = ss[i];
            if (c != stack.Pop())
            {
                return false;
            }
        }
        return true;
    }
    1. 实现strStr()
    public int StrStr(string haystack, string needle)
    {
        return haystack.IndexOf(needle);
    }
    1. 仅仅反转字母
    public string ReverseOnlyLetters(string S)
    {
        Stack<char> ZimuStack = new Stack<char>();

        for (int i = 0; i < S.Length; i++)
        {
            if (IsZimu(S[i]))
            {
                ZimuStack.Push(S[i]);
            }
        }

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < S.Length; i++)
        {
            if (IsZimu(S[i]))
            {
                sb.Append(ZimuStack.Pop());
            }
            else
            {
                sb.Append(S[i]);
            }
        }
        return sb.ToString();
    }
    1. 二分查找
    public int Search(int[] nums, int target)
    {

        int low = 0;
        int hight = nums.Length - 1;

        while (low <= hight)
        {
            int mid = (low + hight) / 2;

            if (target == nums[mid])
            {
                return mid;
            }
            else if (target > nums[mid])
            {
                low = mid + 1;
            }
            else
            {
                hight = mid - 1;
            }
        }

        return -1;
    }
    1. 相对名次
public string[] FindRelativeRanks(int[] nums)
    {
        Dictionary<int, string> dic = new Dictionary<int, string>();
        List<int> list = new List<int>();
        for (int i = 0; i < nums.Length; i++)
        {
            list.Add(nums[i]);
        }

        string[] res = new string[nums.Length];
        Array.Sort(nums);
        Array.Reverse(nums);

        int temp = 3;
        for (int i = 0; i < nums.Length; i++)
        {
            if (i == 0)
            {
                dic.Add(nums[i], "Gold Medal");
            }
            else if (i == 1)
            {
                dic.Add(nums[i], "Silver Medal");
            }
            else if (i == 2)
            {
                dic.Add(nums[i], "Bronze Medal");
            }
            else
            {
                dic.Add(nums[i], (++temp).ToString());
            }
        }
        for (int i = 0; i < list.Count; i++)
        {
            res[i] = dic[list[i]];
        }

        return res;
    }
    1. 单词模式
    public bool WordPattern(string pattern, string str)
    {
        Dictionary<int, string> dic = new Dictionary<int, string>();

        string[] strArray = str.Split(' ');
        if (pattern.Length != strArray.Length) return false;

        for (int i = 0; i < pattern.Length; i++)
        {
            if (!dic.ContainsKey(pattern[i]))
            {
                if (dic.ContainsValue(strArray[i])) return false;

                dic.Add(pattern[i], strArray[i]);
            }
        }

        for (int i = 0; i < pattern.Length; i++)
        {
            if (dic[pattern[i]] != strArray[i])
            {
                return false;
            }
        }
        return true;
    }
    1. 两数之和
    public int[] TwoSum(int[] nums, int target)
    {
		 //不考虑顺序从中选取两个不同的的迭代
        for (int i = 0; i < nums.Length; i++)
        {
            for (int j = i + 1; j < nums.Length; j++)
            {
                if (nums[j] == target - nums[i])
                {
                    return new int[] { i, j };
                }
            }
        }
        return new int[] { 0, 0 };
    }
    1. 两数之和 II - 输入有序数组
    public int[] TwoSum(int[] numbers, int target) {
        for(int i = 0; i < numbers.Length; i++)
        {
            for(int j = i + 1; j < numbers.Length; j++)
            {
                if (numbers[i] + numbers[j] == target)
                {
                        return new int[] { i+1, j+1 };
                }
            }
        }
        return new int[] { 0, 0 };
    }
    1. 字符串中的第一个唯一字符
    public int FirstUniqChar(string s)
    {
        Dictionary<char, int> dic = new Dictionary<char, int>();
        foreach (var item in s)
        {
            if (!dic.ContainsKey(item))
            {
                dic.Add(item, 1);
            }
            else
            {
                dic[item] += 1;
            }
        }

        for (int i = 0; i < s.Length; i++)
        {
            if (dic[s[i]] == 1)
            {
                return i;
            }
        }
        return -1;
    }
    1. 两个列表的最小索引总和
    public string[] FindRestaurant(string[] list1, string[] list2)
    {
        var min = int.MaxValue;
        var dic = new Dictionary<string, int>();
        for (var i = 0; i < list1.Length; i++)
        {
            for (var j = 0; j < list2.Length; j++)
            {
                if (list1[i] == list2[j])
                {
                    min = Math.Min(min, i + j);
                    dic.Add(list1[i], i + j);
                }
            }
        }
        return (from r in dic.ToList()
                where r.Value == min
                select r.Key).ToArray();
    }
    1. 同构字符串
    public bool IsIsomorphic(string s, string t)
    {
        var dic = new Dictionary<int, int>();
        for (int i = 0; i < s.Length; i++)
        {
            if (!dic.ContainsKey(s[i]))
            {
                if (dic.ContainsValue(t[i])) return false;
                dic[s[i]] = t[i];
            }
        }
        for (int i = 0; i < s.Length; i++)
        {
            if (dic[s[i]] != t[i]) return false;
        }
        return true;
    }

    1. 至少是其他数字两倍的最大数
 public int DominantIndex(int[] nums)
    {
        List<int> list = new List<int>();
        foreach (var item in nums)
        {
            list.Add(item);
        }

        Array.Sort(nums);
        int max = nums[nums.Length - 1];
        for (int i = 0; i < nums.Length - 1; i++)
        {
            if (max < nums[i] * 2)
            {
                return -1;
            }
        }

        for (int i = 0; i < list.Count; i++)
        {
            if (list[i] == max)
            {
                return i;
            }
        }

        return -1;
    }
    1. 杨辉三角 II
        int[][] res = new int[rowIndex + 1][];
        for (int i = 0; i < res.Length; i++)
        {
            res[i] = new int[rowIndex + 1];
        }
        res[0][0] = 1;
        for (int i = 1; i < rowIndex + 1; i++)
        {
            res[i][0] = 1;
            for (int j = 1; j < i + 1; j++)
            {
                res[i][j] = res[i - 1][j - 1] + res[i - 1][j];
            }
        }
        return res[rowIndex];

猜你喜欢

转载自blog.csdn.net/qq_37811712/article/details/87910654