转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html

心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想。不要来去都是暴搜,数组遍历。

11:Single Number

Given an array of integers, every element appears twice except for one. Find that single one.

此题:给出一个数组,每个元素出现两次除了一个只出现一次,找到出现一次那个。

思路:这种有很明显映射关系的题:数—出现次数,第一时间就想到用map。

 Map<Integer, Integer> map= new HashMap<Integer, Integer>();
int res = -1;
for(int i:nums){
if(map.get(i)==null){
map.put(i, 1);
}else{
map.put(i, 2);
}
}
Set<Integer> keyset=map.keySet();
Iterator<Integer> iterator=keyset.iterator();
while(iterator.hasNext()){
int curr=iterator.next();
if(map.get(curr)==1){
res=curr;
break;
}
}
return res;

12:Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

此题:求二叉树的深度。

思路:与树有关的,第一时间想到队列、BFS、DFS这些关键字。

法一:BFS,利用队列(Java中LinkedList实现了Queue接口,用法同queue),一层层处理,处理一层,depth++;

 public int maxDepth(TreeNode root) {
int depth=0;
LinkedList<TreeNode> linkedList=new LinkedList<TreeNode>();
linkedList.add(root);
int current_count=1;
//while循环主要用来执行出队入队操作
while(!linkedList.isEmpty()){
TreeNode temp=linkedList.poll();
current_count--;
if(temp.left!=null){
linkedList.add(temp.left);
}
if (temp.right!=null) {
linkedList.add(temp.right);
}
//current_count用来记录每一层的结点数目。然后执行current_count次循环处理完当前层结点,depth++。
if(current_count==0){
depth++;
current_count=linkedList.size();
}
}
return depth;
}

法二:二叉树的深度,最快的做法是DFS。实现DFS就一个思路:递归,返回值。

            //定义边界
if(root==null){
return 0;
}else{
//递归
int m=maxDepth(root.left);
int n=maxDepth(root.right);
//确定递归的返回值
return m>n?m+1:n+1;
}

13:Sum of Two Integers

alculate the sum of two integers a and b, but you are not allowed to use the operator + and -.

此题:求两整数和,但不能用运算符+/-。

思路:位运算的应用。位运算实现四则运算请阅读我的博文:http://www.cnblogs.com/ygj0930/p/6412875.html

 public int getSum(int a, int b) {
int res=a;
int xor=a^b;
int forward=(a&b)<<1;
if(forward!=0){
res=getSum(xor, forward);
}else{
res=xor;
}
return res;
}

14:Find the Difference

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

此题:给出两个字符串,t是s的乱序并且t中多了一个字符。求t中多出的那个字符。

思路:此题容易忽略一个地方——多出的字符可能是s中已经存在的字符。如果多出的字符是s中没有的,比如 s=abc t=abcd ,那么用map很快就能做出来。把s各个字符存入map,然后遍历t各个字符去map.get(ch),get不出来value的就是多出来的。但是如果 s=a,t=aa,那么就求不出来了。所以map不行。转换思路,既然t中包含s的所有字符,那么我们就可以用排除法,从t中逐一剔除s中的元素,直到t留下的最后一个即是多出来的。并且,存放t的容器必须允许重复值的出现而且方便查找删除——很自然地,我们想到list。

public char findTheDifference(String s, String t) {
char res = 0;
char[] ss=s.toCharArray();
char[] ts=t.toCharArray();
List<Character> list=new ArrayList<Character>();
//把t存入list
for(Character ch:ts){
list.add(ch);
}
//从t中逐一删除s的元素
for(Character ch:ss){
list.remove(ch);
}
//留下的最后一个就是多出来的
return list.get(0);
}

15:Add Digits

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

Follow up:
Could you do it without any loop/recursion in O(1) runtime?

此题:求一个整数各位数字相加,直到成为一个数字。

思路:如果按照题目所述去模拟,加一位移除一位的话,无疑是很慢的。而且此题有限制:不能用循环不能用递归,时间复杂度控制在O(1)。由O(1)我们可以看出,题目要求我们一步到位,也就是说,找出一条公式可以直接求。那么找公式,无疑就是找规律了。

