leetcode题解日练--2016.6.18

2024-03-06 10:48
文章标签 leetcode 18 题解 日练 2016.6

本文主要是介绍leetcode题解日练--2016.6.18,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

编程新手,尽量保证每天至少3道leetcode题,仅此记录学习的一些题目答案与思路,尽量用多种思路来分析解决问题,不足之处还望指出。

今日题目:1、判断3的幂次;2、判断2的幂次;3、丑数;4、去除有序链表中的重复元素;5、快乐数

326. Power of Three | Difficulty: Easy

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、对输入的n进行判断,等于0返回false,如果不为0,每次对n%3进行判断,等于0说明能除尽,n=n/3,否则直接返回false,一直循环到n==1结束循环
代码:

class Solution {
public:bool isPowerOfThree(int n) {if (n==0)return false;while(n!=0){if(n==1)return true;if(n%3!=0)return false;n/=3;}return true;}
};

结果:116ms Your runtime beats 99.59% of cppsubmissions.
2、首先用一段python代码求出最大的int型的3的次方数是1162261467

max_3  = 0
for i in range(32):if (3**i<2**32):max_3 = 3**i
print max_3

然后再判断输入的数是否能被1162261467整除即可。
代码:

class Solution {
public:bool isPowerOfThree(int n) {return n>0&&(1162261467 % n == 0);}
};

结果:132ms,Your runtime beats 78.27% of cppsubmissions.

231. Power of Two | Difficulty: Easy

Given an integer, write a function to determine if it is a power of two.
描述:给定一个整数,判断是不是2的次方。
思路:
1、当然可以和上题判断3的次方一样的做法,但是这里不妨利用下2的次方的规律,2的幂次的规律是展开成二进制只有一个1.例如2:10;4:100;8:1000等等,那么知道了这个规律很快就能写出代码了。
代码:

class Solution {
public:bool isPowerOfTwo(int n) {return ((n&n-1)==0&&n>0);}
};

8ms Your runtime beats 12.81% of cppsubmissions.

class Solution {
public:bool isPowerOfTwo(int n) {if (n==0)return false;while(n!=0){if(n==1)return true;if(n%2!=0)return false;n/=2;}return true;}
};

8ms Your runtime beats 12.81% of cppsubmissions.

263. Ugly Number | Difficulty: Easy

Write a program to check whether a given number is an ugly number.

Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.

Note that 1 is typically treated as an ugly number.
题意:丑数是指的素因子只包含2,3,5的正数,1是一个特殊的丑数,因为其不包含2、3、5。
思路:
1、每次去判断是否能被2、3、5之间的一个数整除,如果可以就更新num,如果不行就直接返回false;
代码:

class Solution {
public:bool isUgly(int num) {while(num>0){if(num==1)return true;else if (num%2==0)num/=2;else if(num%3==0)num/=3;else if(num%5==0)num/=5;elsereturn false;}return false;}
};

结果:8ms Your runtime beats 8.34% of cppsubmissions.

83. Remove Duplicates from Sorted List | Difficulty: Easy

Given a sorted linked list, delete all duplicates such that each element appear only once.

For example,
Given 1->1->2, return 1->2.
Given 1->1->2->3->3, return 1->2->3.
题意:给定一个排好序的链表,删除所有连续出现的重复值保证每个值只出现一次。
思路:
从头开始遍历链表,如果当前节点的值和下一个节点相等,那么直接将当前节点指向下一个节点的下一个节点,如果不相等,就将当前节点切换为下一个节点,直到最后一个NULL节点结束循环返回head。

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:ListNode* deleteDuplicates(ListNode* head) {if(head==NULL) return head;ListNode* pNode = head;while(pNode){if(pNode->next&&pNode->next->val==pNode->val)pNode->next = pNode->next->next;elsepNode = pNode->next;}return head;}
};

结果:16ms Your runtime beats 12.68% of cppsubmissions.

202. Happy Number | Difficulty: Easy

Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.
这里写图片描述
题意:将数字各位的平方相加,然后得到的相加之后再循环上一步操作,如果最后能等于1就是Happy Number,也存在不会停下的的可能。
思路:
1、首先,观察两个例子,一个是题目给的19->82->68->100->1->1->1……
另外一个不是Happy Number的数字 9->81->65->61->37->58->89->145->42->20->4->16->37->58,注意到这里58又进入到了循环,因此可以考虑设置两个指针,一快一慢,然后如果不是丑数,相遇的时候一定不是1,相反如果是丑数那么相遇一定是1。

class Solution {
public:int digitSquareSum(int n) {int sum = 0, tmp;while (n) {tmp = n % 10;sum += tmp * tmp;n /= 10;}return sum;}bool isHappy(int n) {int slow, fast;slow = fast = n;do {slow = digitSquareSum(slow);fast = digitSquareSum(fast);fast = digitSquareSum(fast);} while(slow != fast);if (slow == 1) return 1;else return 0;}};

