public class Solution { public String countAndSay(int n) { // Start typing your Java solution below // DO NOT write main() function ArrayList<Character> current = new ArrayList<Character>(); current.add('1'); for (int i = 1; i != n; ++i) current = next(current); StringBuilder sb = new StringBuilder(); for (int i = 0; i < current.size(); ++i) sb.append(current.get(i)); return sb.toString(); } ArrayList<Character> next(ArrayList<Character> current){ ArrayList<Character> tempList = new ArrayList<Character>(); Character temp = current.get(0); int count = 1; for (int i = 1; i < current.size()-1; ++i){ if (!current.get(i).equals(temp)){ add(tempList, count); tempList.add(temp); temp = current.get(i); count = 1; } else count++; } if (current.size() > 1){ if (current.get(current.size()-1).equals(temp)) { add(tempList, count+1); tempList.add(temp); } else { add(tempList, count); tempList.add(temp); add(tempList, new Character('1')); tempList.add(current.get(current.size()-1)); } } else { add(tempList, count); tempList.add(temp); } return tempList; } ArrayList<Character> add(ArrayList<Character>temp, int i){ if (i < 10){ temp.add((char)(i+48)); return temp; } else { temp = add(temp, i / 10); temp.add((char)(i % 10 + 48)); return temp; } } }
原来自己写了一个函数add,而且int强制转换char是要加48的(一开始不知道,囧),然后还runtime error了。后来发现Character类有toChars()方法,改写了一下试试,结果发现完全不对。。。好像是这个方法必须是utf-16的编码= = 还是继续改原来的方法好了
public class Solution { public String countAndSay(int n) { // Start typing your Java solution below // DO NOT write main() function ArrayList<Character> current = new ArrayList<Character>(); current.add('1'); for (int i = 1; i != n; ++i) current = next(current); StringBuilder sb = new StringBuilder(); for (int i = 0; i < current.size(); ++i) sb.append(current.get(i)); return sb.toString(); } ArrayList<Character> next(ArrayList<Character> current){ ArrayList<Character> tempList = new ArrayList<Character>(); Character temp = current.get(0); int count = 1; for (int i = 1; i < current.size()-1; ++i){ if (!current.get(i).equals(temp)){ add(tempList, count); tempList.add(temp); temp = current.get(i); count = 1; } else count++; } if (current.size() > 1){ if (current.get(current.size()-1).equals(temp)) { add(tempList, count+1); tempList.add(temp); } else { add(tempList, count); tempList.add(temp); add(tempList, 1); tempList.add(current.get(current.size()-1)); } } else { add(tempList, count); tempList.add(temp); } return tempList; } ArrayList<Character> add(ArrayList<Character>temp, int i){ if (i < 10){ temp.add((char)('0' + i)); return temp; } else { temp = add(temp, i / 10); temp.add((char)('0' + i % 10)); return temp; } } }
吃饭去!!!
发表评论
-
Insert Interval
2012-11-11 01:33 542各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 ... -
Implement strStr()
2012-11-07 15:44 581唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。 ... -
Flatten Binary Tree to Linked List && Generate Parentheses && Gray Code
2012-11-07 00:08 1093Flatten太简单了 递归 一遍过 oh yeah = = ... -
First Missing Positive
2012-11-06 22:50 589唉 想了很久都没想出来 后来还是看了网上的答案 >_&l ... -
Edit Distance
2012-11-06 00:27 670动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一 ... -
Divide Two Integers
2012-11-05 00:12 712自己实现除法 太太太恶心了。。。。 就是用位移代替了乘法,然后 ... -
Distinct Subsequences
2012-11-04 21:44 657动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一 ... -
Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Sear
2012-11-04 17:36 826/** * Definition for binary ... -
Container With Most Water
2012-11-04 00:25 697本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超 ... -
Construct Binary Tree from Inorder and Postorder Traversal
2012-11-03 23:40 746不知道为什么错了。。。eclipse上明明是正确的啊 leet ... -
Combinations
2012-11-03 22:19 604全排列 按理说很简单,可是用递归写,边界条件就还是难想清楚,s ... -
Combination Sum I && II
2012-11-03 21:41 751还是递归 但是边界条件以及边界上的处理不容易搞清楚(一开始我就 ... -
climbing stairs
2012-11-03 17:18 609一开始觉得是简单的组合数学题,但是写完之后发现,首先组合数不是 ... -
Binary Tree Inorder Traversal
2012-11-02 23:51 615I简单 直接递归就好 addAll函数很好用 /** ... -
Best Time to Buy and Sell Stock I & II
2012-11-02 22:05 1044啊 第一次直接过small和big测试 好爽!虽然主要是以前知 ... -
Balanced Binary Tree
2012-11-01 23:38 609/** * Definition for binary ... -
Anagrams
2012-10-31 00:33 586这题实在是没懂它的意思。。。囧啊 import java. ... -
Add Two Numbers
2012-10-30 23:03 614这题不难 直接上递归就行 /** * Definiti ... -
Add Binary
2012-10-29 00:07 599public String addBinary(Strin ... -
4Sum
2012-10-27 22:49 734本来以为只要在3Sum外面再包一层循环就好了,可是。。。在Ju ...
相关推荐
Leetcode原题Count and Say count-and-say序列是整数序列,前五个术语如下: 1. 1 2. 11 3. 21 1211 5. 111221 1被读作“1”或11。 11被读作“两个1”或21。 21被读作“一个2,然后一个1”或1211。 给定整数n,...
Count and Say Anagrams Valid Anagram Simplify Path Length of Last Word Isomorphic Strings Word Pattern 栈和队列 栈 Min Stack Valid Parentheses Longest Valid Parentheses Largest Rectangle in Histogram ...
3.12 Count and Say 3.13 变位词 3.14 简化系统路径 3.15 最后一个单词的长度 3.16 反转字符串中的单词 3.16.1 字符串前后和中间可能存在多个空格 3.16.2 不存在前后和中间的多余空格 3.17 一个编辑距离 4. 栈 4.1 ...
I'll keep updating for full summary and better solutions. Stay tuned for updates. (Notes: "馃摉" means you need to subscribe to [LeetCode premium membership](https://leetcode.com/subscribe/) for the ...
leetcode中国 我自己的leetcode刷题记录 ###[20150920] ...count and say , easy , 模拟 Anagrams 字符串处理,map Simplify Path,字符串处理,stack Length of Last Word,字符串处理.细节题 Rever
leetcode 答案LeetCode LeetCode in Swift 这个Repo 用来存下我在LeetCode ...Count and Say Easy #53 Maximum Subarray Easy #66 Plus One Easy #70 Climbing Stairs Easy #83 Remove Duplicates from Sorted L
Practice-Leetcode 这是一个Chinese School Girl:China:用来练习leetcode的文档.每道下面的题都有详细的解题思路,和知识点分析,尽请参考。 找实习的小伙伴也可以参考我的Tech-blog...038.Count and Say 递归 040.C
leetcode卡 LeetCode LeetCode题解 目录 字符串问题 ID Title C++ 难度 备注 0008 String to Integer(atoi) ...Count and Say :star: 0043 Multiply Strings :star: :star: 大数相乘 0044 Wild Card Matchi
leetcode给单链表加一js实现 LeetCode By JavaScript LeetCode Solutions (All By JavaScript!) 的个人Solutions汇总,全部使用JavaScript完成:grinning_squinting_face: 目的:了解、掌握数据结构和算法,当然...Say:
………… 根据详细的说明可以参见:http://en.wikipedia.org/wiki/Look-and-say_sequence 下面用PHP实现这个序列,如下: 复制代码 代码如下: function look($str) { $len = strlen($str); $count=0; $result=”
One may count upto one or two numbers per turn, and the numbers must be in counting order. One must start with the number after the last one that the other one said. For example, the first person ...
Lesson 1: Concepts – Locks and Lock Manager 3 Lesson 2: Concepts – Batch and Transaction 31 Lesson 3: Concepts – Locks and Applications 51 Lesson 4: Information Collection and Analysis 63 Lesson 5:...
And they prove it: Imagine a huge scene, consisting of, say, 50 million triangles. Toss it at a recent GeForce with enough memory to store all those triangles, and write down the frame rate. It will ...
count was for 9i, and that was more than 20,000 pages—and I doubt if the number has dropped in 10g and 11g. With three (fairly slim) manuals for 5.0.22, it didn’t take me very long to learn about ...
Troubleshooting server performance-based support calls requires product knowledge, good communication skills, and a proven troubleshooting methodology. In this module we will discuss Microsoft® SQL ...
PAQ6v2 - File archiver and compressor. (C) 2004, Matt Mahoney, mmahoney@cs.fit.edu This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public ...
I can't say much more about it except for the fact that its fast and compact<END><br>51,chexer.zip As Jonathan wrote, "This is a tool I wrote to use in DevStudio to facilitate memory address ...
https://leetcode.com/problems/count-and-say/ 3, java/dungenegame https://leetcode.com/problems/dungeon-game/ 4, java/findminrotaed 5, java/houserobber https://leetcode.com/problems/house-robber/
Sydney Brenner, senior distinguished fellow of the Crick-Jacobs Center in California, won the 2002 Nobel Prize for Medicine and says that if there is a global disaster some humans will survive-and ...
Another way to say this is that the data itself is part of the clustered index. A clustered index keeps the data in a table ordered around the key. The data pages in the table are kept in a doubly ...