题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 思路: 对这个数字的每一位求存在1的数字的…
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. Hint: Beware of overflow.…
给定一个整数 n,计算所有小于等于 n 的非负数中数字1出现的个数. 例如: 给定 n = 13, 返回 6,因为数字1出现在下数中出现:1,10,11,12,13. 详见:https://leetcode.com/problems/number-of-digit-one/description/ Java实现: 方法一: class Solution { public int countDigitOne(int n) { StringBuilder sb=new StringBuilder()…
题目: Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 代码: class Solution {…
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down). Write a function to determine if a number is strobogrammatic. The number is represented as a string. For example, the numbers "69", "…
Determine whether an integer is a palindrome. Do this without extra space. 题目标签:Math 题目给了我们一个int x, 让我们判断它是不是回文数字. 首先,负数就不是回文数字,因为有个负号. 剩下的都是正数,只要把数字 reverse 一下,和原来的比较,一样就是回文. 当有overflow 的时候,返回一个负数就可以了(因为负数肯定不会和正数相等). Java Solution: Runtime beats 61.…
Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?…
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一位的平方和,平方和为1则为happy数,不为1则将原数替换为此平方和,继续上述步骤,直到等于1(为happy数),或者进入不包含1的无限循环(非happy数). 测试用例:19为happy数: 解题思路: 定义set集合存每次计算得到的需判断数n,循环计算新的原数n,终止条件为n等于1或者set中出…
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. For example,Given nums = [0, 1, 3] return 2. Note:Your algorithm should run in linear runtime complexity. Could you implement it usi…
Long Time No See !   题目链接https://leetcode.com/problems/palindrome-number/?tab=Description   首先确定该数字的位数.按照已知数字x对10进行多次求余运算,可以得到数字位数. 具体思路: 1.每次取出该数字的最高位和最低位进行比较. 2.如果不相等则直接返回FALSE, 3.如果相等修改x的值(去掉最高位也同时去掉最低位)其中去掉最高位可以通过求模运算,去掉最低位可以采用除以10 4.进行循环直到x的值不大于…
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should retu…
Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to…
Determine whether an integer is a palindrome. Do this without extra space.(不要使用额外的空间) Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You…
class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j++) w += (A[j]>>(i-))&t; result+= (w%)<<(i-); //若是除过一个数之外,其他数重复k次,则将此处的3改为k } return result; } }; 利用二进制的位运算来解决这个问题,比如输入的数组是[2,2,3,2] 它们对应的二…
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by…
class Solution { public: bool isPalindrome(int x) { ,init=x; ) return true; ) return false; ){ r=r*+init%; init=init/; } if(r==x) return true; else return false; } }; 题目: 判断一个整数是不是回文数,即一个数翻转过来是否跟原来的数仍一样. 需要考虑负数,负数无回文数. 解法: 翻转提供的数字,即123的话,需要翻转为321. 再判…
class Solution { public: int singleNumber(int A[], int n) { int i,j; ; i<n; i++) { ; j<n; j++) { if(A[j]==A[i]) { ]; A[i+] = A[j]; A[j] = temp; i++; break; } } if(j==n) return A[i]; } } }; 上面的是传统方法,时间复杂度是O(n2),空间复杂度是O(1). class Solution { public: in…
/* 查看网上的思路有两种: 1.每次取两边的数,然后进行比较 2.取数的倒置数,进行比较 */ public boolean isPalindrome1(int x) { if (x<0) return false; //以四位数为例,取左边的数用的方法是/1000,取右边的数用的是%10 //注意每次取完两遍要更新数和位数 int len = 1; int temp = x; while (temp>=10) { len*=10; temp/=10; } while (x!=0) { in…
18.4 Write a method to count the number of 2s between 0 and n. 这道题给了我们一个整数n,让我们求[0,n]区间内所有2出现的个数,比如如果n=20,那么满足题意的是2, 12, 20,那么返回3即可.LeetCode上有一道很类似的题Factorial Trailing Zeroes,但是那道题求5的个数还包括了因子中的5,比如10里面也有5,这是两题的不同之处.那么首先这题可以用brute force来解,我们对区间内的每一个数字…
A car travels from a starting position to a destination which is target miles east of the starting position. Along the way, there are gas stations.  Each station[i] represents a gas station that is station[i][0] miles east of the starting position, a…
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…
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. Example: Input: 13 Output: 6 Explanation: Digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 给定一个整数 n,计算所有小于等于 n 的非负整数中数字…
Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. For example:Given n = 13,Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. 解法:参考编程之美 132页 2.4 1的数目,以下代…
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element appears three times except for one. Find that single one. 给出一个整数数组,除了某个元素外所有元素都出现三次.找出仅出现一次的数字. Note: 注意: Your algorithm should have a linear runtime co…
LeetCode 260. Single Number III(只出现一次的数字 III)…
LeetCode 137. Single Number II(只出现一次的数字 II)…
LeetCode 136. Single Number(只出现一次的数字)…
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of integers, every element appears three times except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you…
翻译 给定一个整型数组,除了某个元素外其余元素均出现两次. 找出这个仅仅出现一次的元素. 备注: 你的算法应该是一个线性时间复杂度. 你能够不用额外空间来实现它吗? 原文 Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could yo…
We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. However, when you guess a particula…