结果:4ms Your runtime beats 41.98% of cppsubmissions.
2、
1 : 1
2 : 4 -> … -> 4
3 : 9 -> … -> 4
4 : 4 -> … -> 4
5 : 25 -> … -> 4
6 : 4 -> … -> 4
7 : 49 -> … -> 1
8 : 4 -> … -> 4
9 : 25 -> … -> 4

利用1-9中只有1和7是Happy Number这一条件,写出如下代码

class Solution {
public:bool isHappy(int n) {while(n>9){int next = 0;while(n){next+=(n%10)*(n%10); n/=10;}n = next;}return n==1||n==7;
} 
};

结果:0ms Your runtime beats 98.88% of cppsubmissions
3、用一个集合来存出现过的情况

class Solution {
public:bool isHappy(int n) {set<int> s;while (n != 1) {int t = 0;while (n) {t += (n % 10) * (n % 10);n /= 10;}n = t;if (s.count(n)) break;else s.insert(n);}return n == 1;}
};

结果: 4ms Your runtime beats 41.98% of cppsubmissions.

参考资料
1、https://leetcode.com/discuss/33055/my-solution-in-c-o-1-space-and-no-magic-math-property-involved
2、http://www.cnblogs.com/grandyang/p/4447233.html

这篇关于leetcode题解日练--2016.6.18的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/779813

相关文章

Python实现数据清洗的18种方法

《Python实现数据清洗的18种方法》本文主要介绍了Python实现数据清洗的18种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录1. 去除字符串两边空格2. 转换数据类型3. 大小写转换4. 移除列表中的重复元素5. 快速统

哈希leetcode-1

目录 1前言 2.例题  2.1两数之和 2.2判断是否互为字符重排 2.3存在重复元素1 2.4存在重复元素2 2.5字母异位词分组 1前言 哈希表主要是适合于快速查找某个元素(O(1)) 当我们要频繁的查找某个元素,第一哈希表O(1),第二,二分O(log n) 一般可以分为语言自带的容器哈希和用数组模拟的简易哈希。 最简单的比如数组模拟字符存储,只要开26个c

leetcode-24Swap Nodes in Pairs

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode swapPairs(L

leetcode-23Merge k Sorted Lists

带头结点。 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val = x; }* }*/public class Solution {public ListNode mergeKLists

C++ | Leetcode C++题解之第393题UTF-8编码验证

题目: 题解: class Solution {public:static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num &

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟)

【每日一题】LeetCode 2181.合并零之间的节点(链表、模拟) 题目描述 给定一个链表,链表中的每个节点代表一个整数。链表中的整数由 0 分隔开,表示不同的区间。链表的开始和结束节点的值都为 0。任务是将每两个相邻的 0 之间的所有节点合并成一个节点,新节点的值为原区间内所有节点值的和。合并后,需要移除所有的 0,并返回修改后的链表头节点。 思路分析 初始化:创建一个虚拟头节点

C语言 | Leetcode C语言题解之第393题UTF-8编码验证

题目: 题解: static const int MASK1 = 1 << 7;static const int MASK2 = (1 << 7) + (1 << 6);bool isValid(int num) {return (num & MASK2) == MASK1;}int getBytes(int num) {if ((num & MASK1) == 0) {return

【JavaScript】LeetCode:16-20

文章目录 16 无重复字符的最长字串17 找到字符串中所有字母异位词18 和为K的子数组19 滑动窗口最大值20 最小覆盖字串 16 无重复字符的最长字串 滑动窗口 + 哈希表这里用哈希集合Set()实现。左指针i,右指针j,从头遍历数组,若j指针指向的元素不在set中,则加入该元素,否则更新结果res,删除集合中i指针指向的元素,进入下一轮循环。 /*** @param

C - Word Ladder题解

C - Word Ladder 题解 解题思路: 先输入两个字符串S 和t 然后在S和T中寻找有多少个字符不同的个数(也就是需要变换多少次) 开始替换时: tips: 字符串下标以0开始 我们定义两个变量a和b,用于记录当前遍历到的字符 首先是判断:如果这时a已经==b了,那么就跳过,不用管; 如果a大于b的话:那么我们就让s中的第i项替换成b,接着就直接输出S就行了。 这样

LeetCode:64. 最大正方形 动态规划 时间复杂度O(nm)

64. 最大正方形 题目链接 题目描述 给定一个由 0 和 1 组成的二维矩阵,找出只包含 1 的最大正方形,并返回其面积。 示例1: 输入: 1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4 示例2: 输入: 0 1 1 0 01 1 1 1 11 1 1 1 11 1 1 1 1输出: 9 解题思路 这道题的思路是使用动态规划