Commit 410f33ae by 李楚霏

作业

parent cf84d1f9
/**
* @param {string} S
* @return {string}
*/
var removeOuterParentheses = function(S) {
let res ='';
let open = 0;
for(let c of S) {
//特别注意这个计数,当条件成立自增为1了,那么下次判断时他就自增为2了,
//所以这也是为什么,比如‘(()’判断右括号可以用open-->1,条件也成立的原因
if(c === '(' && open++ >0) res += c;
if(c === ')' && open-- >1) res += c;
}
return res;
};
/**
* @param {number} n
* @return {string[]}
*/
var fizzBuzz = function(n) {
let res = [];
for(let i =1; i <= n; i++) {
if(i %3===0 && i % 5 === 0) {res.push('FizzBuzz');}
else if(i % 3 === 0) {res.push('Fizz');}
else if(i % 5 === 0) {res.push('Buzz');}
else res.push(`${i}`);
}
res.join(',');
return res;
};
// 递归解法
class Solution {
public:
int addDigits(int num) {
if(num < 10) {
return num;
}
int next = 0;
while(num != 0) {
next += num % 10;
num /= 10;
}
return addDigits(next);
}
};
\ No newline at end of file
/**解题思路:使用递归,
* 中序遍历的思路就是先遍历左子树,然后读取根节点,最后再遍历右子树
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int>res = {};
if(!root){
return res;
}
return helper(root, res);
}
vector<int>helper(TreeNode* node, vector<int> &res) {
if (node->left) helper(node->left, res);
res.push_back(node->val);
if(node->right) helper(node->right, res);
return res;
}
};
\ No newline at end of file
/**解题思路:使用队列先进先出的思想
* // Definition for a Node.
* function Node(val,children) {
* this.val = val;
* this.children = children;
* };
*/
/**
* @param {Node} root
* @return {number[][]}
*/
var levelOrder = function(root) {
let res= [];
let queue =[];
if(!root) return res;
queue.push(root);
while(queue.length > 0) {
let len = queue.length;
let tmp = [];
for(let i = 0; i < len; i++) {
let node = queue.shift();
tmp.push(node.val);
queue.push(...node.children);
}
res.push(tmp);
}
return res;
};
/**
* 解题思路: 先排序,后比较字符串
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
let s1 = s.split('').sort().join('');
let t1 = t.split('').sort().join('');
if(s1 == t1) {
return true;
}else {
return false;
}
};
/*
自己没有做出来的原因:1. 对容器的相关函数不了解,没有想到用容器封装好的函数来做题
2.对STL的相关知识不熟悉,比如如何去排序字符串也是没想到用sort来做
也不清楚sort可以排序字符串的字符;
解题思路: 首先就是排序了,然后用一个unorder_map存储键值对<str, int> ,str作为键,int作为值,
异位词排序完的字符串是一样的,所以只要排序完的字符串作为键存入map中,后面遍历到的元素只要排序完得到的
结果也和map存的键一样,那么就说明它们是异位词,就送入res
利用unorder_map的count可以判断键所对应的值是否存在(unorder_map不存储重复的元素),若存在则直接送进结果,
否则表示res当前还不存在str,把str送进res中。接着unorder_map键所对应的值(也就是用来表示res下标)自增
*/
#include<iostream>
#include<vector>
#include<unorder_map>
using namespace std;
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
int sub =0;
string tmp;
vector<vector<string>>res = NULL;
unordered_map<string, int> work;
for(auto str: strs) {
tmp = str;
sort(tmp.begin(), tmp.end());
if(work.count(tmp)) {
res[work[tmp]].push_back(str);
}else {
vector<string> vec(1, str);
res.push_back(vec);
work[tmp] = sub++;
}
}
return res
}
};
int main() {
Solution s;
vector<string> s = {"eat","tea","tan","ate","nat","bat"}
vector<vector<string>> res;
res = s.groupAnagrams(s);
return 0;
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment