506. Relative Ranks

解题思路:

使用priority_queue。它在插入时会将数据按照由大到小的顺序插入,自然排序了。所以插入时考虑插入pair<nums[i],i>,然后根据i填充result。

注意,1) priority_queue没有迭代器,所以要遍历只能通过pop操作

2) vector要注意初始化,不然访问时可能有问题

  1. #include <queue>
  2. class Solution {
  3. public:
  4. vector<string> findRelativeRanks(vector<int>& nums) {
  5. vector<string> result(nums.size(), "");
  6. priority_queue<pair<int, int> > p;
  7. for (int i = 0; i < nums.size(); i++) {
  8. p.push(make_pair(nums[i], i));
  9. }
  10. int count = 0;
  11. while (p.empty() == false) {
  12. if (count == 0) {
  13. result[p.top().second] = "Gold Medal";
  14. }
  15. else if (count == 1) {
  16. result[p.top().second] = "Silver Medal";
  17. }
  18. else if (count == 2) {
  19. result[p.top().second] = "Bronze Medal";
  20. }
  21. else
  22. result[p.top().second] = to_string(count + 1);
  23. p.pop();
  24. count ++;
  25. }
  26. return result;
  27. }
  28. };

  


551. Student Attendance Record I

解题思路:

这道题需要注意的是,L不能连续。所以只要看L的次数有没有到2次就好了。如果1) i = 0; 2) s[i-1]不是L,s[i]是L,那么设count=0。在循序中,

最后一条语句检查A和L数目的情况。不要想复杂了,最简单的方式解决就好。

  1. bool checkRecord(string s) {
  2. int a = 0;
  3. int count = 0;
  4. for (int i = 0; i < s.length(); i++) {
  5. if (s[i] == 'A')
  6. a ++;
  7. if (s[i] == 'L') {
  8. if (i == 0 || s[i-1] != 'L')
  9. count = 0;
  10. count ++;
  11. }
  12. if (a > 1 || count > 2)
  13. return false;
  14. }
  15. return true;
  16. }

  


556. Next Greater Element III

解题思路:

首先,返回-1的有以下几种情况:1) 只有一位数;2) 所有数字呈递减顺序,如9876这种;3) 转换后的数超出INT_MAX

因此,先将数字转化为字符串。然后从后往前,寻找递减序列。假设从i到num.length()-1是递减的,那么,现将这一段倒序,

然后将num[i-1]与这一段中第一个大于它的数交换,即可。例如14653,653是递减的,所以先变为14356,然后交换4和5,

变为15346。注意:最后要比较转换后的数据和INT_MAX。考虑到是字符串比对,所以需要添加长度为10的条件,免得比较

时提前终止。

  1. int nextGreaterElement(int n) {
  2. if (n < 10)
  3. return -1;
  4. string num = to_string(n);
  5. int i;
  6. for (i = num.length() - 1; i >= 1; i--) {
  7. if (num[i-1] < num[i])
  8. break;
  9. }
  10. // digits descends, no answer
  11. if (i == 0)
  12. return -1;
  13. reverse(num.begin() + i, num.end());
  14. for (int j = i; j < num.length(); j++) {
  15. if (num[j] > num[i-1]) {
  16. char temp = num[j];
  17. num[j] = num[i-1];
  18. num[i-1] = temp;
  19. break;
  20. }
  21. }
  22. //cout << to_string(INT_MAX) << endl;
  23. // 2147483647
  24. if (num.size() == 10 && num > to_string(INT_MAX))
  25. return -1;
  26. return stoi(num);
  27. }

  


453. Minimum Moves to Equal Array Elements

解题思路:

sum() - min*length

  1. int minMoves(vector<int>& nums) {
  2. int min = nums[0];
  3. int sum = 0;
  4. for (int i = 0; i < nums.size(); i++) {
  5. sum += nums[i];
  6. if (nums[i] < min)
  7. min = nums[i];
  8. }
  9. return sum - min * nums.size();
  10. }

  


414. Third Maximum Number

https://leetcode.com/problems/third-maximum-number/#/description

解题思路:

给数组排序,然后寻找第三个最大的。。如果没有,返回最大值。

注意,因为开始时取了nums[n],遍历时又从n开始,所以count只要数2就好了。

  1. int thirdMax(vector<int>& nums) {
  2. sort(nums.begin(), nums.end());
  3. int third = nums[nums.size()-1];
  4. int count = 2;
  5. int max = third;
  6. for (int i = nums.size()-1; i >= 0; i--) {
  7. if (count > 0) {
  8. if (nums[i] < third){
  9. third = nums[i];
  10. count --;
  11. }
  12. if (count == 0)
  13. break;
  14. }
  15. }
  16. if (count != 0)
  17. return max;
  18. else
  19. return third;
  20. }

  


326. Power of Three

Given an integer, write a function to determine if it is a power of three.

Follow up:
Could you do it without using any loop / recursion?

解题思路:

先取以3为底的对数,然后乘方,看是否与原值相等。注意:1) 3^0 = 1

2) log是自然对数,要取3的要换底。获得log(3,n)之后,要round,否则会WA在243那个值。。可能是精度原因。

  1. bool isPowerOfThree(int n) {
  2. if (n == 1)
  3. return true;
  4. if (n < 3)
  5. return false;
  6. return abs(pow(3, round(log(n)/log(3))) - n) < 1e-15;
  7. }

367. Valid Perfect Square

Given a positive integer num, write a function which returns True if num is a perfect square else False.

Note: Do not use any built-in library function such as sqrt.

解题思路:

观察:2^2 - 1^2 = 2*1+1

3^2 - 2^2 = 2*2+1

...

x^2 - (x-1)^2 = 2*(x-1)+1

所以一个数的平方可以分解为1+3+5+....所以,只需要验证这个数是否为奇数和即可。

  1. bool isPerfectSquare(int num) {
  2. int i = 1;
  3. while (num > 0) {
  4. num -= i;
  5. i += 2;
  6. }
  7. return num == 0;
  8. }

  


69. Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

解题思路:

使用二分法。需要注意的是,left,right和mid要使用long,因为可能数值很大,超出int的范围。

  1. int mySqrt(int x) {
  2. if (x == 1)
  3. return x;
  4. // initiate as 0
  5. long left = 0;
  6. long right = x;
  7. long mid = left + (right - left) / 2;
  8. while (left + 1 < right) {
  9. if (mid * mid > x)
  10. right = mid;
  11. else if (mid * mid < x)
  12. left = mid;
  13. else
  14. return mid;
  15. mid = left + (right - left) / 2;
  16. }
  17. // notice
  18. return left;
  19. }

  


  

leetcode-24-exercise的更多相关文章

  1. [LeetCode] 24 Game 二十四点游戏

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  2. [LeetCode] 24. Swap Nodes in Pairs 成对交换节点

    Given a linked list, swap every two adjacent nodes and return its head. You may not modify the value ...

  3. Java实现 LeetCode 24 两两交换链表中的节点

    24. 两两交换链表中的节点 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2->3-&g ...

  4. LeetCode 24 Swap Nodes in Pairs (交换相邻节点)

    题目链接: https://leetcode.com/problems/swap-nodes-in-pairs/?tab=Description   Problem: 交换相邻的两个节点     如上 ...

  5. Leetcode: 24 Game

    You have 4 cards each containing a number from 1 to 9. You need to judge whether they could operated ...

  6. LeetCode 24. Swap Nodes in Pairs (两两交换链表中的节点)

    题目标签:Linked List 题目给了我们一组 linked list,让我们把每对nodes 互换位置. 新键一个dummy node,然后遍历list,每次建立 s1 和 s2 记录两个点,然 ...

  7. leetcode 24. 两两交换链表中的节点 及 25. K 个一组翻转链表

    24. 两两交换链表中的节点 问题描述 给定一个链表,两两交换其中相邻的节点,并返回交换后的链表. 你不能只是单纯的改变节点内部的值,而是需要实际的进行节点交换. 示例: 给定 1->2-> ...

  8. [leetcode 24] Swap Nodes in k-Group

    1 题目: 目前被墙,看不了. 2 思路: 比较简单,注意处理边界点就好 3 代码: public ListNode swapPairs(ListNode head) { int temp; if(h ...

  9. leetcode 24

    链表操作的,要注意标记头结点和边界问题. 代码如下: ListNode *swapPairs(ListNode *head) { if(head==NULL||head->next==NULL) ...

  10. Java [leetcode 24]Swap Nodes in Pairs

    题目描述: Given a linked list, swap every two adjacent nodes and return its head. For example, Given 1-& ...

随机推荐

  1. 解决git从remote clone后所有文件都改变的问题

    遇到2次这种情况了,git从remote clone项目代码后发现所有文件都要改变,因为权限改变了,可以通过git来设置忽略权限变化 git config --global core.fileMode ...

  2. 一、Postgresql的基本操作

    ---------------------------------------------------------------------------------------------------- ...

  3. 关于Servlet中的转发和重定项

    一:转发 首先转发属于服务器内部行为,通过浏览器的地址栏是看不到URL变化的.比如说客户端发送一个请求到ServletA,ServletA接收到请求,但是没有能力处理,但是ServletA知道Serv ...

  4. ZR#330. 【18 提高 3】矿石(容斥)

    题意 题目链接 Sol 挺显然的,首先对每个矿排序 那么答案就是$2^x - 2^y$ $x$表示能覆盖到它的区间,$y$表示的是能覆盖到它且覆盖到上一个的区间 第一个可以差分维护 第二个直接vect ...

  5. 使用SharePreferences存取数据(慕课笔记 )

    0.视频地址:http://www.imooc.com/video/3265 1.使用SharePreferences存取数据: public class MainActivity extends A ...

  6. Windows Azure 配置Active Directory 主机(4)

    步骤 6:设置在启动时加入域的虚拟机 若要创建其他在首次启动时加入域的虚拟机,请打开 Windows Azure PowerShell ISE,粘贴以下脚本,将占位符替换为您自己的值并运行该脚本. 若 ...

  7. 单列表变量与字符串拆分的对照(SqlServer)

    最近遇到一个问题,在SQLServer中,需要根据用户传入的一系列ID值更新对应的记录.有两种方法,一种是将这些ID值使用逗号分隔,拼接成字符串传入,一种是以表变量的方式传入.最开始,我想当然的认为传 ...

  8. jsp另外五大内置对象之config

    //配置web.xml <?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi=&q ...

  9. CF Gym 100637K Microcircuits (DP)

    题意:给你n个点,将这些点放在一个环上,问你不相交的连k条线的方案数.(没有重点) 题解:dp[i][j]表示i个点连j条线的方案数,那么新加一个点i, 情况1,i没有和之前的点相连,方案数为dp[i ...

  10. polygon 画图

    cityscape数据集,我现在想根据json文件中的polygon画出整个road的区域,这是运行的脚本.这个文件必须使用coco的pythonAPI的包,把这个脚本放在pythonAPI文件夹下就 ...