【263】Ugly Number

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.

思路:prime factors:素数,就是这个数只能被1和自身整除。所以检验这个数分别循环除以2、3、5的,如果最后只剩下1,则这个数就是由素数组成。

public class Solution {
public boolean isUgly(int num) {
if(num==0)return false; //way 1:
for(int i =2; i<6;i++){
while(num%i==0){
num/=i;
}
} /*way 2:
while(num%2==0){
num/=2;
}
while(num%3==0){
num/=3;
}
while(num%5==0){
num/=5;
}*/
return num==1;
}
}

【202】Happy Number

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.

Example: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

思路:循环取出一个数各个位,然后平方和,问题的关键在于这个循环什么时候结束,题干中说的很清楚,当平方和是1或者存在环路(平方和经过循环计算又是计算过的平方和)陷入死循环的时候,不再迭代。

有两种方法,一种是通过类似快慢指针的方式检测环路,这种要快很多,另一种是通过Java的set结构,每计算一个数,则添加进set,如果循环出添加过的数字,则set添加失败,所以存在环路

public class Solution {
public boolean isHappy(int n) {
//way 2 to use two pointers:fast and slow pointers t detect of there is a loop
int slow ,fast ;
slow = fast = n;
do{
slow = computeSum(slow);
fast = computeSum(fast);
fast = computeSum(fast);
}while(slow!=fast); if(slow==1)return true;
else return false;
/*way1:use the hashset to detect if there is a loop HashSet<Integer> sumSet = new HashSet<Integer>();
while(sumSet.add(n)){
int sum = 0;
while(n>0){
int m = n%10;
sum+=m*m;
n/=10; }
if(sum==1)return true;
else n = sum;;
}
return false;*/
} int computeSum(int n){
int sum = 0;
while(n>0){
int temp = n%10;
sum+= temp*temp;
n/=10;
}
return sum;
}
}

【232】 Implement Queue using Stacks

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

  • You must use only standard operations of a stack -- which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

思路: 使用栈来模拟队列的基本操作,肯定是需要两个栈,一个用来push,一个用来pop,pop的时候,要把push栈里的元素一个一个弹出来push进pop栈里

class MyQueue {
private Stack<Integer> stackForPush = new Stack<Integer>();
private Stack<Integer> stackForPop = new Stack<Integer>();
// Push element x to the back of queue.
public void push(int x) {
stackForPush.push(new Integer(x));
} // Removes the element from in front of queue.
public void pop() {
peek();//the subprocess is the same to peek,so call peek() for short.
stackForPop.pop();
} // Get the front element.
public int peek() {
if(stackForPop.isEmpty()){
while(!stackForPush.isEmpty()){
stackForPop.push(stackForPush.pop());
}
//return stackForPop.peek();
}
return stackForPop.peek();
} // Return whether the queue is empty.
public boolean empty() {
return stackForPop.isEmpty()&&stackForPush.isEmpty();
}
}
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
 
思路:把两个有序的链表合并,并保持有序。这里运用迭代思想更简单,以此对比两个链表的节点
 
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
public class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
if(l1==null)return l2;
if(l2==null)return l1;
ListNode newHead = null;
if(l1.val<l2.val){
newHead = l1;
newHead.next = mergeTwoLists(l1.next,l2);
}else{
newHead = l2;
newHead.next = mergeTwoLists(l2.next,l1);
}
return newHead;
}
}
 【231】Power of Two
Given an integer, write a function to determine if it is a power of two.
思路:判断一个数是不是2的幂,其实也就是判断这个数的二进制位数只能有一位,可以用Integer.bitCount()==1,也可以用n&n-1==0
 
public class Solution {
public boolean isPowerOfTwo(int n) {
return (n>0&&(n&(n-1))==0); }
}
 

