如何标记循环索引?

huangapple go评论72阅读模式
英文:

How to mark loop index?

问题

以下是翻译好的部分:

你好,我正在尝试标记已经访问过的位置。

这段字符串

String s = "123+4+3233"; 

以及多个索引。

所以我将对它进行循环,这将是内循环

 public static void main(String[] args) {
        String s = "123+4+3233";
        String n = "";
        int count = 0;
        List<String> lists = new ArrayList<>();
        for(int i=0; i < s.length(); i++){
            if(s.charAt(i) > 0){
                for(int k = i; k < s.length(); k++){
                    if(s.charAt(k) == '+'){
                        break;
                    }
                    if(s.charAt(k) != '+'){
                        n += s.charAt(k);
                    }
                }
                if(!(n.isEmpty())){
                    lists.add(n);
                    n = "";
                }
                if(s.charAt(i) == '+'){count++; }
            }
       }
        System.out.println(lists);
    }

这个结果会出现,因为我不知道如何标记已经访问过的位置。
不能使用分割(SPLIT)

[123, 23, 3, 4, 3233, 233, 33, 3]

我只想要

期望的结果

[123, 4, 3233]
英文:

Hi I am trying to mark a position that already been visited

This string

String s = &quot;123+4+3233&quot; 

and multiple index.

So I will loop it gotta be inner loop

 public static void main(String[] args) {
        String s = &quot;123+4+3233&quot;;
        String n = &quot;&quot;;
        int count = 0;
        List&lt;String&gt; lists = new ArrayList&lt;&gt;();
        for(int i=0; i &lt; s.length(); i++){
            if(s.charAt(i) &gt; 0){
                for(int k = i; k &lt; s.length(); k++){
                    if(s.charAt(k) == &#39;+&#39;){
                        break;
                    }
                    if(s.charAt(k) != &#39;+&#39;){
                        n += s.charAt(k);
                    }
                }
                if(!(n.isEmpty())){
                    lists.add(n);
                    n = &quot;&quot;;
                }
                if(s.charAt(i) == &#39;+&#39;){count++; }
            }
       }
        System.out.println(lists);
    }

and this result would come out Since I don't know how to mark the place that already visited.
CANNOT USE SPLIT

[123, 23, 3, 4, 3233, 233, 33, 3]

I just want

EXPECTED RESULT

[123, 4, 3233]

答案1

得分: 0

public static void main(String[] args) {
String s = "123+4+3233";
String n = "";
int count = 0;
List lists = new ArrayList<>();
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) > 0) {
int k = i;
for (; k < s.length(); k++) {
if (s.charAt(k) == '+') {
break;
}
if (s.charAt(k) != '+') {
n += s.charAt(k);
}
}
if (!(n.isEmpty())) {
lists.add(n);
n = "";
}
if (s.charAt(i) == '+') {
count++;
}

        if (k > i)
            i = k - 1; //advance i to the right position
    }
}
System.out.println(lists);

}

英文:

you don't have to mark the place that visited, simply advance the i index:

 public static void main(String[] args) {
    String s = &quot;123+4+3233&quot;;
    String n = &quot;&quot;;
    int count = 0;
    List&lt;String&gt; lists = new ArrayList&lt;&gt;();
    for(int i=0; i &lt; s.length(); i++){
        if(s.charAt(i) &gt; 0){
            int k = i;
            for(; k &lt; s.length(); k++){
                if(s.charAt(k) == &#39;+&#39;){
                    break;
                }
                if(s.charAt(k) != &#39;+&#39;){
                    n += s.charAt(k);
                }
            }
            if(!(n.isEmpty())){
                lists.add(n);
                n = &quot;&quot;;
            }
            if(s.charAt(i) == &#39;+&#39;){count++; }

            if(k &gt; i)i = k - 1; //advance i to the right position
        }
   }
    System.out.println(lists);
}

output:

[123, 4, 3233]

答案2

得分: 0

我认为你稍微把这个问题复杂化了一些。以下是我解决的方法:

String s = 123+4+3233;
ArrayList<String> nums = new ArrayList<>();

int lastPlus = 0; // 用于追踪最后一个 "+" 出现的索引

for (int j = 0; j < s.length(); j++) {
    if (s.charAt(j).equals("+")) {
        nums.add(s.substring(lastPlus, j));
        lastPlus = j + 1; // "+1" 跳过下一个 substring() 中的加号
    }

    // 最后一个 "if" 语句是为了添加最后一部分数字
    if (j == s.length() - 1 && !(s.charAt(j).equals("+"))) {
        nums.add(s.substring(lastPlus));
    }
}

这应该可以工作。

英文:

I think you are complicating this one a bit. Here's how I would solve it:

String s = 123+4+3233;
ArrayList&lt;String&gt; nums = new ArrayList&lt;&gt;();

int lastPlus = 0; // Keeping track of the last index at which there was a &quot;+&quot;

for (int j = 0 j &lt; s.length(); j++) {
    if (s.charAt(j).equals(&quot;+&quot;)) {
        nums.add(s.substring(lastPlus, j));
        lastPlus = j + 1; //The &quot;+1&quot; skips over the plus for the next substring()
    }

    // This last &#39;if&#39; statement is for adding that last bit of numbers
    if (j == s.length() - 1 &amp;&amp; !(s.charAt(j).equals(&quot;+&quot;))) { 
        nums.add(lastPlus);
    }
}

This should work.

答案3

得分: 0

我认为可以用更简洁的方式来实现:

public static void main(String... args) {
    String s = "123+4+3233";
    List<String> lists = new ArrayList<>();
    for (String temp : s.split("\\+")) {
        lists.add(temp);
    }
    System.out.println(lists);
}
英文:

I think it can be done in a shorter way:

public static void main(String... args){
    String s = &quot;123+4+3233&quot;;
    List&lt;String&gt; lists = new ArrayList&lt;&gt;();
    for(int i = 0; i&lt;s.length();i++){
        String temp = &quot;&quot;;
        while(i&lt;s.length() &amp;&amp; s.charAt(i) != &#39;+&#39;){
            temp+= s.charAt(i);
            i++;
        }
        lists.add(temp);
    }
    System.out.println(lists);
}

huangapple
  • 本文由 发表于 2020年9月8日 10:35:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/63786403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定