Problem:
Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.)
sumNumbers("abc123xyz") → 123
sumNumbers("aa11b33") → 44
sumNumbers("7 11") → 18
Solution:
public int sumNumbers(String str) { int len = str.length(); int sum = 0; String tmp = ""; for (int i = 0; i < len; i++) { if (Character.isDigit(str.charAt(i))) { if (i < len-1 && Character.isDigit(str.charAt(i+1))) { tmp += str.charAt(i); } else { tmp += str.charAt(i); sum += Integer.parseInt(tmp); tmp = ""; } } } return sum; }
public int sumNumbers(String str) {
ReplyDeleteint sum=0, num=0;
for (char c : (str+".").toCharArray())
if (Character.isDigit(c)){
if (num > 0)
num *= 10;
num += (c - '0');
} else {
sum += num;
num = 0;
}
return sum;
}
Sorry, that 2nd "if" is useless...
Deletepublic int sumNumbers(String str) {
int sum=0, num=0;
for (char c : (str+".").toCharArray())
if (Character.isDigit(c))
num = num*10 + (c - '0');
else {
sum += num;
num = 0;
}
return sum;
}
public int sumNumbers(String str) {
ReplyDeleteint sum = 0;
String str2 = str.replaceAll("[^0-9]+"," ");
String arr[] = str2.trim().split(" ");
for(int i = 0;i<arr.length;i++){
if(arr[i]!=null && !arr[i].isEmpty()){
sum += Integer.parseInt(arr[i]);
}
}
return sum;
}
public int sumNumbers(String str) {
ReplyDeleteString tmp="";
int sum=0;
boolean status=false;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
status = false;
if (!status) {
tmp = tmp + str.charAt(i);
}
}
if (i == str.length() - 1 && Character.isDigit(str.charAt(i)))
{
sum = sum + Integer.parseInt(tmp);
} else if (!Character.isDigit(str.charAt(i))) {
if (!tmp.isEmpty())
sum = sum + Integer.parseInt(tmp);
status = true;
tmp = "";
}
}
return sum;
}
public int sumNumbers(String str) {
ReplyDeleteint sum=0;
for(int i=0;i<str.length();i++){
if(Character.isDigit(str.charAt(i))){
for(int j=i+1;j<str.length()+1;j++){
if(j==str.length()||!Character.isDigit(str.charAt(j))){
sum+=Integer.parseInt(str.substring(i,j));
i=j;
break;
}
}
}
}
return sum;
}
nice...
Deletepublic int sumNumbers(String str) {
ReplyDeleteint sum = 0;
for(int i = 0; i < str.length(); i++){
if(Character.isDigit(str.charAt(i))){
String num = "";
int j;
for(j = i; j < str.length() && Character.isDigit(str.charAt(j)); j++){
num += str.charAt(j);
}
i = j--;
sum += Integer.parseInt(num);
}
}
return sum;
}
public int sumNumbers(String str) {
ReplyDeletestr = str.replaceAll("\\D", " ");
str = str.replaceAll(" +", " ").trim();
if(str.length()<1) return 0;
String[] parts = str.split(" ");
int result = 0;
for(int i=0;i<parts.length;i++){
result = result + Integer.parseInt(parts[i]);
}
return result;
}
public int sumNumbers(String str) {
ReplyDeleteint sum = 0;
String tmp = "";
for(int i=0; i<str.length(); i++){
if(Character.isDigit(str.charAt(i))){
tmp += str.charAt(i);
if(i == str.length()-1 || !Character.isDigit(str.charAt(i+1))){
sum += Integer.parseInt(tmp);
tmp = "";
}
}
}
return sum;
}
public int sumNumbers(String str) {
ReplyDeletereturn Arrays.stream(str.split("\\D+"))
.filter(s -> !s.equals(""))
.mapToInt(Integer::parseInt).sum();
}
is it possible to do it with two for loops, using no parseInt?
ReplyDeletepublic int sumNumbers(String str) {
ReplyDelete// Recursive Solution! //
if (str.length() < 1)
{
return 0;
}
else
{
int count = 0;
while (count < str.length() && Character.isDigit(str.charAt(count)))
{
count++;
}
if (count > 0)
{
return Integer.parseInt(str.substring(0, count)) + sumNumbers(str.substring(count));
}
else
{
return sumNumbers(str.substring(1));
}
}
}
public int sumNumbers(String str) {
ReplyDeleteint sum = 0, num = 0;
for (int i = 0; i < str.length(); i++) {
while (i < str.length() && Character.isDigit(str.charAt(i))) {
num = (num * 10) + Integer.parseInt(str.substring(i, i + 1));
i++;
}
sum += num;
num = 0;
}
return sum;
}
public int sumNumbers(String str) {
ReplyDeleteint i = 0;
int result = 0;
int result2 = 0;
int sum = 0;
String n1 = "";
for (i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
n1+=str.charAt(i);
result = Integer.parseInt(n1);
}
else if (!Character.isDigit(str.charAt(i)) ) {
if(sum!=result+result2) {
sum += result;
n1 = "";
result=0;
}
}
if(i==str.length()-1 && !n1.isEmpty()){
sum+=result;
}
}
return sum;
}
Sorry, int result2 is not needed, i forgot to remove it of the code. Without it:
Deletepublic int sumNumbers(String str) {
int i = 0;
int result = 0;
int sum = 0;
String n1 = "";
for (i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
n1+=str.charAt(i);
result = Integer.parseInt(n1);
}
else if (!Character.isDigit(str.charAt(i)) ) {
if(sum!=result) {
sum += result;
n1 = "";
result=0;
}
}
if(i==str.length()-1 && !n1.isEmpty()){
sum+=result;
}
}
return sum;
}
public int sumNumbers(String str) {
ReplyDeletestr+="x";
int number = 0;
String digit = "";
int newDigit = 0;
for(int i=0; i<str.length()-1; i++){
if(Character.isDigit(str.charAt(i))){
digit+=str.charAt(i);
if(!Character.isDigit(str.charAt(i+1))) {
newDigit = Integer.parseInt(digit);
number += newDigit;
digit = "";
}
}
}
return number;
}
public boolean has12(int[] nums) {
ReplyDeleteboolean flag1 = false, flag2 = false;
for(int i = 0; i < nums.length; i++){
if(nums[i] == 1) flag1 = true;
if(nums[i] == 2 && flag1) flag2 = true;
if(flag1 && flag2) return true;
}
return false;
}
Oh sry, this is a solution for Array2 has12.
Deletepublic int sumNumbers(String str) {
ReplyDeleteString a = "0";
int b = 0;
for(int i = 0; i < str.length(); i++){
if(Character.isDigit(str.charAt(i))){
a += str.substring(i, i + 1);
if(i == str.length() - 1){
b += Integer.parseInt(a);
return b;
}
}else{
b += Integer.parseInt(a);
a = "0";
}
}
return b;
}
public static int sumNumbers(String str) {
ReplyDeleteint sum=0;
String str2=str.replaceAll("[^0-9]"," ");
String []arr=str2.split(" ");
for(int i=0; i<arr.length;i++) {
if(!arr[i].isEmpty() ) {
sum+=Integer.parseInt(arr[i]);
}
}
return sum;
}