链接:https://leetcode-cn.com/problems/how-many-numbers-are-smaller-than-the-current-number/
来源:LeetCode
思路:直接枚举即可,也可以排序之后使用二分查找。
class Solution {
public:
vector<int> smallerNumbersThanCurrent(vector<int>& nums) {
int n = nums.size();
int a[n], cnt = 0;
vector<int>ans(n);
for (auto num : nums) a[cnt ++] = num;
sort(a, a + n);
for (int i = 0; i < n; i ++) {
int inx = lower_bound(a, a + n, nums[i]) - a;
ans[i] = inx;
}
return ans;
}
};
我们统计出所有的队伍每一个排名对应了多少票,这样我们就得到了一个二维数组,然后我们可以对二维数组进行列排序,得票数多的在前。这里有两种方法,一种是调用系统写好的函数,一种是自定义 c m p cmp cmp 函数。
class Solution {
public:
static bool cmp(vector<int> a, vector<int> b) {
int n = a.size();
for (int i = 0; i < a.size(); i ++) {
if (a[i] != b[i]) return a[i] > b[i];//不相等就按照大小排
}
return a[26] > b[26];//所有都相等,按照最后字母的编号排列
}
string rankTeams(vector<string>& votes) {
vector<vector<int>>cnt(26, (vector<int>(27)));
for (auto str : votes) {
int len = str.length();
for (int i = 0; i < len; i ++) {
cnt[str[i] - 'A'][i] ++;
cnt[str[i] - 'A'][26] = 26 - (str[i] - 'A');//我们将字母的编号转化一下,放在每一行的最后一位。
}
}
sort(cnt.begin(), cnt.end(), cmp);
string ans;
for (int i = 0; i < 26; i ++) {
if (cnt[i][26]) {
ans += 26 - cnt[i][26] + 'A';
}
}
return ans;
}
};
class Solution {
public:
string rankTeams(vector<string>& votes) {
vector<vector<int>>cnt(26, (vector<int>(27)));
for (auto str : votes) {
int len = str.length();
for (int i = 0; i < len; i ++) {
cnt[str[i] - 'A'][i] ++;
cnt[str[i] - 'A'][26] = 26 - (str[i] - 'A');
}
}
sort(cnt.begin(), cnt.end(), greater<vector<int>>());//默认每一列从大到小排序
string ans;
for (int i = 0; i < 26; i ++) {
if (cnt[i][26]) {
ans += 26 - cnt[i][26] + 'A';
}
}
return ans;
}
};
思路:此题一开始我是先在 r o o t root root 中找到了 h e a d head head 的第一个值得位置,然后在往下判断是否符合题意。但是要注意 r o o t root root 中可能有多个 h e a d head head 的第一个值,所以我们进行 b f s bfs bfs 找到值为 h e a d head head 的第一个值得结点,然后向下 d f s dfs dfs 即可。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* 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:
bool dfs(ListNode* head, TreeNode* root) {
if (head == NULL) return true;
if (root == NULL) return false;
if (head -> val != root -> val) return false;
return dfs(head -> next, root -> left) || dfs(head -> next, root -> right);
}
bool isSubPath(ListNode* head, TreeNode* root) {
queue<TreeNode*> q; q.push(root);
bool flag;
while(!q.empty()) {
auto now = q.front(); q.pop();
if (now -> val == head -> val) {
flag = dfs(head, now);
if (flag) break;
}
if (now -> right != NULL) q.push(now -> right);
if (now -> left != NULL) q.push(now -> left);
}
return flag;
}
};
思路:从 ( 0 , 0 ) (0, 0) (0,0) 点开始 b f s bfs bfs,将四个方向上的点都放进优先队列(花费最少的先出队)中,对于每一个出队的点,如果之前使用更小的花费更新过就不在更新,否则就标记该点进行更新。遇到非箭头方向花费 + 1 +1 +1,箭头方向花费不变。
class Solution {
public:
struct Node {
int x, y, cost;
Node(){}
Node(int x, int y, int cost): x(x), y(y), cost(cost) {};
bool operator < (const Node& node) const {
return cost > node.cost;
}
};
int bfs(int x, int y, int n, int m, vector<vector<int>> grid) {
int net[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
vector<vector<bool>> vis(m, vector<bool>(n, false));
priority_queue<Node> pq; pq.push(Node(x, y, 0));
while(!pq.empty()) {
Node now = pq.top(); pq.pop();
if (now.x == m - 1 && now.y == n - 1) return now.cost;
if (vis[now.x][now.y]) continue;
vis[now.x][now.y] = true;
for (int i = 0; i < 4; i ++) {
int netx = now.x + net[i][0];
int nety = now.y + net[i][1];
if (netx < 0 || netx >= m || nety < 0 || nety >= n || vis[netx][nety]) continue;
if (i + 1 == grid[now.x][now.y]) {
pq.push(Node(netx, nety, now.cost));
} else {
pq.push(Node(netx, nety ,now.cost + 1));
}
}
}
return 0;
}
int minCost(vector<vector<int>>& grid) {
int m = grid.size();
int n = grid[0].size();
int Min = bfs(0, 0, n, m, grid);
return Min;
}
};