LeetCode Happy Number 202

202. Happy Number 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 number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers. ...

August 29, 2016 · 1 min · 209 words · Me

LeetCode Roman to Integer 13

13. Roman to Integer Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. wikipedia-罗马数字 罗马数字共有 7 个,即 I(1)、V(5)、X(10)、L(50)、C(100)、D(500)和 M(1000)。按照下述的规则可以表示任意正整数。需要注意的是罗马数字中没有“0”,与进位制无关。一般认为罗马数字只用来记数,而不作演算。 重复数次:一个罗马数字重复几次,就表示这个数的几倍。 右加左减: 在较大的罗马数字的右边记上较小的罗马数字,表示大数字加小数字。 在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。 左减的数字有限制,仅限于 I、X、C。比如 45 不可以写成 VL,只能是 XLV 但是,左减时不可跨越一个位值。比如,99 不可以用 IC(100-1)表示,而是用 XCIX([100-10]+[10-1])表示。(等同于阿拉伯数字每位数字分别表示。) 左减数字必须为一位,比如 8 写成 VIII,而非 IIX。 右加数字不可连续超过三位,比如 14 写成 XIV,而非 XIIII。(见下方“数码限制”一项。) 加线乘千: 在罗马数字的上方加上一条横线或者加上下标的 Ⅿ,表示将这个数乘以 1000,即是原数的 1000 倍。 同理,如果上方有两条横线,即是原数的 1000000(1000^{{2}})倍。 数码限制: 同一数码最多只能连续出现三次,如 40 不可表示为 XXXX,而要表示为 XL。 例外:由于 IV 是古罗马神话主神朱庇特(即 IVPITER,古罗马字母里没有 J 和 U)的首字,因此有时用 IIII 代替 IV。 自己的解法 做一个数组对照的字典,遵守此条规定:在较大的罗马数字的左边记上较小的罗马数字,表示大数字减小数字。 ...

August 25, 2016 · 1 min · 160 words · Me

LeetCode Majority Element 169

169. Majority Element Given an array of size n, find the majority element. The majority element is the element that appears more than [ n/2 ] times. You may assume that the array is non-empty and the majority element always exist in the array. 题目大意 给定一个数组,找这个数组中的主元素,主元素是元素出现次数大于⌊ n/2 ⌋的元素。 假设给定的数组非空,主元素都存在。 自己的解法 将数组进行排序,根据题意,那么中间的这个数就是 majority element public int majorityElement(int[] nums) { Arrays.sort(nums); return nums[nums.length / 2]; } 别人的解法 【LeetCode-面试算法经典-Java 实现】【169-Majority Element(主元素)】 ...

August 24, 2016 · 1 min · 191 words · Me

LeetCode First Unique Character in a String 387

387. First Unique Character in a String Given a string, find the first non-repeating character in it and return it’s index. If it doesn’t exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. 自己的解法 开始觉的遍历 char[] 然后判断在 String 的首位置和末位置,若一样就返回索引。呃,感觉有点偷鸡 public int firstUniqChar(String s) { for (int i = 0; i < s.length(); i++) { if (s.indexOf(s.charAt(i)) == s.lastIndexOf(s.charAt(i))) { return i; } } return -1; } 查了下别人的,又是 new int[26] ...

August 23, 2016 · 1 min · 133 words · Me

LeetCode Maximum Depth of Binary Tree 104

104. 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. 自己的解法 自己想到应该用递归、为空的返回 0,不为空的返回 1,递归累加;但是有两个点这么判断呢?其实很容易取两数字的 MAX public int maxDepth(TreeNode root) { if (root == null) { return 0; } return 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); } 参考: 104 Maximum Depth of Binary Tree | Data Structure and algorithm analysis ...

August 23, 2016 · 1 min · 73 words · Me

【此生为完成】随笔

