8:String to Integer

思考:本题的难点体现在对各种情况能否全部覆盖,已经对细节的把握上。

可能出现的各种特殊情况:

1.null or empty input

2.+& -

3.white space

4.max and min of integer

public class atoi {
	 
	 public int luoAtoi(String str){
		if(str==null){
			return 0;
		 }
		str = str.trim();
		if(str.length()==0){
			return 0;
		}
		long result = 0;
		int flag = 1;	//标记正负	
		int i = 0;	    //索引
		if(str.charAt(i) == '+'){
			i++;			
		}
		else if(str.charAt(i) == '-'){
			i++;
			flag = -1;
		}
		for(;i<str.length();i++){
			if((str.charAt(i)<'0')||(str.charAt(i)>'9')){
				break;
			}
			else{
				result = result*10 + (str.charAt(i) - '0');
			}
			if(result>Integer.MAX_VALUE){
				break;
			}
		}
		result = result*flag;
		if(result >= Integer.MAX_VALUE){
			return Integer.MAX_VALUE;
		}
		if(result <= Integer.MIN_VALUE){
			return Integer.MIN_VALUE;
		}
		return (int)result;
	 }
}

细节点:
1.返回的result变量需要是long型,满足String的值可能已经大于Integer的最大值,不然就会取负。
2.char类型相减:因为char类型实际上是ASCII码对应的整数,所以取与'0'的差与10进制整数符合。
3.一旦result大于Integer最大值,需要跳出循环,不然可能导致String的值大于long型的最大值。
4.最后记得把result强转为int型


