Leetcode 4.28 string
1. 38. Count and Say
就是对于前一个数,找出相同元素的个数,把个数和该元素存到新的string里。数量+字符
class Solution {
public String countAndSay(int n) {
if( n == 1)
return "1";
StringBuffer sb = new StringBuffer();
String str = countAndSay(n - 1);
int count = 0;
char c = '0';
for( int i = 0; i < str.length(); i++ ){
c = str.charAt(i);
count = 1;
while((i+1) < str.length() && str.charAt(i) == str.charAt(i+1)){
count++;
i++;
}
sb.append(count + "" + c);
}
return sb.toString();
}
}
2. 58. Length of Last Word
String.lastIndexOf();
;1 class Solution {
public int lengthOfLastWord(String s) {
s = s.trim();
int lastIndex = s.lastIndexOf(' ') + 1;
return s.length() - lastIndex;
}
}
3. 100. Same Tree
采用递归的方法,return中逻辑的运用。
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;
}
}
Leetcode 4.28 string的更多相关文章
- 【LeetCode算法-28/35】Implement strStr()/Search Insert Position
LeetCode第28题 Return the index of the first occurrence of needle in haystack, or -1 if needle is not ...
- [Leetcode][Python]28: Implement strStr()
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 28: Implement strStr()https://oj.leetco ...
- LeetCode 笔记28 Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- [LeetCode] 344 Reverse String && 541 Reverse String II
原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ...
- LeetCode 606. Construct String from Binary Tree (建立一个二叉树的string)
You need to construct a string consists of parenthesis and integers from a binary tree with the preo ...
- [LeetCode] Special Binary String 特殊的二进制字符串
Special binary strings are binary strings with the following two properties: The number of 0's is eq ...
- [LeetCode] Valid Parenthesis String 验证括号字符串
Given a string containing only three types of characters: '(', ')' and '*', write a function to chec ...
- [LeetCode] Design Compressed String Iterator 设计压缩字符串的迭代器
Design and implement a data structure for a compressed string iterator. It should support the follow ...
- [LeetCode] Permutation in String 字符串中的全排列
Given two strings s1 and s2, write a function to return true if s2 contains the permutation of s1. I ...
随机推荐
- C# WebRequest.Create 锚点“#”字符问题
背景 在与后台API接口对接时,如将网页Url作为参数请求数据时,如果是锚点参数,则会丢失. 锚点参数 请求通过WebRequest.Create创建一个WebRequest: var uri = & ...
- FTP方式部署Azure Web App
创建部署凭据 在仪表盘中创建或重置部署凭据,在凭据中设置用户名和密码. 通过FTP方式打开Web根目录 在本地资源管理器中打开FTP地址(例如:ftp://cnws-prod-sha-001.ftp. ...
- springMVC报404,没有显示地址
正常报404会显示你的错误地址信息,而针对本问题 如果你使用的是springMVC框架,这就代表你的请求被拦截了
- Ubuntu 16.04 nvidia-smi报错(重装Nvidia驱动)
之前因为学习TensorFlow,所以在自己的Ubuntu上安装了cuda,cudnn以及Nvidia驱动.但可能是由于自己经常不注重正常关闭自己的Ubuntu,这就导致了一个问题: 某天在查看自己的 ...
- vue-render函数和插槽
Vue render函数,官方文档定义绝大部分时候我们使用template 来创建html 模板,但是纯html 和css都基本上都不具有编程能力,而当我们想使用 javascript的编程能力时,我 ...
- 008. 阻塞&非阻塞、同步&异步
阻塞 非阻塞:关注的对象是调用者: 阻塞:调用者发起调用后,处于等待状态,直到该调用有返回: 非阻塞:调用者发起调用后,不需要等待返回,可以往下执行: 同步 异步: 关注的对象是被调用者: 同步:服 ...
- iOS----------Xcode 无线调试
环境要求: 至少Mac OSX 10.12.6 iOS 11 Xcode 9 1. ”自己的工程“ -> windows -> Device and Simulators ,打开设备和模拟 ...
- BASE64编码的图片在网页中的显示问题的解决
BASE64位转码有两种: 一种是图片转为Base64编码,这种编码是直接可以在页面通过<img src='base64编码'/>的方式显示 Base64 在CSS中的使用 .demoIm ...
- yum 安装 python-pip 失败解决方法
这个包在EPEL源里,要添加EPEL源才可以.然后按博客里说的方法添加,执行以下命令: sudo rpm -ivh epel-release* 第一种方式:由于epel在禁用列表里需要另外加参数yum ...
- Linux学习历程——Centos 7 diff命令
一.命令介绍 diff命令用于比较文本差异. diff以逐行的方式,比较文本文件的异同处.如果指定要比较目录,则diff会比较目录中相同文件名的文件,但不会比较其中子目录. ------------- ...