problem 202,263、232、21、231的更多相关文章

  1. [计算机图形学] 基于C#窗口的Bresenham直线扫描算法、种子填充法、扫描线填充法模拟软件设计(二)

    上一节链接:http://www.cnblogs.com/zjutlitao/p/4116783.html 前言: 在上一节中我们已经大致介绍了该软件的是什么.可以干什么以及界面的大致样子.此外还详细 ...

  2. python27期day01:变量、常量、注释、PEP8开发规范、数据类型、Python2和Python3的区别、用户输入、流程控制语句、作业题

    1.变量:将程序中运行的中间值临时存储起来,以便下次使用. 2.变量命名规范:数字.字母.下划线.建议驼峰体.变量名具有可描述性.不能使用中文和拼音.不能数字开头和使用关键字('and', 'as', ...

  3. 在同一个硬盘上安装多个 Linux 发行版及 Fedora 21 、Fedora 22 初体验

    在同一个硬盘上安装多个 Linux 发行版 以前对多个 Linux 发行版的折腾主要是在虚拟机上完成.我的桌面电脑性能比较强大,玩玩虚拟机没啥问题,但是笔记本电脑就不行了.要在我的笔记本电脑上折腾多个 ...

  4. ABP(现代ASP.NET样板开发框架)系列之21、ABP展现层——Javascript函数库

    点这里进入ABP系列文章总目录 基于DDD的现代ASP.NET开发框架--ABP系列之21.ABP展现层——Javascript函数库 ABP是“ASP.NET Boilerplate Project ...

  5. JavaScript高级程序设计(第三版)学习笔记20、21、23章

    第20章,JSON JSON(JavaScript Object Notation,JavaScript对象表示法),是JavaScript的一个严格的子集. JSON可表示一下三种类型值: 简单值: ...

  6. Python全栈开发之21、django

    http://www.cnblogs.com/wupeiqi/articles/5237704.html http://www.cnblogs.com/wupeiqi/articles/5246483 ...

  7. IP:192.168.21.173 子网掩码:255.255.255.0 网关:192.168.21.2 DNS:8.8.8.8 8.8.4.4 1、设置IP地址、网关 ee /etc/rc.conf #编辑 ifconfig_em0="inet 192.168.21.173 netmask 255

    IP:192.168.21.173子网掩码:255.255.255.0网关:192.168.21.2DNS:8.8.8.88.8.4.41.设置IP地址.网关ee /etc/rc.conf #编辑if ...

  8. SSM(spring mvc+spring+mybatis)学习路径——2-1、spring MVC入门

    目录 2-1 Spring MVC起步 一.回顾Servlet 二.SpringMVC简介 三.搭建SpringMVC第一个案例 四.简单流程及配置 五.使用注解开发Controller 六.参数绑定 ...

  9. 21、numpy数组模块

    一.numpy简介 numpy官方文档:https://docs.scipy.org/doc/numpy/reference/?v=20190307135750 numpy是Python的一种开源的数 ...

随机推荐

  1. angular.js简介

    angularJS是一个javaScript框架.可通过<script>标签添加到HTML页面 angular通过指令扩展了HTML,且通过表达式绑定数据到html 当网页加载完毕,Ang ...

  2. Python文件打包成EXE文件

    工具:pyinstaller 安装:pip install pyinstaller 使用: 1 将依赖文件集中到一个文件夹:           pyinstaller -D -w xxx.py    ...

  3. PHP使用Redis

    首先确保安装了Redis扩展 详细的使用方式 请浏览扩展文档 https://github.com/phpredis/phpredis#connection $obj = new Redis(); $ ...

  4. Vsftp配置都没有问题 连接不上 530 Login incorrect 解决方法

    客户端输入正确的用户名和密码之后,却一直显示:530 Login incorrectLogin Failed后来发现在etc下面有个pam.d文件夹进去打开vsftpd这个文件, 发现里面对之前的用户 ...

  5. 基于JAVA语言的多线程技术

    1.简介 多线程技术属于操作系统范围内的知识: 进程与线程 可以这么理解,一个应用程序就是一个进程,在一个进程中包含至少一个线程:进程就是线程的容器,真正工作.处理任务的是线程. 进程是操作系统分配资 ...

  6. 一个完整的ant build.xml

    <?xml version="1.0" encoding="UTF-8"?> <project name="genwar" ...

  7. 转:总结Selenium WebDriver中一些鼠标和键盘事件的使用

    在使用 Selenium WebDriver 做自动化测试的时候,会经常模拟鼠标和键盘的一些行为.比如使用鼠标单击.双击.右击.拖拽等动作:或者键盘输入.快捷键使用.组合键使用等模拟键盘的操作.在 W ...

  8. string数组转化成int数组

    string idsStr = "1,2,3,4,5"; int[] ids = idsStr.Split(',').Select(Int32.Parse).ToArray();

  9. L2-006. 树的遍历

    题目链接:L2-006. 树的遍历 今天一神给我手敲二叉树模板,瞬间就领悟了,感觉自己萌萌哒! 看上去很直观! #include <iostream> #include <cstdi ...

  10. 浅谈Linux集群

      集群听起来好像就是一个很高端很的技术,其实不是的,那么集群其实就是一堆计算机的集合,给用户提供同一个服务的一组计算机,就称之为集群,对于用户而言好像就是一台计算机提供的服务,集群主要分为三大类, ...