我们要用多大的代价,才能认清活着的意义? 于娟,这个风华正茂的女子,拥有留洋经历和博士学位的复旦大学青年教师,在与晚期癌症抗争一年又四个月后,终于撒手人寰。她带走的家人的思念和不舍,给我们留下坚强的力量。 《此生未完成》这本书是上周末聚会时 Zhen 提到的,上周京东买书就带上了。书里前半探讨作者在生死临界时对生活、工作、名利、家人、朋友的一些看法和思考。这对于刚刚毕业的我来说,确实有些是无法真切体会到的,毕竟经历少。但是在书的字里行间中,仍然可以看到一个(不知道用什么词)的女子。 (一) 疾病能让人有怎样的疼痛? 我还是没有哭,不是因为坚强,而是因为痛的想不起来哭,那个时候,只要能用尽全力顶着。如果稍微分神,我就会痛的晕厥。我不想家人看到我的痛苦。 说来惭愧的一个对比,自己在喝多时有过这样的经历:自己抱着垃圾桶使劲顶着一股劲,要是有人来给自己说话,就会分神似的使劲的呕吐。可这蚀骨之痛要比这疼痛多少倍呢? (二) 重要的是当他得知我生病消息后的第一反应,眼神表情乃至电话语气网络留言里端倪尽出,你会觉得世间很多人情世故是那么的让你淡然一笑。 在遇到大事的时候方显出真正的朋友和所谓的朋友,扬尘散土,洗沙留金。能遇到“大事”的机会不多,遇到的“大事”还能全身而退的机会就更少了。所以觉的自己应该及时去明辨所有的朋友,将真心给最真的,对于其他的就随他去吧。我不可能讨所有人喜欢,也用不着。 (三) 三十岁之前的努力更多是因为自己有着太多的欲望和执著,从没有“只要活着就好”的简单。名利权情,没有一样是不辛苦的,却没有一样可以带走。 这是一个作者很重要的一个观点。在现在的我看来,“只要活着就好”的活着,是一种精神上富有的活着,是充满智慧的活着。 就说现在的自己,什么都不曾拥有,或许不用拥有过的名、利,但是其他方面什么都没有,自己一切都是空的,看到好就会有强烈的“欲望”,如果再强烈一点就会有“执著”。在文字中我感受到了作者在精神方面的强大与富有,或许在这之后才能感受到在名、利之外更重要的东西。其实,自己真的很难想通,到底拥有什么自己就可以“苟”了,自己到底想要什么。了解自己真正想要什么真是一幸事。 作者提到的有时间,多多陪陪家人父母、有机会就尽孝,自己是很赞同的。 (四) 外公反映土豆每次经过,都趴在玻璃上看玻璃窗里的圣诞树、圣诞彩球和姜饼屋。土豆眼神里有点想,但却乖乖看着,并不讨。外公回想说:“那个眼神我想起了卖火柴的小女孩,有时候孩子听话,你却反而会心酸。” 这文字碰触心灵最柔软的地方。 (五) 你的妈妈不是懦夫,所以你的人生里,遇到珍贵的人与关键的事,都要积极争取,可以失败,但是不能放弃。我想做一个让儿子骄傲的妈妈,只此一点,无论任何地步,我都不会选择自己走,哪怕,万劫不复的痛。 (六) 更多人不会明白,我们两个的谈笑深处埋藏着多少不能言表的无声叹息。上一次见面,我和梅两个事多么风华正茂,像展翅云霄的鹰隼,挥着翅膀相约下次的冲天。这次的相逢,是灰头土脸被命运按在尘土里依然微笑的土鸡之间的问候。 (七) “为啥是我得癌症” 的非学术报告,是书的精华文章。从饮食习惯、睡眠习惯、突击作业、环境问题,分析了对身体不利的因素。突击作业其实就是工作习惯。 在饮食上常吃不寻常的动物、暴饮暴食、嗜荤如命。 我们要相信聪明的祖先,几千年的智慧沉淀,才最终锁定了我们现在的食材,并在远古对他们进行豢养。如果孔雀比鸡好吃,那么现在的就是孔雀,孔雀就是鸡。 在睡眠习惯上: 二十三时至次日三时,是肝脏活动能力最强的时段,也是肝脏最佳的排毒时期,如果肝脏功能得不到休息,会引起肝脏血流相对不足,已受损的肝细胞难以修复并加剧恶化。所以“长期熬夜等于慢性自杀”的说法并不夸张。因此医生建议人们从二十三时左右开始上床睡觉,次日一至三时进入深睡眠状态,好好地养足肝血。 突击作业: 一辆平时就跌跌撞撞一直不维修的破车,一踩油门就彻夜地疯跑疯开半个月。一年搞个五六次,就是钢筋铁打的汽车,开个二十几年也到报废了。 环境问题,空气污染、水污染和食品安全危机还有那可怕的甲醛家具。 (八) 不去想控制大局小局,不想去多管闲事淡事,我不在有对手,不再有敌人,我也不在关心谁比谁强,课题也好,任务也罢,暂且放着。 也许只有正在想明白的人,才懂的世间的一切,隔岸看花、风淡云轻。

