1.写一个方法method1(String s),返回字符串参数中最后一个数字。
例:“His telephone number is 123456789” return 123456789;
"There are 1 bees,2 butterflies,3 dragonflies" return 3;2.用2分法查询并返回数组中元素的索引 method2(int n)。
{-10,0,10,17,17,23,65,123} 当n = 17 return 4;当n=24 return 5;前段时间去面试时候做的,1个小时,一共3道。还有一道2进制的题数字太长没记下来。当时俺没做完,特来此寻觅高人。看看你有没有进著名外企的实力!^_^
例:“His telephone number is 123456789” return 123456789;
"There are 1 bees,2 butterflies,3 dragonflies" return 3;2.用2分法查询并返回数组中元素的索引 method2(int n)。
{-10,0,10,17,17,23,65,123} 当n = 17 return 4;当n=24 return 5;前段时间去面试时候做的,1个小时,一共3道。还有一道2进制的题数字太长没记下来。当时俺没做完,特来此寻觅高人。看看你有没有进著名外企的实力!^_^
解决方案 »
- 多个虚拟机上的程序怎么并发控制
- 关于awt监听器的一简单问题
- 捕捉ctrl+鼠标滚轮事件
- 致JAVA初学者+如何下手学JAVA [精华]
- Exception in thread "main" java.lang.NoClassDefFoundError: javax/activation/DataSource
- 关于JDK1.4和JDK1.5版本问题
- 求数字签名源代码示例
- Java与xml
- 将DataOutputStream.writeBytes(byte[])写完后,如何能够显示出DataOutputStream中的内容?如何知道写入bytes的大小?
- 我照JBUILDER上的tutorials做的SESSION BEAN怎么会有错误?
- 急!!!!!!!!tomcat的问题,好奇怪!!!!!!
- 郁闷我三天的问题
==============
他不是要一个字符,而是要一堆字符。There are 12157 112 bees,2 22butterfli33es,3 dragonflies 4
提取出
12157
112
2
22
33
3
4
写了半天,没写出来,望高人指点。
例:“His telephone number is 123456789” return 123456789;
"There are 1 bees,2 butterflies,3 dragonflies" return 3;
一段字符串要返回最后面的数字 一个的话返回一个一组返回一组。
这个方法虽然笨,但可以避免数字前是非空格的情况.
/////////////////////////////////////////////////////
这个方法不错,将string用split分割成字符数组,然后找到第一个全为数字的元素就行了
,不知道“hello my3”要不要提取3???题的意义还不太明确
import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Random;import com.bruceeckel.util.IntGenerator;public class Summary { public static void main(String[] args) {
int[] L = new int[]{-10,0,10,17,17,23,65,123} ;
System.out.println(new Summary().Binary_Search(L, 0, 7, 23));
} public int Binary_Search(int[] L, int a, int b, int x) {
int m = 0;
if (a > b) {
return -1;
} else {
m = (a + b) / 2;
if (x == L[m]) {
return m;
} else if (x > L[m]) {
return (Binary_Search(L, m + 1, b, x));
} else
return (Binary_Search(L, a, m - 1, x));
}
}
}
===================
3也应该要提取出来吧
import java.util.regex.*;
import java.io.*;
class A
{
public static void main(String[] args) throws IOException
{
System.out.println("输入一行字符串,返回最后一个数字!");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s=br.readLine();
method1(s);
} static void method1(String s)
{
Pattern p=Pattern.compile("\\d+");
StringBuffer sb=new StringBuffer(s);
String s1=sb.reverse().toString();
Matcher m=p.matcher(s1);
m.find();
String s2=m.group();
StringBuffer sb1=new StringBuffer(s2);
System.out.println(sb1.reverse());
}}
小弟技术差,查了半天API才写出来...
对吗?
===================
3也应该要提取出来吧
=========================================
其实,这样的话,只需要改一下对每个数组(单词)元素的判断条件,然后再截取出来放到string中,对吧
如楼上所说,“hello my3”就要返回3,返回的数中,数字与数字之间必须是相连的,中间是空格或其他的字符都算成两个数,返回的也只能是后边的那个数。
System.out.println(str[str.length-1]);
String str = "There are 12157 112 bees,2 22butterfli33es,3 dragonflies 4";
String tag = "\\d+"; // ?个方法根本不?,没有正?的反映出正?表?式的性?\
List temp = new ArrayList();
Pattern p = Pattern.compile(tag);
// ?建Email字段匹配器,待??字符串Data?其参数
Matcher emailMatcher = p.matcher(str);
while (emailMatcher.find()) {
temp.add(emailMatcher.group(0));
}
System.out.println(temp.get(temp.size() - 1));
}
然后保存这个位置为end,再往前搜索直到不是数字,保存这个位置为begin,最后取出从begin到end子串,程序如下,稍加改动即可判断出小数和负数。
public class TestString
{
public static void main(String[] args)
{
String s1 = new String("His telephone number is 123456789");
String s2 = new String("There are 1 bees,2 butterflies,3 dragonflies");
System.out.println(method1(s1));
System.out.println(method1(s2));
}
public static String method1(String s)
{
char end;
for(int i=s.length()-1; i>=0;i--)
{
end = s.charAt(i);
if(end<48||end>57) continue;
else
{
char begin;
int j=i;
do
{
i--;
begin=s.charAt(i);
}while(begin>=48&&begin<=57);
return s.substring(i+1,j+1);
}
}
return null;
}
}
String output="";
boolean startflag=false;
char c;
for(int i=str.length()-1;i>=0;i--){
c=str.charAt(i);
if(c>'0'&&c<'9'){
startflag=true;
output=c+output;
}else{
if(startflag){
System.out.println(output);
return;
}
}
}
if(output.equals("")){
System.out.println("字符串中没有数字");
}else{
System.out.println(output);
}
}
import java.util.regex.*;
import java.util.*;
public class Cbase {
public static void main(String[] arg){
String s1="His telephone 3456 number is 123456789";
String s2="There are 1 bees,2 butterflies,3 dragonflies";
System.out.println(method1(s1));
System.out.println(method1(s2));
int a[]={-10,0,10,17,17,23,65,123};
System.out.println(Arrays.binarySearch(a, 123)+1);//二分,用库
System.out.println(method2(a,-10)+1);//二分,自己构造函数
}
static String method1(String s) {
String ss[]=s.split("\\D+");
return ss[ss.length-1];
}
static int method2(int d[],int k) {
int down=0,up=d.length,index=-100;
boolean find=false;
while(up>=down && !find) {
index=(up-down)/2+down;
if(d[index]>k) up=index-1;
else if(d[index]<k) down=index+1;
else find=true;
}
if(!find) index=-1;
return index;
}
}
import java.io.*;public class mainclass {
public mainclass() {
}
public static void main(String[] args) {
String s="His telephone 3456 number is 123456789";
char a;
int isbegin=0;
int numbegin=0;
int numend=0;
for(int i=0;i<s.length();i++){
a=s.charAt(i);
if((a>47&&a<58)&&isbegin==0){
numbegin = i;
isbegin = 1;
}
if(isbegin==1&&(a<48||a>57)){
numend=i;
isbegin=0;
}
if(isbegin==1&&(i==(s.length()-1))){
numend=i+1;
isbegin=0;
}
}
System.out.println(" the begin is:"+numbegin);
System.out.println(" the end is:"+numend);
System.out.println(" the answer is:"+s.substring(numbegin,numend));
}
}
高手可以研究一下小数和负数的情况。
weijieut() 的方法返回了所有的数字
jayflee() 的方法看起来也象返回所有数字,而且0,9两个数字都没有了
JianZhiZG(健之) 的二分法查找,如果参数是24的话,返回的是-1另外,我记错了,是用机器答的题。
* @author Red_angelX
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(method1("There are 1 bees,2 butterflies,3 dragonflies"));
}
public static String method1(String s)
{
int start = 0;
int end = 0;
int len = s.length();
for(int i=0;i<len;i++)
{
if(s.charAt(len-1-i) >= '0' && s.charAt(len-1-i) <= '9')
{
end = len - 1 -i;
int t = 0;
while(len-1-i-t>=0 && s.charAt(len-1-i-t)>='0' && s.charAt(len-1-i-t)<='9')
{
start = len -1-i-t;
t++;
}
String temp = s.substring(start,end+1);
System.out.println(temp);
return temp;
}
}
return "No Found!";
}}
public class BinarySearch { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int[] s = {-10,0,10,17,17,23,65,123};
System.out.println(bSearch(s,23,0,s.length-1));
}
public static int bSearch(int[] s,int value,int start,int end)
{
if(value < s[0] || value > s[s.length-1])
return -1;
if(start == end && s[start] != value)
return -1;
int mid = (end + start) / 2;
if(s[mid] < value)
{
return bSearch(s,value,mid+1,end);
}
else if(s[mid] > value)
{
return bSearch(s,value,start,mid-1);
}
else
return mid; }}但是当24的时候取不出5 不知道是不是lz笔误还是公司故意的?
if(value < s[0] || value > s[s.length-1])
return -1;if(start == end && s[start] != value)
return -1;
改成
if(value < s[start] || value > s[end])
return -1;
public static int method(int[] iArray, int iSeek)
{ int index = 0;
int start = 0;
int end = iArray.length - 1;
int temp = 0;
while (true)
{
index = (start + end) / 2;
if (iArray[index] < iSeek)
{
start = index;
}
else if (iArray[index] > iSeek)
{
end = index;
}
else
{
temp = index;
break;
}
}
return temp;
}
package qujianfeng;
/**
* 1.写一个方法method1(String s),返回字符串参数中最后一个数字。
*例:“His telephone number is 123456789” return 123456789;
*"There are 1 bees,2 butterflies,3 dragonflies" return 3;
*/
public class FindLastNumber {
public static String mothod12(String s) throws Exception{
int beginIndex=-1;//返回的数字串的第一个数字的序号
int endIndex=-1;//返回的数字串的最后一个数字的序号
boolean searchedEnd=false;
for(int i=s.length()-1;i>=0;i--){
if(s.charAt(i)>='0' && s.charAt(i)<='9'){
if(!searchedEnd){
endIndex=i;
searchedEnd=true;
beginIndex=i;
}
}else{
beginIndex=i+1;
if(searchedEnd){
break;
}
}
}
if(!searchedEnd){
throw new Exception("没有找到数字");
}
return s.substring(beginIndex, endIndex+1);
}
public static void main(String[] args) throws Exception {
System.out.println(mothod12("here are 1 bees,2 butterflies,3 dragonflies"));
}
}
----------------
如此美妙的代码,居然没人顶~~
--------曾经看贴不回贴,如今回帖不看贴!
public static int method2(int n) throws Exception{
int [] array={-10,0,10,17,17,23,65,123};
int startIndex=0;
int endIndex=array.length-1;
int index;
int lastIndex=-1;
while(true){
index=startIndex+endIndex/2;
if(index==lastIndex){
throw new Exception("没有此数字");
}
lastIndex=index;
if(array[index]<n){
startIndex=index;
}else if(array[index]>n){
endIndex=index;
}else{
return index+1;
}
}
}
public static void main(String[] args) throws Exception {
System.out.println(method2(24));
}
关于正则表达式,我又仔细看了看,并试用了一下,考虑了表达正负带小数的实数字符串的正则表达式问题。各位可以参考我的博客http://blog.csdn.net/JianZhiZG/archive/2007/03/06/1522508.aspx,请多提意见,多交流。
{
String[] num = s.split("\\D+");
if(num.length == 0)throw new NumberFormatException("no number");
return Integer.parseInt(num[num.length - 1]);
}
不过给出的例子我有印象,虽然不一定完全按原例照搬,但意思是如此的
{-10,0,10,17,17,23,65,123}是一个递增序列,当n==17 return 4,n==24 return 5;根据我的理解和给出的例子,再加上面试人员最后的阅卷to Red_angelX(八戒) :我认为他要求查出不是最接近的,而是n>=“最后一个元素”的索引,当n=64,那么返回的也是5
string filter_str = "0123456789";
string temp_str;
pos = s.find_last_of(filter_str);
if (pos == string::npos) {
return 0;
}
pos_prev = s.find_last_not_of(filter_str, pos);
if (pos_prev == string::npos) {
temp_str.assign(s, 0, pos);
}
else temp_str.assign(s, pos_prev+1, pos - pos_prev);
return atoi(temp_str.c_str());
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace 二分法
{
public partial class Form1 : Form
{
int[] intArray ={ 12,19,32,56,62,78,96,96,105,250,500,523};
int seekNum = 96;
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = Convert.ToString(xunHuan());
} private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = Convert.ToString(twoPart());
} private int xunHuan()
{
int findCount = 0;
for (int i = 0; i < intArray.Length; i++)
{
findCount++;
if (intArray[i] == seekNum)
break;
} return findCount;
} private int twoPart()
{
int findCount = 0;
int intIndex = 0;
int intStart = 0;
int intEnd = intArray.Length - 1;
while (true)
{
findCount++;
intIndex = (intStart + intEnd) / 2;
if (intArray[intIndex] < seekNum)
{
intStart = intIndex;
}
else if (intArray[intIndex] > seekNum)
{
intEnd = intIndex;
}
else
{
break;
}
} return findCount;
}
}
}
/*赛门铁克的面试题
* 1.写一个方法method1(String s),返回字符串参数中最后一个数字。
例:“His telephone number is 123456789” return 123456789;
"There are 1 bees,2 butterflies,3 dragonflies" return 3;
*/public class StringFindLastNum {
public String str;
public StringFindLastNum(String str){
this.str=str;
}
public String FindLastNum(){
String result="";
for(int i=str.length()-1;i>=0;i--){
char c=str.charAt(i);
if(c>'0'&&c<='9'){
result=c+result;
}else if(!result.equals(""))
return result;
}
return result;
}
public static void main(String arg[]){
String string1="His telephone number is 123456789";
StringFindLastNum sfln1=new StringFindLastNum(string1);
System.out.println(sfln1.FindLastNum());
String string2="There are 1 bees,2 butterflies,3 dragonflies";
StringFindLastNum sfln2=new StringFindLastNum(string2);
System.out.println(sfln2.FindLastNum());
}}
{
int i = s.Length - 1;
int j =0;
while(i>=0)
{
if(isNum(s[i]))
{
j=i;
while(j>=0&&isNum(s[j])) j--;
break;
}
i--;
}
if(i<0)
return "no Number";
else
return s.Substring(j+1,i-j);
}private bool isNum(char a)
{
if(a>48&&a<=57)
return true;
return false;
}调用GetNum() 就OK了
public class test {
public static void main(String[] arg){
int a[]={-10,0,10,17,17,23,65,123};
System.out.println("17 is located at "+(bSearch(a,17)+1));
System.out.println("24 is located at "+(bSearch(a,24)+1));
}
static int bSearch(int a[], int k) {
int up=a.length-1,down=0,index=0;
boolean find=false;
while(up>=down && !find) {
index=(up-down)/2+down;
if(k>a[index]) down=index+1;
else if (k<a[index]) up=index-1;
else find=true;
}
if(!find) index+=k>a[index]?0:-1;
return index;
}
}
只不过应该注意的是,传统的二分法在没有找到时的index值是不定的,其a[index]值可能大于关键值k也可能小于它。所以,根据题目要求,此时是要定位到k值的前面一个位置,采用if(!find) index+=k>a[index]?0:-1一句对其进行调整就行了。
不过题目的另一个问题是,题目给出的数组中有重复数17。按我的理解,如果查找操作的目的是为正确插入做准备(在返回值后面插入),则当n=24时应该返回6而不是返回5,我上面的算法就是这样的。而如果的确要求返回5,则题目的意思是索引的计数应该消除重复数,则可以先将数组的重复数据删除后再用二分查找,如下:
public class Cbase {
public static void main(String[] arg){
int a[]={-10,0,10,17,17,23,65,123};
System.out.println("17 is located at "+(bSearch(a,17)+1));
System.out.println("24 is located at "+(bSearch(dDup(a),24)+1));
}
static int bSearch(int a[], int k) {
int up=a.length-1,down=0,index=0;
boolean find=false;
while(up>=down && !find) {
index=(up-down)/2+down;
if(k>a[index]) down=index+1;
else if (k<a[index]) up=index-1;
else find=true;
}
if(!find) index+=k>a[index]?0:-1;
return index;
}
static int[] dDup(int a[]) {
int total=1;
for(int i=0; i<a.length-2; i++)
if(a[i]!=a[i+1]) total++;
int r[]=new int[total];
total=0;
for(int i=0; i<a.length-2; i++)
if(a[i]!=a[i+1]) r[total++]=a[i];
r[total]=a[a.length-1];
return r;
}
}
/// <summary>
/// 1.写一个方法method1(String s),返回字符串参数中最后一个数字。
/// 例:“His telephone number is 123456789” return 123456789;
/// "There are 1 bees,2 butterflies,3 dragonflies" return 3;
/// </summary>
/// <param name="s"></param>
/// <returns></returns>
private string Method1(string s)
{
string strReturn = "";
string strTmp = "";
for(int i=0;i<s.Length;i++)
{
//if("0"<= s[i].ToString() && s[i].ToString() <="9")
if(s[i].ToString().CompareTo("0") >=0 && s[i].ToString().CompareTo("9") <=0)
{
strTmp += s[i].ToString();
strReturn = strTmp;
}
else
{
strTmp = "";
}
}
return strReturn;
} /// <summary>
/// 2.用2分法查询并返回数组中元素的索引 method2(int n)。
/// {-10,0,10,17,17,23,65,123} 当n = 17 return 4;当n=24 return 5;
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
private int Method2(int n)
{
int [] intArray = {-10,0,10,17,17,23,65,123};
int intMin = 0;
int intMax = 7;
int intMid = (intMin + intMax) / 2; while((intMin + 1) < intMax)
{
if(intArray[intMid] <= n)
{
intMin = intMid;
}
else
{
intMax = intMid;
}
intMid = (intMin + intMax) / 2;
}
return intMin;
}
public class FindNumber
{ /**
* @param args
*/
public static void main(String[] args)
{
FindNumber fn = new FindNumber();
long num = fn.findLastNumberFromStr(
"There are 1 bees,2 butterflies,3 dragonflies,His telephone number is 012304567890");
System.out.println(num);
}
private long findLastNumberFromStr(String tempStr)
{
long lastNumber = 0;
for ( int i = 0; i < tempStr.length(); i++ )
{
if ( isNumberChar( tempStr.charAt(i) ) )
{
int j = i;
while( j< tempStr.length() && isNumberChar( tempStr.charAt(j) ) )
{
j++;
}
lastNumber = Long.parseLong(tempStr.substring(i, j));
i = j;
}
}
return lastNumber;
}
private boolean isNumberChar(char c)
{
if ( c >= '0' && c <= '9' )
{
return true;
}else
return false;
}
}
String str = "There are 12157 112 bees,2 22butterfli33es,3 dragonflies 4";
String tag = "\\d+"; // ?个方法根本不?,没有正?的反映出正?表?式的性?\
List temp = new ArrayList();
Pattern p = Pattern.compile(tag);
// ?建Email字段匹配器,待??字符串Data?其参数
Matcher emailMatcher = p.matcher(str);
while (emailMatcher.find()) {
temp.add(emailMatcher.group(0));
}
System.out.println(temp.get(temp.size() - 1));
}
呵呵 真的是啊。学习了,不过你抛的那个异常是什么,自己定义的?
public static void main(String args[]){ //Split kkk=new Split();
String s="His telephone number is 123456789";
int i=0; String xy="";
String[] kgxq=s.split(" ");
for(i=0;i<kgxq.length;i++){
xy=kgxq[i];
} System.out.println(xy); }
public static void main(String args[]){
String s="His telephone number is 123456789";
int i=0;
String[] kgxq=s.split(" ");
System.out.println(kgxq[kgxq.length-1]);
}
public static void main(String args[]){
method1("hello 123 youyou !");
} public static void method1(String s){
int i=s.length()-1;
String result="";
boolean b=false; //用来保存上次比较的是不是数字
while(i>=0){
char j=s.charAt(i);
if(method2(j)){
result=result+j;
b=true;
}else{
if(b) break;
}
i--;
}
for(int p=result.length()-1;p>=0;p--) //结果,但是顺序是颠倒的
System.out.print(result.charAt(p));
}
public static boolean method2(char j){
boolean result=false;
if(j<'9'&&j>'0') result=true;
return result;
}
}
---------------------------------------------------------
凑个热闹,调试运行通过
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 写一个方法method1(String s),返回字符串参数中最后一个数字。
* 例:“His telephone number is 123456789” return 123456789;
* "There are 1 bees,2 butterflies,3 dragonflies" return 3;
*/
public class MyMethod2 {
public static void main(String[] args) throws IOException{
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
String in=buffer.readLine();
char[] re=in.toCharArray();
int le=re.length;
int i=le-1;
int beginIndex=le,endIndex=le;
boolean Bswitch=false,Eswitch=true,Pend=true;
while(i>0 && Pend){
if(Character.isDigit(re[i]) && Eswitch){
endIndex=i+1;
Eswitch=false;
Bswitch=true;
}
if(!Character.isDigit(re[i]) && Bswitch){
beginIndex=i+1;
Bswitch=false;
Pend=false;
}
i--;
}
System.out.println(in.substring(beginIndex, endIndex));
}
}
public static void main(String[] asrgs){
String nae ="this1 a ssd2 asnd 231and24";
String s = nae.replaceAll("\\D+"," ");
System.out.println(s.trim());
System.out.println(s.substring(s.length()-1,s.length()));
}
public static void main(String args[]){
int s[]={-10,0,10,17,17,23,65,123};
int n=0;
method2(s,n);
} public static void method2(int s[],int n){
int i=s.length;
int low=0,high=i-1,middle;
boolean result=true;
while(low<=high&&result){
middle=(low+high)/2;
if(s[middle]==n){
System.out.println(n+"is at "+middle);
result=false;
}else if(s[middle]>n){
high=middle-1;
}else{
low=middle+1; }
}
}
}
----------------------------------------------------------
第二题,调试运行通过!
算法好久没接触了,几乎都要忘记了
package myapp;
/**
* 用2分法查询并返回数组中元素的索引 method2(int n)。
* {-10,0,10,17,17,23,65,123} 当n = 17 return 4;当n=23 return 5;
*/public class MyMethod3 {
public static void main(String args[]){
int[] re={-10,0,10,17,17,23,65,123};
int endp=re.length-1,beginp=0;
System.out.println("默认数组为递增序列,否则二分查找无意义!");
int nu=-10; //被查找的数
int i=0; //指针
boolean find=false;
while(beginp<=endp && !find){
i=(beginp+endp+1)/2;
//System.out.println(i);
if(nu<re[i]){
endp=i-1;
}
else if(nu==re[i]){
find=true;
}
else{
beginp=i+1;
}
}
if(find){
System.out.println("找到该数,序号为:"+i);
}else{
System.out.println("未发现!");
}
}
}
String getLastNumber(String str){
int m = -1;
int n = -1;
for(int i = str.length() - 1; i >= 0; i--){
if(str.charAt(i) >= '0' && str.charAt(i) <= '9'){
if(m == -1){
m = i;
}else{
n = i;
}
}
else{
if(m != -1){
break;
}
}
}
if(m == -1 && n == -1){
return null;
}
if(n == -1){
return str.charAt(m) + "";
}
return str.substring(n, m+1);
}
public static String getLastNumberInString(String s){
String[] numArray=s.split("[\\D]+");
return numArray[numArray.length-1];
}
为什么不这样做
String SubjectString = "His telephone number is 123456789";
String ResultString = null;
try {
Pattern Regex = Pattern.compile("\\d+$",Pattern.CANON_EQ);
Matcher RegexMatcher = Regex.matcher(SubjectString);
if (RegexMatcher.find()) {
ResultString = RegexMatcher.group();
}
} catch (PatternSyntaxException ex) {
// Syntax error in the regular expression
}
{17,17,17,17,17,17,17,17,17,17} 当n = 17 return ?
System.out.println(getStringNumber("There are 1 bees,2 butterflies,111ddd1113"));
System.out.println( checkNumber(123));
-------------------------------------------------
public static String method1(String t) {
Pattern p = Pattern.compile("(\\d+)[^\\d]{0,}$", Pattern.CASE_INSENSITIVE|Pattern.MULTILINE);
Matcher m = p.matcher(t);
if(m.find(1))
return m.group(1);
return null ;
}
public static final Integer test[] = {-10,0,10,17,17,23,65,123} ;
public static int method2(int t) {
int checkindex = test.length;
int start = 1;
int end = test.length ;
while(true){
if(checkindex==(start+end)/2)
return checkindex ;
checkindex = (start+end)/2;
if(test[checkindex]>t){
end = checkindex;
}else if(test[checkindex]==t){
return checkindex+1;
}else
start = checkindex;
}
}----------------------------------
大概写了下.测试通过.我觉得应该严格按照别人出题得要求来,不应该添加多余得方法和参数.
另外..很久没写java了.所以是在eclipse下写出来得.应该没什么关系吧.
int b,c=2;
StringBuffer cc=new StringBuffer();
b=s.length();
for(int i=b-1;i>=0;i--){
if((int)(s.charAt(i)-'0')>=0&&(int)(s.charAt(i)-'0')<=9){
z.addFirst(s.charAt(i));
c=0;
}
else
if(c==0){
break;
}
else{
continue;
}
}
if(c==0){
for(int i=0;i<z.size();i++){
cc.append(z.get(i));
}
System.out.println(cc);
}
else
System.out.println("该字符串中无数字");
}
当然你想表现一下能力可以用正则表达式。
第二题Arrays.binaraySearch(),看看API吧, 熟悉一下Arrays和Collections类。
题目的意思应该是查到等于指定的值的最后一个,如果没有等于,那么是最接近的一个。