LeetCode第一周总结
LeetCode 383. Ransom Note
https://leetcode.com/problems/ransom-note/ 原题地址
题目描述
Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false.
Each letter in the magazine string can only be used once in your ransom note.
Note:
You may assume that both strings contain only lowercase letters.
canConstruct("a", "b") -> false canConstruct("aa", "ab") -> false canConstruct("aa", "aab") -> true
解法:
class Solution { public: bool canConstruct(string ransomNote, string magazine) { vector<int> num(26); for(int i = 0; i < magazine.size(); i++)## 遍历 magazine,巧妙利用数组下标 num[magazine[i] - 'a']++; for(int i = 0; i < ransomNote.size(); i++){ num[ransomNote[i] - 'a']--; if(num[ransomNote[i]- 'a'] < 0)## 出现负数则返回false return false; } return true; } };
LeetCode 344. Reverse String
https://leetcode.com/problems/reverse-string/原题地址
Write a function that reverses a string. The input string is given as an array of characters char[]
.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
You may assume all the characters consist of printable ascii characters.
Example 1:
Input: ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
Example 2:
Input: ["H","a","n","n","a","h"] Output: ["h","a","n","n","a","H"]
解法:
简单的申请一个char类型的字符,然后遍历就可以
运行时间 > 43%,效率一般
class Solution { public: void reverseString(vector<char>& s) { for(int i = 0; i < s.size()/2; i++){ char c = s[i]; s[i] = s[s.size() -i -1]; s[s.size() -i -1] = c; } } };#52ms faster than 43%
LeetCode 67. Add Binary
题目描述:
Given two binary strings, return their sum (also a binary string).
The input strings are both non-empty and contains only characters 1
or 0
.
Example 1:
Input: a = "11", b = "1" Output: "100"
Example 2:
Input: a = "1010", b = "1011" Output: "10101"
解法:
1、统计两个字符串的长度,
2、如果字符串先遍历到首位则补零
3、从右向左逐位相加(并加上进位carry)
4、计算当前位和进位值
5、最后判断进位值是否为1,
例: string1 = "1101"; string2 = "111"变为->"0111"
确定进位carry的值,因为是二进制 sum % 2 为当前位置的值,sum / 2 为进位值
3/2 == 1进位1,2/2进位1,1/2进位0,0/2进位0
3%2 == 1当前位1,2%2当前位0,1%2当前位1,0/2当前位0
## 普通写法,便于理解
class Solution {
public:
string addBinary(string a, string b) {
int num_a = a.size() - 1;
int num_b = b.size() - 1;
string res;
int carry = 0;
while(num_a >= 0 || num_b >= 0){
int sum = 0;
sum += num_a >= 0? a[num_a--] - '0' : 0;
sum += num_b >= 0? b[num_b--] - '0' : 0;
sum += carry;
res = to_string(sum % 2) + res;
carry = sum / 2;
}
return carry == 1 ? '1' + res: res;
}
};
# 直接用Carry位作为求和值
class Solution { public: string addBinary(string a, string b) { int num_a = a.size() - 1; int num_b = b.size() - 1; string res; int carry = 0; while(num_a >= 0 || num_b >= 0){ carry += num_a >= 0? a[num_a--] - '0' : 0; carry += num_b >= 0? b[num_b--] - '0' : 0; res = to_string(carry % 2) + res; carry = carry / 2; } return carry == 1 ? '1' + res: res; } };
LeetCode 415. Add Strings 和上面解法一样