概述
1047. Remove All Adjacent Duplicates In String
Given a string S of lowercase letters, a duplicate removal consists of choosing two adjacent and equal letters, and removing them.
We repeatedly make duplicate removals on S until we no longer can.
Return the final string after all such duplicate removals have been made. It is guaranteed the answer is unique.
Example 1:
Input: “abbaca” Output: “ca”
Explanation: For example, in “abbaca” we could remove “bb” since the letters are adjacent and equal, and this is the only possible move. The result of this move is that the string is “aaca”, of which only “aa” is possible, so the final string is “ca”.
Note:
1 <= S.length <= 20000
S consists only of English lowercase letters.
Python3:
class Solution:
def removeDuplicates(self, S: str) -> str:
#空栈
stack = []
for i in S:
if (stack and i == stack[-1]):
stack.pop()
else:
stack.append(i)
return "".join(stack)
C++:
class Solution{
public:
string removeDuplicates(string S){
string res;
for(auto i : S){
if(res.size() && res.back == i){
res.pop_back();
}else res += i;
}
return res;
}
};
1119. Remove Vowels from a String
Given a string s, remove the vowels ‘a’, ‘e’, ‘i’, ‘o’, and ‘u’ from it, and return the new string.
Example 1:
Input: s = “leetcodeisacommunityforcoders”
Output: “ltcdscmmntyfrcdrs”
Example 2:
Input: s = “aeiou”
Output: “”
Constraints:
1 <= s.length <= 1000
s consists of only lowercase English letters.
python:
class Solution:
def removeVowels(self, s: str) -> str:
res = ""
for string in s:
if string not in "aeiou":
res += string;
return res
C++:
class Solution {
public:
string removeVowels(string s) {
string S;
for(int i = 0; i < s.size(); i++){
if(s[i] != 'a' && s[i] != 'e' && s[i] != 'i' && s[i] != 'o' && s[i] != 'u'){
S += s[i];
}
else{
continue;
}
}
return S;
}
};
You are given two integers, x and y, which represent your current location on a Cartesian grid: (x, y). You are also given an array points where each points[i] = [ai, bi] represents that a point exists at (ai, bi). A point is valid if it shares the same x-coordinate or the same y-coordinate as your location.
Return the index (0-indexed) of the valid point with the smallest Manhattan distance from your current location. If there are multiple, return the valid point with the smallest index. If there are no valid points, return -1.
The Manhattan distance between two points (x1, y1) and (x2, y2) is abs(x1 - x2) + abs(y1 - y2).
Example 1:
Input: x = 3, y = 4, points = [[1,2],[3,1],[2,4],[2,3],[4,4]]
Output: 2
Explanation: Of all the points, only [3,1], [2,4] and [4,4] are valid. Of the valid points, [2,4] and [4,4] have the smallest Manhattan distance from your current location, with a distance of 1. [2,4] has the smallest index, so return 2.
Example 2:
Input: x = 3, y = 4, points = [[3,4]]
Output: 0
Explanation: The answer is allowed to be on the same location as your current location.
Example 3:
Input: x = 3, y = 4, points = [[2,3]]
Output: -1
Explanation: There are no valid points.
Constraints:
1 <= points.length <= 104
points[i].length == 2
1 <= x, y, ai, bi <= 104
中文:给你两个整数 x 和 y ,表示你在一个笛卡尔坐标系下的 (x, y) 处。同时,在同一个坐标系下给你一个数组 points ,其中,points[i] = [ai, bi] 表示在 (ai, bi) 处有一个点。当一个点与你所在的位置有相同的 x 坐标或者相同的 y坐标时,我们称这个点是 有效的 。请返回距离你当前位置 曼哈顿距离 最近的 有效 点的下标(下标从 0 开始)。如果有多个最近的有效点,请返回下标 最小的一个。如果没有有效点,请返回 -1 。 两个点 (x1, y1) 和 (x2, y2) 之间的 曼哈顿距离 为 abs(x1 - x2) + abs(y1 - y2) 。
C++:暴力遍历
class Solution {
public:
int nearestValidPoint(int x, int y, vector<vector<int>>& points) {
int cnt = 0,index = 0,min_sum = INT_MAX;
for(int i = 0; i<points.size();i++){
if(points[i][0] == x || points[i][1] == y){
cnt++;
if(abs(points[i][0] - x) + abs(points[i][1] - y) < min_sum){
min_sum = abs(points[i][0] - x) + abs(points[i][1] - y);
index = i;
}
}
}
if(!cnt) return -1;
return index;
}
};
1780 Check if Number is a Sum of Powers of Three
我傻了,这题,脑子没转过来,其实这题很简单。
十进制转三进制,如果成立那么三进制都是0或1,据此可以进行判断
C++:
class Solution {
public:
bool checkPowersOfThree(int n) {
while (n > 0){
if(n % 3 == 2){
return false;
}
n /= 3;
}
return true;
}
};
224. Basic Calculator
Given a string s representing an expression, implement a basic calculator to evaluate it.
Example 1:
Input: s = “1 + 1”
Output: 2
Example 2:
Input: s = " 2-1 + 2 "
Output: 3
Example 3:
Input: s = “(1+(4+5+2)-3)+(6+8)”
Output: 23
Constraints:
1 <= s.length <= 3 * 105
s consists of digits, ‘+’, ‘-’, ‘(’, ‘)’, and ’ '.
s represents a valid expression.
这一题一看到就想到用栈来做,408里面有这种题目,使用栈来存符号,然后进行计算,因为这里只需要进行加或减,所以不是特别复杂,最重要的是逻辑必须理清楚。
C++:
class Solution {
public:
int calculate(string s) {
stack<int> sym;
sym.push(1);
int sum = 0;
int punc = 1;
for(int i = 0; i < s.length(); i++){
if(s[i] == ' '){
continue;
} else if (s[i] == '+') {
punc = sym.top();
} else if (s[i] == '-') {
punc = -sym.top();
} else if (s[i] == '(') {
sym.push(punc);
} else if (s[i] == ')') {
sym.pop();
} else{
long num = 0;
while(i < s.length() && s[i] >= '0' && s[i] <= '9') {
num = num * 10 + s[i] - '0';
i++;
}
i--;
sum += punc * num;
}
}
return sum;
}
};
最后
以上就是壮观面包为你收集整理的【算法-easy】算法刷题记录的全部内容,希望文章能够帮你解决【算法-easy】算法刷题记录所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复