摘要:给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。 C++ class Solution { public: int firstUniqChar(string s) { unordered_map<char, int> m; for (char c : s) m[
阅读全文
摘要:请编写一个函数,其功能是将输入的字符串反转过来。 C++ class Solution { public: string reverseString(string s) { int left = 0, right = s.size() - 1; while (left < right) { swap
阅读全文
摘要:给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 C++ class Solution { public: void moveZeroes(vector<int>& nums) { for (int i = 0, j = 0; i < nums.size
阅读全文
摘要:给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 说明: 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? C++ class Solution { public: int singleNumber(vector<int>& nu
阅读全文
摘要:给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 C++ class Solution { public: void rotate(vector<int>& nums, int k) { vector<int> t = nums; for (int i = 0; i < num
阅读全文
摘要:给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 C++ class Solution { public: int max
阅读全文