转载请注明原文地址: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. SpringBoot扫描包提示找不到mapper的问题

    SpringBoot扫描包问题 报错信息:Consider defining a bean of type in your configuration 方法一: 使用注解 @ComponentScan ...

  2. 真爱如血第一季/全集True Blood迅雷下载

    第一季 True Blood Season 1 (2008)看点:该剧根据小说<南方吸血鬼>(Southern Vampire)改编,故事围绕路易斯安那州的吸血鬼和人类展开,当日本将人造血 ...

  3. [Web 前端] Jquery 复制元素,并修改属性, 追加到另一个元素后面

    cp from  : https://blog.csdn.net/cooledi/article/details/52813668 jquery 复制元素,并修改属性 $('#ID').clone() ...

  4. Matlab中使用LaTeX

    Matlab作为数据计算和处理的数学语言(软件),而LaTex作为出版界的重要排版语言(软件),尤其是对数学公式的排版功能特别强.在Matlab中有两种方法使用LaTeX:1)对Matlab生成的图形 ...

  5. Django创建自定义错误页面400/403/404/500等

    直接参考: https://zhuanlan.zhihu.com/p/38006919 DEBUG =True的话,为开发环境,显示不了404页面.

  6. WebFormViewEngine及用户控件寻址bug

    在做我的网站的时候遇到了主题切换的问题,特总结与大家共享. 熟悉asp.net mvc的朋友都知道,mvc中,默认情况下视图都在views文件夹下放着.要想改变文件必须重写WebFormViewEng ...

  7. [leetcode]Best Time to Buy and Sell Stock @ Python

    原题地址:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock/ 题意: Say you have an array for ...

  8. 为 hexo NexT 添加 Gitment 评论插件

    Gitment 是作者imsun实现的一款基于 GitHub Issues 的评论系统. 支持在前端直接引入, 不需要任何后端代码. 可以在页面进行登录, 查看, 评论, 点赞等操作. 同时有完整的 ...

  9. 3d打印机的软件系统组成部分

    主要由计算机.应用软件.底层控制软件和接口驱动单元组成1)计算机一般采用上位机和下位机两级控制.其中上位主控机一般采用配置高.运行速度快的PC机:下位机采用嵌入式系统DSP,驱动执行机构.上位机和下位 ...

  10. 设置Linux中的Mysql不区分表名大小写

    1. MySQL数据库的表名在Linux系统下是严格区分大小写的,在Windows系统下开发的程序移植到Linux系统下,如果程序中SQL语句没有严格按照大小写访问数据库表,就可能会出现找不到表的错误 ...