August 22, 2016 · 1 min · 37 words · Me

LeetCode Intersection of Two Arrays 349

349. Intersection of Two Arrays Given two arrays, write a function to compute their intersection. Example: Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. Note: Each element in the result must be unique. The result can be in any order. 自己的解法 使用 set 进行去重 public static int[] intersection2(int[] nums1, int[] nums2) { Set<Integer> nums1Set = new HashSet<>(); Set<Integer> set = new HashSet<>(); for (int integer : nums1) { nums1Set.add(integer); } for (int integer : nums2) { if (nums1Set.contains(integer)) { set.add(integer); } } int[] result = new int[set.size()]; int i = 0; for (int integer : set) { result[i] = integer; i++; } return result; } 别人的思路 LeetCode – Intersection of Two Arrays (Java) ...

August 22, 2016 · 1 min · 172 words · Me

LeetCode Same Tree 100

100. Same Tree 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. 关于树结构自己没怎么看过,查了查遍历通常是:递归、stack 自己的解法 /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isSameTree(TreeNode p, TreeNode q) { if (p == null && q == null) { return true; } if (p == null || q == null) { return false; } if (p.val == q.val) { return isSameTree(p.left, q.left) && isSameTree(p.right, q.right); } return false; } }

August 22, 2016 · 1 min · 122 words · Me

LeetCode Excel Sheet Column Title 168

168. Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 自己的思路 十进制转“二十六进制” public String convertToTitle(int n) { String str = ""; do { int chrInt = n % 26; char chr; if (chrInt == 0) { chr = 'Z'; n -= 26; } else { chr = (char) (chrInt - 1 + 'A'); } str = chr + str; n = n / 26; } while (n != 0); return str; } 别人的思路 public String convertToTitle(int n) { if(n <= 0){ throw new IllegalArgumentException("Input is not valid!"); } StringBuilder sb = new StringBuilder(); while(n > 0){ n--; char ch = (char) (n % 26 + 'A'); n /= 26; sb.append(ch); } sb.reverse(); return sb.toString(); } 思路是一样的,但是代码更简洁,使用了StringBuilder 和 reverse(),提前减 1 也避免了特殊处理’Z' ...

August 22, 2016 · 1 min · 170 words · Me

LeetCode Excel Sheet Column Number 171

171. Excel Sheet Column Number Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 自己的思路 很像是“二十六进制”转十进制 public int titleToNumber(String s) { int col = 0; char[] chars = s.toCharArray(); int i = 0; while (i < chars.length) { int charInt = chars[chars.length - 1 - i] - 64; col += Math.pow(26, i) * charInt; i++; } return col; } 在算法问题中常常用到: ...

August 22, 2016 · 1 min · 110 words · Me