观察一个数X,可以发现 X=Xn*10^n+Xn-1*10^(n-1)......+X0*10^0,我们把各位权10^n拆分成99...99+1的形式,就可以得到X=Xn*(99..9+1)+Xn-1*(9..9+1)+....的形式,化开括号就有了  X=(Xn+Xn-1+Xn-2+...+X0)+(Xn*99...9+Xn-1*9.99+...)的形式,很明显可以看出,后面的括号内是9的倍数,可以被9整除。而前面的括号内的和(求各位上数字的和)可以令为X’,继续套用上面拆分方法处理X’,把X’拆分为 各位数值之和+9的某倍数  的形式......直到最终  X=Xs+9*Xo 的形式,Xs是一个个位数,9*Xo为拆分以来众多9的倍数相加得到的9的总倍数。由此可见,对任一个非负整数我们都可以用 一个个位数+9的倍数  形式表示。并且这条公式的推断过程,就是不断求各位上数值的和最终剩下一位的过程,由此可得,求一个数各位数字相加直到成为一个数字,就是求这条公式中的那  个位数。所以,解此题,直接套用公式即可。

public int addDigits(int num) {
//由 num=mod+9*X可以看出,求mod就是用num对9取余即可
int mod=num%9;
//若mod为0,说明num是9的倍数,分两种情况处理:如果num是0,则返回0.如果num是非0的9的倍数,那么num=9*M=9+9*(M-1),所以返回9.
if(mod==0){
if(num==0){
return 0;
}else{
return 9;
}
}
return mod;
}

【leetcode】solution in java——Easy3的更多相关文章

  1. 【leetcode】solution in java——Easy4

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6415011.html 16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到 ...

  2. 【leetcode】solution in java——Easy2

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html 6:Reverse String Write a function that takes ...

  3. 【leetcode】solution in java——Easy1

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6409067.html 1:Hamming distance The Hamming distance betw ...

  4. 【leetcode】solution in java——Easy5

    转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...

  5. 【Leetcode】Reorder List JAVA

    一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...

  6. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

随机推荐

  1. [Android Pro] AndroidStudio IDE界面插件开发(进阶篇之Action机制)

    转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53883500] 从上一篇<AndroidSt ...

  2. [转]linux最新分区方案

    FROM : http://www.cnblogs.com/chenlulouis/archive/2009/08/27/1554983.html 我的服务器是500G.最重要的是/var分区一定要大 ...

  3. 我讨厌Apple Safari浏览器的一些地方。不想用

    1. 书签栏 无法直接新建文件夹 2. 新建书签 无法新建文件夹 3.地址栏 不显示当前书签 是否已收藏! 4. 书签栏 移动书签,体验没有Chrome好. 5.书签栏 没有chrone的 " ...

  4. [转载][HASS.IO] 【HASSOS安装】成功安装HASSOS 1.9(避开了大部分坑版)

    7月20日HA官方放出HASSOS说明时,我开始入坑HASSOS,经历了安装没流量.打开主页:8123没显示.HASS.IO边栏不显示.安装不了HASS.IO插件等问题之后,在8月6日总算避开了大坑进 ...

  5. 【转】asm.js 和 Emscripten 入门教程

      Web 技术突飞猛进,但是有一个领域一直无法突破 ---- 游戏. 游戏的性能要求非常高,一些大型游戏连 PC 跑起来都很吃力,更不要提在浏览器的沙盒模型里跑了!但是,尽管很困难,许多开发者始终没 ...

  6. go语言之进阶篇无缓冲channel

    1.无缓冲channel 示例: package main import ( "fmt" "time" ) func main() { //创建一个无缓存的ch ...

  7. RxJava RxLifecycle 生命周期 内存泄漏 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  8. 领扣-120 三角形最小路径和 Triangle MD

    三角形最小路径和 Triangle 数组 动态规划 问题 给定一个三角形,找出自顶向下的最小路径和.每一步只能移动到下一行中相邻的结点上. 例如,给定三角形: [2], [3,4], [6,5,7], ...

  9. TextEdit 只能输入数字(0-9)的限制

    MaskType="RegEx" MaskUseAsDisplayFormat="True" Mask="[0-9]*" <dxe:T ...

  10. 解决ThinkPHP的Create方法失效而没有提示错误信息的问题

    ThinkPHP中的数据创建Create方法是一个非常有用的功能,它自动根据表单数据创建数据对象(在表字段很多的情况下尤其明显) 但有时候该方法可能并未按照你期望的来工作,比如方法不工作而且还没有提示 ...