搜索仓库 GET https://gitee.com/api/v5/search/repositories Response Class "root": "assignee": 0: "assignees_number": integer 代码审查设置,审查人数 "assigner": "id": integer "login": string "name": string "avatar_url": string "url": string "html_url": string "remark": string 企业备注名 "followers_url": string "following_url": string "gists_url": string "starred_url": string "subscriptions_url": string "organizations_url": string "repos_url": string "events_url": string "received_events_url": string "type": string "member_role": string "blobs_url": string "branches_url": string "can_comment": boolean 是否允许用户对仓库进行评论 "collaborators_url": string "comments_url": string "commits_url": string "contributors_url": string "created_at": string "default_branch": string 默认分支 "description": string 仓库描述 "enterprise": "id": integer namespace id "type": string namespace 类型,企业:Enterprise,组织:Group,用户:null "name": string namespace 名称 "path": string namespace 路径 "html_url": string namespace 地址 "fork": boolean 是否是fork仓库 "forks_count": integer 仓库fork数量 "forks_url": string "full_name": string "gvp": boolean 是否是 GVP 仓库 "has_issues": boolean 是否开启issue功能 "has_page": boolean 是否开启了 Pages "has_wiki": boolean 是否开启Wiki功能 "homepage": string 主页 "hooks_url": string "html_url": string "human_name": string "id": integer "internal": boolean 是否内部开源 "issue_comment": boolean 是否允许用户对“关闭”状态的 Issue 进行评论 "issue_comment_url": string "issue_template_source": string Issue 模版来源 project: 使用仓库 Issue Template 作为模版; enterprise: 使用企业工作项作为模版 "issues_url": string "keys_url": string "labels_url": string "language": string 语言 "license": string 开源许可 "members": 0: string "milestones_url": string "name": string 仓库名称 "namespace": "id": integer namespace id "type": string namespace 类型,企业:Enterprise,组织:Group,用户:null "name": string namespace 名称 "path": string namespace 路径 "html_url": string namespace 地址 "notifications_url": string "open_issues_count": integer 开启的issue数量 "outsourced": boolean 仓库类型(内部/外包) "owner": "id": integer "login": string "name": string "avatar_url": string "url": string "html_url": string "remark": string 企业备注名 "followers_url": string "following_url": string "gists_url": string "starred_url": string "subscriptions_url": string "organizations_url": string "repos_url": string "events_url": string "received_events_url": string "type": string "member_role": string "paas": string "parent": "id": integer "full_name": string "human_name": string "url": string "namespace": "id": integer namespace id "type": string namespace 类型,企业:Enterprise,组织:Group,用户:null "name": string namespace 名称 "path": string namespace 路径 "html_url": string namespace 地址 "path": string 仓库路径 "name": string 仓库名称 "owner": "id": integer "login": string "name": string "avatar_url": string "url": string "html_url": string "remark": string 企业备注名 "followers_url": string "following_url": string "gists_url": string "starred_url": string "subscriptions_url": string "organizations_url": string "repos_url": string "events_url": string "received_events_url": string "type": string "member_role": string "assigner": "id": integer "login": string "name": string "avatar_url": string "url": string "html_url": string "remark": string 企业备注名 "followers_url": string "following_url": string "gists_url": string "starred_url": string "subscriptions_url": string "organizations_url": string "repos_url": string "events_url": string "received_events_url": string "type": string "member_role": string "description": string 仓库描述 "private": boolean 是否私有 "public": boolean 是否公开 "internal": boolean 是否内部开源 "fork": boolean 是否是fork仓库 "html_url": string "ssh_url": string "forks_url": string "keys_url": string "collaborators_url": string "hooks_url": string "branches_url": string "tags_url": string "blobs_url": string "stargazers_url": string "contributors_url": string "commits_url": string "comments_url": string "issue_comment_url": string "issues_url": string "pulls_url": string "milestones_url": string "notifications_url": string "labels_url": string "releases_url": string "recommend": boolean 是否是推荐仓库 "gvp": boolean 是否是 GVP 仓库 "homepage": string 主页 "language": string 语言 "forks_count": integer 仓库fork数量 "stargazers_count": integer 仓库star数量 "watchers_count": integer 仓库watch数量 "default_branch": string 默认分支 "open_issues_count": integer 开启的issue数量 "has_issues": boolean 是否开启issue功能 "has_wiki": boolean 是否开启Wiki功能 "issue_comment": boolean 是否允许用户对“关闭”状态的 Issue 进行评论 "can_comment": boolean 是否允许用户对仓库进行评论 "pull_requests_enabled": boolean 是否接受 Pull Request,协作开发 "has_page": boolean 是否开启了 Pages "license": string 开源许可 "outsourced": boolean 仓库类型(内部/外包) "project_creator": string 仓库创建者的 username "members": 1 item "pushed_at": string 最近一次代码推送时间 "created_at": string "updated_at": string "paas": string "stared": boolean 是否 star "watched": boolean 是否 watch "permission": Object 操作权限 "relation": string 当前用户相对于仓库的角色 "assignees_number": integer 代码审查设置,审查人数 "testers_number": integer 代码审查设置,测试人数 "assignee": 1 item "testers": 1 item "status": string 仓库状态 "programs": 1 item "enterprise": 5 properties "project_labels": 1 item "issue_template_source": string Issue 模版来源 project: 使用仓库 Issue Template 作为模版; enterprise: 使用企业工作项作为模版 "path": string 仓库路径 "permission": Object 操作权限 "private": boolean 是否私有 "programs": 1 item "project_creator": string 仓库创建者的 username "project_labels": 1 item "public": boolean 是否公开 "pull_requests_enabled": boolean 是否接受 Pull Request,协作开发 "pulls_url": string "pushed_at": string 最近一次代码推送时间 "recommend": boolean 是否是推荐仓库 "relation": string 当前用户相对于仓库的角色 "releases_url": string "ssh_url": string "stared": boolean 是否 star "stargazers_count": integer 仓库star数量 "stargazers_url": string "status": string 仓库状态 "tags_url": string "testers": 1 item "testers_number": integer 代码审查设置,测试人数 "updated_at": string "url": string "watched": boolean 是否 watch "watchers_count": integer 仓库watch数量 该API能否获取以下内容:必取核心数据 项目基础信息: # 从API返回的每个repo对象中提取 data = { "name": repo["name"], # 项目名称 "html_url": repo["html_url"], # 项目URL "description": repo["description"], # 项目描述 "language": repo["language"], # 主要编程语言 星级数据: # 核心指标 "stargazers_count": repo["stargazers_count"], # 当前总星数 ???
最新发布
06-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值