主页

342.Power of Four(Easy)

Given an integer (signed 32 bits), write a function to check whether it is a power of 4. 给定一个32位的整数,判断是否为4次幂   一开始看到这题我就想到打表,但是,果然还是位运算最厉害 Example: Given num = 16, return true. Given num = 5, return false. Follow up Could you solve it without loops/recursion? My Solution (Java) Version 1 Time: 2ms:   没有什么好说的,在32位数的世界里面,4次幂并没有几个,只有1...

阅读更多

328.Odd Even Linked List(Medium)

Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes. You should try to do it in place. The program should run in O(1) space complexity and O(nodes) time complexity.   题目不难理解我就不翻译了,就是一个单链表,把所有奇数的结点放在前面,然后接着所有的偶数结点,然后要求在时间复杂度O(n...

阅读更多

6.ZigZag Conversion(Easy)

The string ”PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) And then read line by line: ”PAHNAPLSIIGYIR” Write the code that will take a string and make this conversion given a number of rows: string convert(string text, ...

阅读更多

415.Add Strings(Easy)

Given two non-negative numbers num1  and num2  represented as string, return the sum of num1  and num2 不解释了,就是大数相加 Note: 1.The length of both num1 and num2 is < 5100. 2.Both num1 and num2 contains only digits 0-9. 3.Both num1 and num2 does not contain any leading zero. 4.You must not use any built-in BigInteger library or convert the inp...

阅读更多

67.Add Binary(Easy)

Given two binary strings, return their sum (also a binary string). 给两个二进制的字符串,返回它们的和 Example a = ”11” b = ”1” Return ”100” My Solution (Java) Version 1 Time: 4ms:   这个其实没有太复杂的原理,基本上只要是字符串表示数都是大数运算的套路,先把字符串逆序,然后高位补0,然后用循环逐位相加,对进位的情况进行处理,然后就得到结果了,复杂的是进位的几种情况的处理 public class Solution { public String addBinary(String a, String b) { ...

阅读更多

345.Reverse Vowels of a String(Easy)

Write a function that takes a string as input and reverse only the vowels of a string. Note:The vowels does not include the letter “y”. 写一个函数把字符串中的元音字母的顺序调转,‘Y’不是一个元音字母 Example 1: Given s = “hello”, return “holle”. Example 2: Given s = “leetcode”, return “leotcede”. My Solution (Java) Version 1 Time: 5ms:   先一次遍历把元音字母的下标记下来,然后把通过下标直...

阅读更多

70.Climbing Stairs(Easy)

You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 你在爬楼梯,楼梯总共有n个台阶,每次你可爬一级或者两级台阶,问爬完整个楼梯有多少种爬的方式?   这是以前小学的奥数题,找规律,爬楼梯,显然我们都知道对于一次只走一级或者两级的情况是符合斐波那契数列的,所以题目的答案就变成了求第n个斐波那契数是多少 My Solution (Java) Version 1 Time: 0ms:   应该是因为是Easy...

阅读更多

141.Linked List Cycle(Easy)

Given a linked list, determine if it has a cycle in it. 给定一个线性链表,判断它是否有环   题目很久之前做过,那时候一个方法都不懂,成环的意思,就是链表的尾结点连接到了链表中的某个节点,形成了一个环形的结构 My Solution (Java) Version 1 Time: Time Limit Exceeded:   这是以前什么都不懂时候的做法,当然这个效果肯定是有的,不过效率一般般,之前是C语言写的,现在用Java复现一下,留作纪念。这里用的方式是,在遍历链表的时候把遍历过的结点保存下来,然后在遍历下一个结点的时候,确定结点中的next指向是否在遍历过的结点中。 /** * Definition for sin...

阅读更多

102.Binary Tree Level Order Traversal(Easy)

Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to right, level by level). 给定一个二叉树,返回他的水平层序遍历(从左到右,一层再一层)   一个对队列的巧妙应用 For example Given binary tree [3,9,20,null,null,15,7].   3  /   \ 9   20      /   \    15   7 return its level order traversal as: [ [3], [9,20], [15,7] ] My Solution (Java) Ver...

阅读更多

222.Count Complete Tree Nodes(Medium)

Given a complete binary tree, count the number of nodes. 给定一个完全二叉树,计算其节点数 Definition of a complete binary tree from Wikipedia:In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h  nodes inclusive at the last level h. ...

阅读更多

7.Reverse Integer(Easy)

Reverse digits of an integer. 反转一个整数的数字 Example1: x = 123, return 321 Example2: x = -123, return -321   看似没有坑,实则不然,因为在这个题目里面,隐含的条件还有要做溢出处理,即输入一个超过Integer最大值的整数 My Solution (Java) Version 1 Time: 43ms:   首先想到的做法就是把整数当做个字符串进行反转,刚好前几天做过了反转字符串的操作,这里直接应用,然后就通过了,关键是用long类型来装传入的int值,就可以有效避免在运算中溢出的问题 public class Solution { public int reverse(in...

阅读更多

100.Same Tree(Easy)

Given two binary trees, write a function to check if they are equal or not. Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 给定两个二叉树,写一个函数检查它们是否相同。 如果两个二叉树相同结点的值相同,则认为这两个二叉树相同。 My Solution (Java) Version 1 Time: 0ms:   并没有什么大坑,只是简单地遍历两个二叉树并判断结点的值是否相等就ok了,测试样例似乎也没有走极端,用递归也没有超时,确实是Easy题...

阅读更多

26.Remove Duplicates from Sorted Array(Easy)

Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length. Do not allocate extra space for another array, you must do this in place with constant memory. 给定一个已排序的数组,去掉其中重复的元素,使得每个元素只出现一次,并返回新数组的长度。 不要为数组分配额外的空间,你必须在常量内存中进行操作   意思不难理解,就是一个已经排好序的数组然后把不同的数抽出来组成一个新的数组,这里说的不要额外分配空间的意思是不要n...

阅读更多

241.Different Ways to Add Parentheses(Medium)

Given a string of numbers and operators, return all possible results from computing all the different possible ways to group numbers and operators. The valid operators are+ ,- and*. 给定一个数字和运算符的字符串,返回所有不同分组情况下运算的结果。合法的运算符有+,-和*   题目的意思就是给一段只有+,-,*的三则运算,然后加括号,加括号的时候符号不变,把加了括号后计算的不同结果列出来 Example 1 Input:”2-1-1”. ((2-1)-1) = 0 (2-(1-1)) = 2 Ou...

阅读更多

233.Number of Digit One(暂留)

Given an integer n, count the total number of digit 1 appearing in all non-negative integers less than or equal to n. 给出一个整数n,计算在所有小于或等于n的非负整数中,数字1出现的个数。   这里最麻烦的自然就是当数字大的时候,用简单的循环暴力计数肯定会超时,这个时候考验的就是找规律的能力了 For example: Given n = 13, Return 6, because digit 1 occurred in the following numbers: 1, 10, 11, 12, 13. My Solution (Java) Versio...

阅读更多