面试笔试杂项积累-leetcode 231-235
2016-02-12 22:00
549 查看
231.231-Power of Two-Difficulty: Easy
Given an integer, write a function to determine if it is a power of two.方法一
思路
判断一个数是否是2的幂除和取余,循环
public class Solution {
public bool IsPowerOfTwo(int n) {
if (n <= 0)
return false;
while (n > 1)
{
if (n % 2 != 0)
return false;
n = (int)(n * 0.5);
}
return true;
}
}
方法二
思路
位运算1参考:
https://leetcode.com/discuss/78667/java-solution-with-explanation
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n <= 0){ return false;}
int m = -n;
// m is completely different with n except the last position of 1.
// For example if n = 5, i.e., 0000 0101, m would be 1111 1011
m &= n;// m only have the last digit of n to be 1.
return m == n;
}
}
方法三
思路
位运算2参考:
https://leetcode.com/discuss/78626/easy-java-bit-shift-solution
public class Solution {
public boolean isPowerOfTwo(int n) {
if(n < 1) return false;
if(n == 1) return true;
while((n & 1) != 1){
n >>= 1;
}
if(n > 1) return false;
else return true;
}
}
方法四
思路
一种投机的方法超过测试数值平方的数,肯定能除开测试数值所有的二的幂
参考:
https://leetcode.com/discuss/83100/how-about-this-trick
bool isPowerOfTwo(int n) {
if(n<1) return false;
return 4294967296%n==0 ? 1: 0;
}
232.232-Implement Queue using Stacks-Difficulty: Easy
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 top,
peek/pop from top,
size, and
is emptyoperations 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).
思路
使用栈来模拟队列同225题
在push中颠倒过来即可,其他操作不变
public class Queue {
Stack<int> stack = new Stack<int>();
// Push element x to the back of queue.
public void Push(int x)
{
if (Empty())
stack.Push(x);
else
{
Stack<int> temp = new Stack<int>();
while (stack.Count > 0)
temp.Push(stack.Pop());
stack.Push(x);
while (temp.Count > 0)
stack.Push(temp.Pop());
}
}
// Removes the element from front of queue.
public void Pop()
{
stack.Pop();
}
// Get the front element.
public int Peek()
{
return stack.Peek();
}
// Return whether the queue is empty.
public bool Empty()
{
return stack.Count <= 0 ? true : false;
}
}
233.233-Number of Digit One-Difficulty: Medium
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以内所有含1的数开始打算转换成string一个一个比对,理所当然的超时了。
这道题考查的是math
参考:
https://leetcode.com/discuss/64604/my-simple-and-understandable-java-solution
/**
* Calculate occurance on every digit, from
* least important digit to most important digit
* number = 1432
* One's digit: n/10=143 143*1+1
* Ten's digit: n/100=14 14*10+10
* Hun's digit: n/1000=1 1*100+100
* Tho's digit: 1432%1000+1=433
* Sum all occurance on digits together
*/
public static int countDigitOne(int k) {
int count = 0, factor = 1, n = k;
while(n>0){
int m = n/10, r = n%10, amount;
if(r == 0) amount = 0;
else if(r > 1) amount = factor;
else amount = k%factor+1;
count += m*factor + amount;
factor *= 10;
n = n/10;
}
return count;
}
方法二
思路
参考:https://leetcode.com/discuss/64962/java-python-one-pass-solution-easy-to-understand
The idea is to calculate occurrence of 1 on every digit. There are 3 scenarios, for example
if n = xyzdabc
and we are considering the occurrence of one on thousand, it should be:
(1) xyz * 1000 if d == 0 (2) xyz * 1000 + abc + 1 if d == 1 (3) xyz * 1000 + 1000 if d > 1
iterate through all digits and sum them all will give the final answer
public class Solution {
public int CountDigitOne(int n) {
if (n <= 0) return 0;
int q = n, x = 1, ans = 0;
do {
int digit = q % 10;
q /= 10;
ans += q * x;
if (digit == 1) ans += n % x + 1;
if (digit > 1) ans += x;
x *= 10;
} while (q > 0);
return ans;
}
}
234.234-Palindrome Linked List-Difficulty: Easy
Given a singly linked list, determine if it is a palindrome.Follow up:
Could you do it in O(n) time and O(1) space?
方法一
思路
判断链表是否是回文最容易想到的方法是找一个list存起来,再反过来与链表前一半比较,但是不符合in O(n) time and O(1) space
/**
* Definition for singly-linked list.
* public class ListNode {
* public int val;
* public ListNode next;
* public ListNode(int x) { val = x; }
* }
*/
public class Solution {
public bool IsPalindrome(ListNode head) {
IList<int> list = new List<int>();
ListNode temp = head;
while (temp != null)
{
list.Add(temp.val);
temp = temp.next;
}
temp = head;
for (int i = list.Count - 1; i > list.Count / 2-1; i--)
{
if (list[i] != temp.val)
return false;
temp = temp.next;
}
return true;
}
}
方法二
思路
递归参考: https://leetcode.com/discuss/82586/share-my-short-c-recursive-solution-i-believe-its-o-1-space
class Solution {
public:
bool isPalindrome(ListNode* head) {
ListNode *cur = head;//this is the normal cursor
return judge(head, cur);
}
bool judge(ListNode *head, ListNode* &cur) {
if (!head) return true;
if (!judge(head->next, cur)) return false;
if (cur->val != head->val) return false;
else {cur = cur->next; return true;}
}
};
235.235-Lowest Common Ancestor of a Binary Search Tree-Difficulty: Easy
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the
definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow
a node to be a descendant of itself).”
_______6______ / \ ___2__ ___8__ / \ / \ 0 _4 7 9 / \ 3 5
For example, the lowest common ancestor (LCA) of nodes
2and
8is
6. Another example is LCA of nodes
2and
4is
2, since a node can be a descendant of itself according to the LCA definition.
思路
最近相同根节点,在二叉搜索树中两个节点,根据二叉搜索树的性质,遍历中节点大小在二节点之间的就是最近相同根节点
/** * Definition for a binary tree node. * public class TreeNode { * public int val; * public TreeNode left; * public TreeNode right; * public TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode LowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { TreeNode min = p.val <= q.val ? p : q; TreeNode max = p.val > q.val ? p : q; return printNode(root,min.val,max.val); } TreeNode printNode(TreeNode root, int min, int max)//夹在中间的就是共同的根节点 { if (root.val >= min && root.val <= max) return root; else if (root.val > max) return printNode(root.left, min, max); else //if (root.val < min) return printNode(root.right, min, max); } }
相关文章推荐
- 50个Java多线程面试题
- 面试笔试杂项积累-leetcode 226-230
- 大公司最喜欢问的Java集合类面试题
- 近5年133个Java面试问题列表
- 栈向下(低地址)增长(像水里的海绵)——系统程序员成长计划读书笔记
- 程序员跳槽神级攻略
- 程序员必须注意的十大编程禁忌
- 面试笔试杂项积累-leetcode 221-225
- 面试笔试杂项积累-leetcode 216-220
- 面试笔试杂项积累-leetcode 211-215
- 一个程序员的顿悟:理想的程序员只比你多了6个一点点
- 初当技术面试官感悟:给面试者的忠告
- 开发web面试
- 岁月划过生命线(从阿里到微店)
- 第一篇日志,准备做程序员
- 数组指针面试题
- 为什么女程序员会这么少?
- 程序员应该每天写代码
- 程序员狂想曲
- #春节复习系列#操作系统相关知识